Django uploads file to S3

I have this class that provides a POST endpoint to an API user using the Django REST framework.

The code should receive a file download, and then upload it to S3. The file loads correctly in the Django application ( 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 register a return file_obj.read() , it is also empty.

What's 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) 
+6
source share
2 answers

Is it possible that something is already reading file file, maybe your method of saving the load class, and you need to go back to the beginning?

 file_obj.seek(0) 
+7
source

You can use django repository

 pip install django-storages 

http://django-storages.readthedocs.org/en/latest/

In your model

 def upload_image_to(instance, filename): import os from django.utils.timezone import now filename_base, filename_ext = os.path.splitext(filename) return 'posts/%s/%s' % ( now().strftime("%Y%m%d"), instance.id ) image = models.ImageField(upload_to=upload_image_to, editable=True, null=True, blank=True) 

In your settings

 DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage' AWS_S3_SECURE_URLS = False # use http instead of https AWS_QUERYSTRING_AUTH = False # don't add complex authentication-related query parameters for requests AWS_S3_ACCESS_KEY_ID = 'KEY' # enter your access key id AWS_S3_SECRET_ACCESS_KEY = 'KEY' # enter your secret access key AWS_STORAGE_BUCKET_NAME = 'name.media' INSTALLED_APPS = ( ... 'storages', ) 
+2
source

Source: https://habr.com/ru/post/973344/


All Articles