Django Cloud Storage NoSuchKey

I deploy a Django/React project with GKE, Cloud SQL as DB, Cloud Storage for static file. When I trying upload files(both by Django admin and API), the url I get always tells me

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<Error>
<Code>NoSuchKey</Code>
<Message>The specified key does not exist.</Message>
<Details>No such object: my-bucket/media/uploads/01.JPG</Details>
</Error>

I tryed directly upload file in GCP console and file can be visited. Also when I use

gsutil cp .gitignore gs://my-bucket/media/

file can be visited.

below are my 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_ROOT = '/app/static'
STATIC_URL = f"https://storage.googleapis.com/{GS_BUCKET_NAME}/static/"
MEDIA_URL = f"https://storage.googleapis.com/{GS_MEDIA_BUCKET_NAME}/media/"

and 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

What I want is that when I upload by API or Django admin, I can find files in my bucket.

The errors you're getting are due to the auth settings of your app. You can't sign URLs with the access token only (default way your app uses). Thus you can either:

  1. Configure app to use IAM Sign Blob API. Prerred way, minimal changes, but there is a quota limits;
  2. Configure app to use a mounted private key of the Service Account. More actions, might be less secure.

Please read the guide you attached carefully starting from the Authentication settings section. You will find all the answers here.


When going to Django admin, there's 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.

Your guide says:

If your app handles signed (expiring) urls, then read through the options in the Settings for Signed Urls in the following section

Back to Top