5

我正在尝试使用 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 = '我的存储桶名称'

我已经安装了boto

sudo pip install boto

dyld:DYLD_ 环境变量被忽略,因为主可执行文件 (/usr/bin/sudo) 是 setuid 或 setgid

已满足要求(使用 --upgrade 升级):/Library/Python/2.7/site-packages/boto-2.9.0_dev-py2.7.egg 中的 boto

打扫干净...

当我保存项目时,django的调试页面跳出来,

无法加载 Boto 的 S3 绑定。

https://github.com/boto/boto

有任何想法吗?(我使用 mac os x 10.8.3)

4

1 回答 1

0

create a bash script: 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 

on Django's settings - the django default storage will be s3:

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

on your 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
于 2014-02-03T10:29:25.510 回答