我正在尝试使用s3来存储用户的上传文件,我使用django存储
pip install django-storages
将其添加到我的INSTALLED_APPS
INSTALLED_APPS =(
...
'储存器',
)
在settings.py中设置变量
DEFAULT_FILE_STORAGE ='storages.backends.s3boto.S3BotoStorage'
AWS_ACCESS_KEY_ID ='#################'
AWS_SECRET_ACCESS_KEY ='#######################'
AWS_STORAGE_BUCKET_NAME ='mybucketname'
我已经安装了boto
sudo pip install boto
dyld:DYLD_环境变量被忽略,因为主可执行文件(/ usr / bin / sudo)是setuid或setgid
要求已经满足(使用--upgrade升级):boto in /Library/Python/2.7/site-packages/boto-2.9.0_dev-py2.7.egg
清理......
当我保存项目时,django的调试页面会跳出来,
无法加载Boto的S3绑定。
有什么想法吗? (我使用mac os x 10.8.3)
答案 0 :(得分:0)
创建一个bash脚本:install_latest_boto.sh:
#install latest boto from source
cd /home/ubuntu/
sudo mkdir boto_temp
cd boto_temp
sudo git clone git://github.com/boto/boto.git
cd boto
sudo python setup.py install
关于Django的设置 - django默认存储将是s3:
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
AWS_S3_FILE_OVERWRITE = False
在models.py上:
class MyBaseModel(models.Model):
class Meta:
abstract = True
@staticmethod
def get_upload_path(instance, filename):
if hasattr(instance, 'get_upload_folder'):
return os.path.join(instance.get_upload_folder(), filename)
else:
raise Exception('Upload Folder is missing')
class User(MyBaseModel):
name = models.CharField(max_length=100)
email = models.EmailField(max_length=255, unique=True)
image = models.ImageField(upload_to=MyBaseModel.get_upload_path, default=None, blank=True, max_length=200)
def get_upload_folder(self):
upload_folder = 'users/images/orig'
return upload_folder