我正在尝试将文件上传到S3(实际上是上传工作),但是我的应用程序崩溃并引发了错误:
fp is at EOF. Use rewind option or seek() to data start.
我看到人们解决了一些方法是将rewind=True
添加到他们的set_contents_from_string调用中。但是,这引发了以下错误:
set_contents_from_string() got an unexpected keyword argument 'rewind'
以下是我的views.py
的内容。我在做什么错?
def store_in_s3(filename, filecontent):
conn = S3Connection(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY)
b = conn.create_bucket('mybucket')
mime = mimetypes.guess_type(filename)[0]
k = Key(b)
k.key = filename
k.set_metadata("Content-Type", mime)
k.set_contents_from_string(filecontent)
#k.set_contents_from_string(filecontent, rewind=True)
k.set_acl("public-read")
def add_m(request, points=None):
mname = request.GET.get ('mname')
format = request.GET.get ('format')
type = request.GET.get ('type')
if request.method == "POST":
formtoaddm = spiceform(request.POST, request.FILES)
if formtoaddm.is_valid():
new_m = formtoaddm.save(commit=False)
new_m.adder = request.user
mname = new_m.mname
file = request.FILES['content']
filename = file.name
filecontent = file.read()
store_in_s3(filename, filecontent)
...
之后
filecontent = file.read()
put:
file.seek(0)
请参阅此处的最终评论。假设您正在使用Django-storage,只是升级到最新版本,应该解决此问题。