Django媒体上传到AS3 [英] Django media uploads to AS3

查看:206
本文介绍了Django媒体上传到AS3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将所有Django媒体文件(从管理面板上传)上传到Amazon S3。所以设置文件看起来像这样:

I'm trying to upload all the Django media files (uploaded from the admin panel) to Amazon S3. So settings file look something like this :

INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'tastypie',
'core',
'advertisment',
'storages',

)

  DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
  AWS_ACCESS_KEY_ID = 'xxx' 
  AWS_SECRET_ACCESS_KEY = 'xxx'
  AWS_STORAGE_BUCKET_NAME = 'media'
  MEDIA_URL = 'https://s3.amazonaws.com/%s/' % AWS_STORAGE_BUCKET_NAME

,当我尝试从管理面板上传文件时,我收到以下错误:

and when I try to upload a file from the admin panel, i get the following error:

S3ResponseError: 403 Forbidden

我检查了这个答案,但仍然没有帮助

I checked this answer but still didn't help

推荐答案

安装了 boto ,因为django-storage对boto有依赖性。

Have you installed boto, as django-storage has a dependency on boto?

然后您将不得不将以下内容添加到settings.py

Then you will have to add the following into your settings.py

import os

AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = os.environ.get('S3_BUCKET_NAME')

STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'

STATIC_URL = 'http://' + AWS_STORAGE_BUCKET_NAME + '.s3.amazonaws.com/'
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'

然后您的缓存将存储在AWS S3本身。

Then your cache will be stored on AWS S3 itself.

我希望它有帮助

注意:出于安全考虑,最好将AWS_ACCESS_KEY_ID和AWS_SECRET_ACCESS_KEY添加为环境变量,而不是直接在setting.py中写入它们。

Note: for security reasons its a good idea to add your AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY as environment variables instead of just writing them down in setting.py directly.

这篇关于Django媒体上传到AS3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆