I have this class that exposes a POST endpoint to an API consumer using Django REST framework.
The code is supposed to receive a file upload, and then upload it to S3. The file is uploaded correctly to the Django app (file_obj.length
returns the actual file size), and the object is created in S3. However, the file size in S3 is zero. If I log the return of file_obj.read()
it is empty as well.
What is wrong?
from django.conf import settings
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.parsers import FileUploadParser
from boto.s3.connection import S3Connection
from boto.s3.key import Key
from .models import Upload
from .serializers import UploadSerializer
class UploadList(APIView):
parser_classes = (FileUploadParser,)
def post(self, request, format=None):
file_obj = request.FILES['file']
upload = Upload(user=request.user, file=file_obj)
upload.save()
conn = S3Connection(settings.AWS_ACCESS_KEY, settings.AWS_SECRET_KEY)
k = Key(conn.get_bucket(settings.AWS_S3_BUCKET))
k.key = 'upls/%s/%s.png' % (request.user.id, upload.key)
k.set_contents_from_string(file_obj.read())
serializer = UploadSerializer(upload)
return Response(serializer.data, status=201)
Is it possible that something is reading the file object already, perhaps your Upload class save method, and you need to seek back to the beginning?
file_obj.seek(0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With