简体   繁体   中英

Django + Google Storage (GCP) multiple bucket

For now i use django 1.11 and using google cloud platform (google storage) to store pdf.

my bucket is not open for public and everything was fine, but now i need to store public file (people with link can access uploaded pdf file)

I was thinking is it possible for me to have options(choose the bucket target) when upload to google storage?

Maybe something like this codes

from django.db import models
from storages.backends.s3boto import S3BotoStorage

class MyModel(models.Model):
    file_1 = models.FileField() # Uses default storage
    file_2 = models.FileField(storage=S3BotoStorage(bucket='other-bucket'))

I want to do the same as this, but using GCP instead of AWS django-storages with multiple S3 Buckets

I'm sorry if this question is too basic, but I've never found the answer until now

Thanks for your attention, and hope you can help me and the others who faced the same problem.

SOLVED Thanks to Thomas Plihibert

This codes worked well in my project

from django.db import models
from storages.backends.gcloud import GoogleCloudStorage

class MyModel(models.Model):
    file_1 = models.FileField() # Uses default storage
    file_2 = models.FileField(storage= GoogleCloudStorage(bucket_name='other-bucket'))

Hope this question useful for people who looking for the same case.

Not tested but logically you can do this:

from django.db import models
from storages.backends.gcloud import GoogleCloudStorage

class MyModel(models.Model):
    file_1 = models.FileField() # Uses default storage
    file_2 = models.FileField(storage= GoogleCloudStorage(bucket='other-bucket'))

You can look how the backend work here: https://github.com/jschneier/django-storages/blob/master/storages/backends/gcloud.py

A small change - it should use bucket_name instead of bucket .

More info here https://github.com/jschneier/django-storages/issues/744#issuecomment-526243643

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM