[英]TypeError uploading image file to Amazon S3 in Django using BOTO Library
I am a total beginner to programming and Django so I'd appreciate help that beginner can get his head round! 我是编程和Django的初学者,所以我很感激帮助初学者可以得到他的头脑!
I was following a tutorial to show how to upload images to an Amazon S3 account with the Boto library but I think it is for an older version of Django (I'm on 1.1.2 and Python 2.65) and something has changed. 我正在按照一个教程来展示如何使用Boto库将图像上传到Amazon S3帐户,但我认为它适用于较旧版本的Django(我在1.1.2和Python 2.65)并且有些变化。 I get an error: Exception Type: TypeError Exception Value: 'InMemoryUploadedFile' object is unsubscriptable
我收到一个错误:异常类型:TypeError异常值:'InMemoryUploadedFile'对象是unsubscriptable
My code is: 我的代码是:
Models.py: Models.py:
from django.db import models
from django.contrib.auth.models import User
from django import forms
from datetime import datetime
class PhotoUrl(models.Model):
url = models.CharField(max_length=128)
uploaded = models.DateTimeField()
def save(self):
self.uploaded = datetime.now()
models.Model.save(self)
views.py: views.py:
import mimetypes
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.core.urlresolvers import reverse
from django import forms
from django.conf import settings
from boto.s3.connection import S3Connection
from boto.s3.key import Key
def awsdemo(request):
def store_in_s3(filename, content):
conn = S3Connection(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY)
b = conn.create_bucket('almacmillan-hark')
mime = mimetypes.guess_type(filename)[0]
k = Key(b)
k.key = filename
k.set_metadata("Content-Type", mime)
k.set_contents_from_strong(content)
k.set_acl("public-read")
photos = PhotoUrl.objects.all().order_by("-uploaded")
if not request.method == "POST":
f = UploadForm()
return render_to_response('awsdemo.html', {'form':f, 'photos':photos})
f = UploadForm(request.POST, request.FILES)
if not f.is_valid():
return render_to_response('awsdemo.html', {'form':f, 'photos':photos})
file = request.FILES['file']
filename = file.name
content = file['content']
store_in_s3(filename, content)
p = PhotoUrl(url="http://almacmillan-hark.s3.amazonaws.com/" + filename)
p.save()
photos = PhotoUrl.objects.all().order_by("-uploaded")
return render_to_response('awsdemo.html', {'form':f, 'photos':photos})
urls.py: urls.py:
(r'^awsdemo/$', 'harkproject.s3app.views.awsdemo'),
awsdemo.html: awsdemo.html:
<div class="form">
<strong>{{form.file.label}}</strong>
<form method="POST" action ="." enctype="multipart/form-data">
{{form.file}}<br/>
<input type="submit" value="Upload">
</form>
</div>
I'd really appreciate help. 我真的很感激帮助。 I hope I have provided enough code.
我希望我提供了足够的代码。
Kind regards AL 亲切的问候AL
I think your problem is this line: 我认为你的问题是这一行:
content = file['content']
From the Django docs : 来自Django文档 :
Each value in FILES is an UploadedFile object containing the following attributes:
FILES中的每个值都是一个UploadedFile对象,包含以下属性:
- read(num_bytes=None) -- Read a number of bytes from the file.
read(num_bytes = None) - 从文件中读取一些字节。
- name -- The name of the uploaded file.
name - 上载文件的名称。
- size -- The size, in bytes, of the uploaded file.
size - 上载文件的大小(以字节为单位)。
- chunks(chunk_size=None) -- A generator that yields sequential chunks of data.
chunk(chunk_size = None) - 生成顺序数据块的生成器。
Try this instead: 试试这个:
content = file.read()
Have you tried Django Storages ? 你试过Django存储吗? That way you only have to specify a storage backend (s3boto in this case) either as the default storage backend, or supply it as an argument to a FileField or ImageField class.
这样,您只需将存储后端(在本例中为s3boto)指定为默认存储后端,或将其作为参数提供给FileField或ImageField类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.