如何使用Django的ImageField将文件上传到Google Cloud Storage而不是本地存储。

huangapple go评论65阅读模式
英文:

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 MEDIAROOT***andMEDIAURL** 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-storagesgoogle-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 = &#39;storages.backends.gcloud.GoogleCloudStorage&#39;
>
> # django >= 4.2
> STORAGES = {&quot;default&quot;: {&quot;BACKEND&quot;: &quot;storages.backends.gcloud.GoogleCloudStorage&quot;}}

> 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 = &#39;storages.backends.gcloud.GoogleCloudStorage&#39;

> # django >= 4.2
> STORAGES = {&quot;staticfiles&quot;: {&quot;BACKEND&quot;: &quot;storages.backends.gcloud.GoogleCloudStorage&quot;}}

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__)
&lt;class &#39;storages.backends.gcloud.GoogleCloudStorage&#39;&gt;
# This way, if you define a new FileField, it will use the Google Cloud Storage

&gt;&gt;&gt; from django.db import models
&gt;&gt;&gt; class Resume(models.Model):
...     pdf = models.FileField(upload_to=&#39;pdfs&#39;)
...     photos = models.ImageField(upload_to=&#39;photos&#39;)
...

huangapple
  • 本文由 发表于 2023年6月29日 07:01:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577180.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定