jschneier/django-storages

How to provide signed URL with GCP

Closed this issue · 1 comments

Hello,

I have a default GCP backend where everything is stored in publicRead where I manage to serve the public url. However I'd like some file to be served with signed URL.

I do not find the documentation to do this task.
Currently I'm just using the ImageField like this:

def callable_path(instance, filename):
    return f"object/{instance.id}/{filename}"


class ModelWithImages(models.BaseModel):
    # ...
    image = models.ImageField(upload_to=callable_path)

Here is what my settings looks like:

STORAGES = {
    "default": {
        "BACKEND": "storages.backends.gcloud.GoogleCloudStorage",
        "OPTIONS": {
            "bucket_name": "my-public-bucket",
            "credentials": service_account.Credentials.from_service_account_file("/path/to/secret-2d6e51b7cd7f.json"),
            "default_acl": "publicRead",
        },
    },
}

What modification should I make to store some images more privately and serve them through signed URL ?

Hi this question is isomorphic to asking about using 2 separate backends. The docs for managing files have a decent but not amazing overview. In short you want to create two separate instances in settings. So like you have default you'd want to make another such as public (if you want default to be private). Then you'd do something like:

from django.core.files.storage import storages

class M(models.Mode):
    file = FileField(storage=storages["public"])