You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GKE部署Django项目使用Cloud Storage时的文件上传异常问题求助(NoSuchKey及AttributeError)

GKE部署Django项目使用Cloud Storage时的文件上传异常问题求助(NoSuchKey及AttributeError)

大家好,我在GKE上部署Django 5.1 + React项目时,搭配Cloud SQL做数据库、Cloud Storage存静态和媒体文件,遇到了两个文件上传相关的异常,想请大家帮忙看看:

最初的问题:NoSuchKey错误

我一开始的配置是这样的:

settings.py 配置

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.1/howto/static-files/

DEFAULT_FILE_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage'
STATICFILES_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage'

GS_BUCKET_NAME = 'my-bucket'
GS_MEDIA_BUCKET_NAME = 'my-bucket'
GS_DEFAULT_ACL = 'publicRead'
GS_FILE_OVERWRITE = False

STATIC_URL = f"https://storage.googleapis.com/{GS_BUCKET_NAME}/static/"
MEDIA_URL = f"https://storage.googleapis.com/{GS_MEDIA_BUCKET_NAME}/media/"

deployment.yaml 配置

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: app
  template:
    metadata:
      labels:
        app: app
    spec:
      serviceAccountName: my-service-account
      containers:
      - name: app
        image: asia-east1-docker.pkg.dev/my-project-id/app/backend:latest
        ports:
        - containerPort: 8000
        env:
          - name: DB_USER
          - name: DB_PASSWORD
          - name: DB_NAME
          - name: DB_HOST
          - name: DB_PORT
        resources:
          requests:
            memory: "512Mi"
            cpu: "500m"
          limits:
            memory: "1Gi"
            cpu: "1"
      - name: cloud-sql-proxy
        image: gcr.io/cloud-sql-connectors/cloud-sql-proxy:latest
        args:
        volumeMounts:
          - name: cloudsql
            mountPath: /cloudsql
        securityContext:
          runAsNonRoot: true
        resources:
          requests:
            memory: "256Mi"
            cpu: "100m"
          limits:
            memory: "512Mi"
            cpu: "250m"
      volumes:
      - name: cloudsql
        emptyDir:
          medium: Memory

现象是:通过DRF API或者Django admin上传文件后,返回的访问URL会提示NoSuchKey错误,错误内容如下:

This XML file does not appear to have any style information associated with it. The document tree is shown below.

return (<Code>NoSuchKey</Code>)

return (<Message>The specified key does not exist.</Message>)

return (<Details>No such object: my-bucket/media/uploads/01.JPG</Details>
)

但我手动通过GCP控制台上传文件,或者用gsutil cp .gitignore gs://my-bucket/media/命令上传的文件,都能正常访问。


后续更新:修改配置后API正常,但Admin报AttributeError

后来我调整了settings.py的配置,改成了针对Django 5.1的写法:

STORAGES = {
    "default": {
        "BACKEND": "storages.backends.gcloud.GoogleCloudStorage",
        "OPTIONS": {
            "bucket_name": "erp-backend",
            "location": "media",
            "project_id": "smart-portfolio-412002",
        },
    },
    "staticfiles": {
        "BACKEND": "storages.backends.gcloud.GoogleCloudStorage",
        "OPTIONS": {
            "bucket_name": "erp-backend",
            "location": "static",
        },
    },
}

STATIC_URL = "https://storage.googleapis.com/erp-backend/static/"
MEDIA_URL = "https://storage.googleapis.com/erp-backend/media/"

现在通过DRF API上传的文件能正常出现在Cloud Storage的bucket里了,但进入Django admin操作时,会抛出AttributeError:

Exception Value:    
you need a private key to sign credentials.the credentials you are currently using <class 'google.auth.compute_engine.credentials.Credentials'> just contains a token. see https://googleapis.dev/python/google-api-core/latest/auth.html#setting-up-a-service-account for more details.

我的需求

现在希望不管是通过DRF API还是Django admin上传文件,都能正常将文件存储到Cloud Storage的bucket中,同时Django admin能正常使用,不会抛出异常。

备注:内容来源于stack exchange,提问作者王儷穎

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 10:47:59