英文:
How to use Django ImageField to upload to Google Cloud Storage instead of local
问题
I want to use Django's ImageField on a model so people can upload a logo. I found the ImageField however reading from the docs it says Django stores files locally, using the [
MEDIA_ROOT](https://docs.djangoproject.com/en/4.2/ref/settings/#std-setting-MEDIA_ROOT)
and MEDIA_URL
settings.py
I'm wondering how I can change the upload_to=
behavior to upload to a gcp bucket instead? If it's possible when I call .save
what is saved to the database, the gcp bucket url?
From reading the docs it doesn't say one way or the other if this is possible or if all images should be stored locally where the service is being ran. I have thought of saving the binary images directly to the db but that seems inefficient.
英文:
I want to use Django's ImageField on a model so people can upload a logo. I found the ImageField however reading from the docs it says Django stores files locally, using the
MEDIA
ROOT
***and
MEDIAURL
** settings.py
I'm wondering how I can change the *upload_to=
behavior to upload to a gcp bucket instead? If it's possible when I call .save
what is saved to the database, the gcp bucket url?
From reading the docs it doesn't say one way or the other if this is possible or if all images should be stored locally where the service is being ran. I have thought of saving the binary images directly to the db but that seems inefficient.
答案1
得分: 0
您可以使用 django-storages
和 google-cloud-storage
。以下是文档链接:Google Cloud Storage
设置默认存储和存储桶名称在您的 settings.py 文件中:
django < 4.2
DEFAULT_FILE_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage'
django >= 4.2
STORAGES = {"default": {"BACKEND": "storages.backends.gcloud.GoogleCloudStorage"}}
GS_BUCKET_NAME = 'YOUR_BUCKET_NAME_GOES_HERE'
要允许 django-admin collectstatic 自动将静态文件放入您的存储桶中,请在您的 settings.py 中设置以下内容:
django < 4.2
STATICFILES_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage'
django >= 4.2
STORAGES = {"staticfiles": {"BACKEND": "storages.backends.gcloud.GoogleCloudStorage"}}
用法
字段
完成后,default_storage 将是 Google Cloud Storage
from django.core.files.storage import default_storage
print(default_storage.__class__)
<class 'storages.backends.gcloud.GoogleCloudStorage'>
# 这样,如果您定义一个新的 FileField,它将使用 Google Cloud Storage
>>> from django.db import models
>>> class Resume(models.Model):
... pdf = models.FileField(upload_to='pdfs')
... photos = models.ImageField(upload_to='photos')
...
英文:
You can use django-storages
and google-cloud-storage
. Here is the document link: Google Cloud Storage
> Set the default storage and bucket name in your settings.py file
> # django < 4.2
> DEFAULT_FILE_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage'
>
> # django >= 4.2
> STORAGES = {"default": {"BACKEND": "storages.backends.gcloud.GoogleCloudStorage"}}
> GS_BUCKET_NAME = 'YOUR_BUCKET_NAME_GOES_HERE'
> To allow django-admin collectstatic to automatically put your static files in > your bucket set the following in your settings.py:
> # django < 4.2
> STATICFILES_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage'
> # django >= 4.2
> STORAGES = {"staticfiles": {"BACKEND": "storages.backends.gcloud.GoogleCloudStorage"}}
Usage
Fields
Once you’re done, default_storage will be Google Cloud Storage
from django.core.files.storage import default_storage
print(default_storage.__class__)
<class 'storages.backends.gcloud.GoogleCloudStorage'>
# This way, if you define a new FileField, it will use the Google Cloud Storage
>>> from django.db import models
>>> class Resume(models.Model):
... pdf = models.FileField(upload_to='pdfs')
... photos = models.ImageField(upload_to='photos')
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论