2010-08-05 7 views
6

Ich bin ein absoluter Anfänger zu programmieren und Django, also würde ich die Hilfe schätzen, dass Anfänger seinen Kopf bekommen können!TypeError Bilddatei auf Amazon S3 in Django mit BOTO-Bibliothek hochladen

Ich folgte einem Tutorial, um zu zeigen, wie Bilder zu einem Amazon S3-Konto mit der Boto-Bibliothek hochladen, aber ich denke, es ist für eine ältere Version von Django (ich bin auf 1.1.2 und Python 2.65) und etwas hat geändert. Ich erhalte eine Fehlermeldung: Ausnahmetyp: Typeerror Ausnahmewert: 'InMemoryUploadedFile' Objekt unsubscriptable ist

Mein Code ist:

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:

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:

(r'^awsdemo/$', 'harkproject.s3app.views.awsdemo'), 

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> 

Ich würde wirklich Hilfe zu schätzen wissen. Ich hoffe, ich habe genug Code zur Verfügung gestellt.

Kind regards AL

+2

Eine Sache, die ich sofort sehe: 'k.set_contents_from_strong (content)' sollte wirklich 'set_contents_from_string' sein. –

Antwort

7

Ich denke, Ihr Problem ist diese Linie:

content = file['content'] 

Vom Django docs:

Each value in FILES is an UploadedFile object containing the following attributes:

  • read(num_bytes=None) -- Read a number of bytes from the file.
  • name -- The name of the uploaded file.
  • size -- The size, in bytes, of the uploaded file.
  • chunks(chunk_size=None) -- A generator that yields sequential chunks of data.

dieses Versuchen Sie stattdessen:

content = file.read() 
0

Haben Sie versucht Django Storages? Auf diese Weise müssen Sie nur ein Speicher-Backend (s3boto in diesem Fall) entweder als Standardspeicher-Backend angeben oder es als Argument für eine FileField- oder ImageField-Klasse bereitstellen.

Verwandte Themen