Fix:s3 Presign Put Request with File upload (#3235)

* Fix:s3 Presign Put Request with File upload

* Added imports in test

Co-authored-by: usmankb <usman@krazybee.com>
This commit is contained in:
usmangani1 2020-09-02 11:40:56 +05:30 committed by GitHub
parent 3ea46617d9
commit 00a5641cb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 1 deletions

View File

@ -1092,6 +1092,11 @@ class ResponseObject(_TemplateEnvironmentMixin, ActionAuthenticatorMixin):
else:
# Flask server
body = request.data
# when the data is being passed as a file
if request.files and not body:
for _, value in request.files.items():
body = value.stream.read()
if body is None:
body = b""

View File

@ -3,7 +3,7 @@ from __future__ import unicode_literals
import datetime
import sys
import os
from boto3 import Session
from six.moves.urllib.request import urlopen
from six.moves.urllib.error import HTTPError
@ -1054,6 +1054,29 @@ def test_streaming_upload_from_file_to_presigned_url():
assert response.status_code == 200
@mock_s3
def test_multipart_upload_from_file_to_presigned_url():
s3 = boto3.client("s3", region_name=DEFAULT_REGION_NAME)
s3.create_bucket(Bucket="mybucket")
params = {"Bucket": "mybucket", "Key": "file_upload"}
presigned_url = boto3.client("s3").generate_presigned_url(
"put_object", params, ExpiresIn=900
)
file = open("text.txt", "w")
file.write("test")
file.close()
files = {"upload_file": open("text.txt", "rb")}
requests.put(presigned_url, files=files)
resp = s3.get_object(Bucket="mybucket", Key="file_upload")
data = resp["Body"].read()
assert data == b"test"
# cleanup
os.remove("text.txt")
@mock_s3
def test_s3_object_in_private_bucket():
s3 = boto3.resource("s3")