【发布时间】:2020-04-02 21:36:54
【问题描述】:
是否可以使用 Google Drive 来保存来自 Django 应用程序的所有媒体文件(用户将上传的图像文件)?这个应用程序将部署在 Heroku(免费)中。我也知道 Amazon S3,但我想避免它,因为它在 12 个月后不是免费的。我注意到在 Heroku 中部署应用程序后,静态图像加载正常(使用白噪声),但使用 ImageField 从模型上传的图像没有加载。我对 Google API 甚至 Heroku 没有任何经验,我找不到太多关于这种集成是否可行的信息。如果是的话,如果我能获得更多关于如何去做的细节,那就太好了。我目前正在使用最新版本的 Django (V3.0) 以及 Python 3.7.5(64 位)。
编辑:
以下是我为尝试使用 Google Drive 通过 Heroku 托管媒体文件所做的更改。我使用了django-googledrive-storage,如下所示。但是当我设置 DEBUG = False 并运行代码时,我没有收到任何错误,但我没有看到任何文件被上传或从谷歌驱动器访问。
Settings.py
# Application definition
INSTALLED_APPS = [
# Default Apps
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# User Apps
'users.apps.UsersConfig',
'contacts.apps.ContactsConfig',
'crispy_forms', # For rendering forms
'gdstorage' # To host static and media files for Heroku
]
# Google drive storage settings
# I have added an environment variable with the same name and provided the full path
# of the JSON file where it is saved, like this - D:\secret_key.json
GOOGLE_DRIVE_STORAGE_JSON_KEY_FILE = os.environ.get('GOOGLE_DRIVE_STORAGE_JSON_KEY_FILE')
models.py
from gdstorage.storage import GoogleDriveStorage
# Define Google Drive Storage
gd_storage = GoogleDriveStorage()
class CustomUser(AbstractBaseUser):
.
.
image = models.ImageField(upload_to='users', default='default.png', blank=True, storage=gd_storage)
.
.
【问题讨论】:
标签: django heroku google-drive-api