Content-Disposition in file body from s3 presigned URL

1.4k Views Asked by At

I'm attempting to use presigned S3 URLs to PUT/GET a file via requests but an getting additional information in the uploaded file:

This is another Test Document!

The uploaded file will look like:

--424232468bf0c117eea367b5410bbcd7
Content-Disposition: form-data; name="file"; filename="my_test3.txt"

This is another Test Document!
--424232468bf0c117eea367b5410bbcd7--

Is there any reason the hash and Content-Disposition would be appearing in the uploaded file?

params = {
    'Bucket': bucket_name,
    'ContentType': content_type,
    'Key': object_name
}
if encode:
    params['ContentEncoding'] = encode

# do_client is a boto3.session.Session(...)
return do_client().generate_presigned_url(
    ClientMethod='put_object',
    Params=params,
    ExpiresIn=600
)

Code to put the file onto the server:

headers = {
    'Content-Type': content_type,
}
if content_encoding:
    headers['Content-Encoding'] = content_encoding

files = {'file': f} # f is an open() file handle

r = requests.put(
    put_url,
    headers=headers,
    files=files
)

Perhaps I misunderstand something about the object uploading process? I've tested without using the presigned URLs (via the storages lib for django) and everything uploaded as expected.

1

There are 1 best solutions below

0
On

I've managed to get passed the issue by changing the PUT arguments to:

r = requests.put(
    put_url,
    headers=headers,
    data=f.read()    #< Contains the entire file body...
)

This isn't ideal for large files since the entire thing is placed onto memory and subsequently onto the request so might have to switch to multi-part uploads. Not ideal for simplicity but not terrible.