我有这个类使用Django REST框架将POST端点暴露给API使用者.
该代码应该接收文件上传,然后将其上传到S3.文件正确上传到Django应用程序(file_obj.length返回实际文件大小),并在S3中创建对象.但是,S3中的文件大小为零.如果我记录file_obj.read()的返回,它也是空的.
怎么了?
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)