【发布时间】:2020-02-28 17:49:21
【问题描述】:
我正在使用 Google Cloud Sql 代理在本地计算机上运行文件管理器应用程序,并将文件存储在 Google Cloud Storage 存储桶中。 该文件正在保存在存储桶中,但 FileField 设置为“null”。我希望它显示我可以访问该文件的 url。
我正在关注这个答案Configure Django and Google Cloud Storage?
我已将 Google Cloud Storage 存储分区设为公开。
Django 模型:
class Document(models.Model):
docfile = models.FileField(upload_to='documents/%Y/%m/%d')
设置.py:
#MEDIA_URL = "/media/"
#MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
DEFAULT_FILE_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage'
GS_BUCKET_NAME = 'printhub-files'
GS_PROJECT_ID = 'preasy-53c43'
GS_MEDIA_BUCKET_NAME = 'printhub-files'
# GS_STATIC_BUCKET_NAME = '<name-of-static-bucket>'
# STATIC_URL = 'https://storage.googleapis.com/{}/'.format(GS_STATIC_BUCKET_NAME)
MEDIA_URL = 'https://storage.googleapis.com/{}/'.format(GS_MEDIA_BUCKET_NAME)
预期结果:
{
"id": 13,
"docfile": "https://storage.googleapis.com/bucket/documents/2019/11/03/myfile.pdf",
}
实际结果:
{
"id": 13,
"docfile": null,
}
如果我将我的 Settings.py 更改为(取消注释第 1,2 行。注释第 4 行),该文件将保存在我的本地计算机 media/ 文件夹中,并且“docfile”设置为存储桶 url:
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
#DEFAULT_FILE_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage'
GS_BUCKET_NAME = 'printhub-files'
GS_PROJECT_ID = 'preasy-53c43'
GS_MEDIA_BUCKET_NAME = 'printhub-files'
# GS_STATIC_BUCKET_NAME = '<name-of-static-bucket>'
# STATIC_URL = 'https://storage.googleapis.com/{}/'.format(GS_STATIC_BUCKET_NAME)
MEDIA_URL = 'https://storage.googleapis.com/{}/'.format(GS_MEDIA_BUCKET_NAME)
我得到了输出:
{
"id": 13,
"docfile": "https://storage.googleapis.com/bucket/documents/2019/11/03/myfile.pdf",
}
【问题讨论】:
标签: django google-cloud-storage