How to generate signed URLs for files in Google Cloud Storage in Django Rest?


hardik24

I am using Django Rest Framework to make an API. Meanwhile, I'm using Google Cloud Storage to store media files. I have some questions:

  1. How to generate URL of signed file using django rest framework? I've been searching the web with the same tool but can't get a clear picture. Link

  2. So if I delete a file in the database, will that file also be automatically deleted from Google Cloud Storage?

Thanks in advance! ! !

exaggerate

How to generate URL of signed file using django rest framework?

The Django storage module will automatically take care of this for you.

You can easily check it, if you have uploaded the asset to the server, check the url in the model's FileField. (called in my case raw)

enter image description here

If you go to the right, you will see the Signature parameter in the URL. If you reload the page, you'll see the value of the signature change, proving that you're actually using a temporarily signed URL.

The default expiration time is 86400 seconds (1 day), you can change it with a variableGS_EXPIRATION

So if I delete a file in the database, will that file also be automatically deleted from Google Cloud Storage?

Not by default, but you can easily add this functionality by overriding the Model's delete method

class MyModel(models.Model):
    myfile = models.FileField(upload_to='filename/')

    def delete(self, *args, **kwargs):
        self.myfile.delete()
        super(MyModel, self).delete(*args, **kwargs)

Related


Google Cloud Storage: CORS settings not working for signed URLs

Simon PUTResponses to requests with signed URLs do not contain headers Access-Control-Allow-Origin. import os from datetime import timedelta import requests from google.cloud import storage os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = <path to google crede

Creating signed URLs for Google Cloud Storage using NodeJS

stukennedy I'm trying to create a signature for a privately stored file in Google Cloud Storage; so that I can distribute a time-limited link. Currently doing this and it makes a signature that's too short ... where am I going wrong? var crypto = require("cryp

Using Version Control with Signed URLs in Google Cloud Storage

Nick G. I'm having trouble signing a GET request to Google Cloud Storage (GCS) when specifying a "generation" (version number) on an object. Signing the URL without generating it works like a charm, and the GET request works fine. However, when I prepend the p

Creating signed URLs for Google Cloud Storage using NodeJS

stukennedy I'm trying to create a signature for a privately stored file in Google Cloud Storage; so that I can distribute a time-limited link. Currently doing this and it makes a signature that's too short ... where am I going wrong? var crypto = require("cryp

Using Version Control with Signed URLs in Google Cloud Storage

Nick G. I'm having trouble signing a GET request to Google Cloud Storage (GCS) when specifying a "generation" (version number) on an object. Signing the URL without generating it works like a charm, and the GET request works fine. However, when I prepend the p

Google Cloud Storage: CORS settings not working for signed URLs

Simon PUTResponses to requests with signed URLs do not contain headers Access-Control-Allow-Origin. import os from datetime import timedelta import requests from google.cloud import storage os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = <path to google crede

Using Version Control with Signed URLs in Google Cloud Storage

Nick G. I'm having trouble signing a GET request to Google Cloud Storage (GCS) when specifying a "generation" (version number) on an object. Signing the URL without generating it works like a charm, and the GET request works fine. However, when I prepend the p

Put files into Google Cloud Storage (GCS) via signed URL

kctang: I'm trying to use a "signed URL" to access/upload files to Google Cloud Storage (GCS). Follow the instructions at https://developers.google.com/storage/docs/accesscontrol#Signing-Strings what did I do: "Web Application" is registered in Google Cloud Co

Put files into Google Cloud Storage (GCS) via signed URL

tea soup I'm trying to use a "signed URL" to access/upload files to Google Cloud Storage (GCS). Follow the instructions at https://developers.google.com/storage/docs/accesscontrol#Signing-Strings what did I do: "Web Application" is registered in Google Cloud C

Put files into Google Cloud Storage (GCS) via signed URL

kctang: I'm trying to use a "signed URL" to access/upload files to Google Cloud Storage (GCS). Follow the instructions at https://developers.google.com/storage/docs/accesscontrol#Signing-Strings what did I do: "Web Application" is registered in Google Cloud Co

Put files into Google Cloud Storage (GCS) via signed URL

kctang: I'm trying to use a "signed URL" to access/upload files to Google Cloud Storage (GCS). Follow the instructions at https://developers.google.com/storage/docs/accesscontrol#Signing-Strings what did I do: "Web Application" is registered in Google Cloud Co