Google Cloud Stoarge using Django's library django-storages I solved the file deletion method by trial and error, so I will summarize the memo including the upload method.
The django-storages page has basic setup instructions.
settings.py
DEFAULT_FILE_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage'
GS_BUCKET_NAME = 'YOUR_BUCKET_NAME_GOES_HERE'
When,
settings.py
from google.oauth2 import service_account
GS_CREDENTIALS = service_account.Credentials.from_service_account_file(
"path/to/credentials.json"
)
If you describe the two points and define the model as usual, it will work up to upload. The following is a model example of an image file.
models.py
class Image(models.Model):
image = models.ImageField(upload_to='image/')
def __str__(self):
return self.image.name
However, up to this point, even if you delete the record, the Google Cloud Storage file will not be deleted. This seems to be the design concept of Django itself (I just read the secondary information), not only when using Google Cloud Storage, but also to implement file deletion by myself.
Its implementation requires the decorator to define a function that will be called when deleting records. If you delete a file with Google cloud console (when there is no file on Google Cloud Storage side at the time of this process), an error will occur, so exception handling is included.
models.py
from django.dispatch import receiver
from google.cloud import exceptions
@receiver(models.signals.post_delete, sender=Image)
def auto_delete_image(sender, instance, **kargs):
file = instance.image
try:
file.storage.delete(name=file.name)
except exceptions.NotFound as e:
print(e)
If you set the environment variable GOOGLE_APPLICATION_CREDENTIALS
, you can also implement it in the google.cloud module as follows.
However, in this case, an error occurred unless storage.buckets.get
was assigned as the authority of google cloud.
model.py
from google.cloud import storage
from django.conf import settings
@receiver(models.signals.post_delete, sender=Image)
def auto_delete_image(sender, instance, **kargs):
storage_client = storage.Client()
bucket = storage_client.get_bucket(settings.GS_BUCKET_NAME)
blob = bucket.blob(instance.image.name)
try:
blob.delete()
except exceptions.NotFound as e:
print(e)
I will supplement the settings on the Google Cloud Storage side. I haven't verified it well, so I may not need everything, but in my environment I issue a service account with the following permissions.
The official link is posted for the actual setting method.
-Create and manage custom roles --Create and manage service accounts
Recommended Posts