在 django 上使用 Amazon S3 通过 ImageField 上传图像

Dob*_*Dog 6 python django django-models amazon-s3 amazon-web-services

我用 pip 安装了 django-storages (pip install django-storages)

获取 AWS 首选项的设置:

DEFAULT_FILE_STORAGE = 'storages.backends.s3.S3Storage'
AWS_STORAGE_BUCKET_NAME = 'bar.media'
AWS_ACCESS_KEY_ID = 'MyAwesomeKeyId'
AWS_SECRET_ACCESS_KEY = 'BlahBlahBlah'
Run Code Online (Sandbox Code Playgroud)

我的模型是这样的:

def Bar(Model):
   image = models.ImageField(upload_to='bar')
Run Code Online (Sandbox Code Playgroud)

在forms.py上:

 class BarForm(forms.Form):
     image = forms.FileField(label='Logo',required=False)
     def __init__(self, *args, **kwargs):
            super(BarForm, self).__init__(*args,**kwargs)
            self.fields['image'].label='My Awesome Photo'
Run Code Online (Sandbox Code Playgroud)

在views.py上:

     @csrf_exempt
     def bar_web(request):
         context = {}
         if request.method == 'POST':
            form = BarForm(request.POST, request.FILES)
            context['form'] = form
            if form.is_valid():
                    try:
                            logoFile = request.FILES['image']
                            print 'PIC: '+str(logoFile)
                    except:
                            logoFile = False 
                    if logoFile:
                          bar = Bar(image=logoFile)
                          bar.save()
                          bar.reload()
Run Code Online (Sandbox Code Playgroud)

在模板上:

         <p>Image Name: {{bar.image.name}}</p>
         <p>Image Url: {{bar.image.url}}</p>
         <p>Image : {{bar.image.content_type}}</p>
         <p>Image Path: {{bar.image.path}}</p>
Run Code Online (Sandbox Code Playgroud)

但什么也没打印出来。

我目前在打印件上看到了文件名;但 s3 没有任何反应。没有上传文件。

我没看到的错误是什么?

Dob*_*Dog 6

我的问题是 IAM 配置错误;这些视频可能会对其他用户有所帮助...

https://www.youtube.com/watch?v=JuffWMBeJkw