Can you upload to S3 using a stream rather than a local file?

前端 未结 5 1496
迷失自我
迷失自我 2020-11-29 23:17

I need to create a CSV and upload it to an S3 bucket. Since I\'m creating the file on the fly, it would be better if I could write it directly to S3 bucket as it is being cr

5条回答
  •  渐次进展
    2020-11-29 23:41

    We were trying to upload file contents to s3 when it came through as an InMemoryUploadedFile object in a Django request. We ended up doing the following because we didn't want to save the file locally. Hope it helps:

    @action(detail=False, methods=['post'])
    def upload_document(self, request):
         document = request.data.get('image').file
         s3.upload_fileobj(document, BUCKET_NAME, 
                                     DESIRED_NAME_OF_FILE_IN_S3, 
                                     ExtraArgs={"ServerSideEncryption": "aws:kms"})
    

提交回复
热议问题