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

查看:183
本文介绍了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

我检查<一href="http://stackoverflow.com/questions/10854095/boto-exception-s3responseerror-s3responseerror-403-forbidden">this回答但还是没有帮助

I checked this answer but still didn't help

推荐答案

你有没有安装博托作为Django的存储有一个依赖于博托?

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天全站免登陆