Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confusing use of object_name in example for S3 upload using presigned URL #3986

Open
dennisvang opened this issue Jan 11, 2024 · 1 comment
Labels
documentation This is a problem with documentation. feature-request This issue requests a feature. p3 This is a minor priority issue

Comments

@dennisvang
Copy link

dennisvang commented Jan 11, 2024

Problem

Please consider the following relevant parts of the example from generating-a-presigned-url-to-upload-a-file:

# Generate a presigned S3 POST URL
object_name = 'OBJECT_NAME'
response = create_presigned_post('BUCKET_NAME', object_name)

As far as I know (see e.g. botocore source), this hardcodes the object name 'OBJECT_NAME' into the pre-signed post's policy string.

As a result, the filename that is supplied in the subsequent part of the example, i.e. the {'file': (object_name, f)} in the code below, is ignored. In other words, we could say {'file': ('something completely different', f)}, but the object in our bucket would still be named 'OBJECT_NAME'.

# Demonstrate how another Python program can use the presigned URL to upload a file
with open(object_name, 'rb') as f:
    files = {'file': (object_name, f)}
    http_response = requests.post(response['url'], data=response['fields'], files=files)

This is even more confusing if you consider the HTML form example that is provided at the bottom of the documentation page, which uses an <input type="file"> to select a file, but the object name is already fixed, so the name of the selected file will be ignored.

...
      <input type="hidden" name="policy" value="VALUE" />
      <input type="hidden" name="signature" value="VALUE" />
    File:
      <input type="file"   name="file" /> <br />
      <input type="submit" name="submit" value="Upload to Amazon S3" />
...

The result is a catch-22:

  1. we need an object_name in order to generate the pre-signed post
  2. the pre-signed post data is needed to build the HTML form
  3. the HTML form allows us to select a file
  4. the name of the selected file should (typically) be part of the object_name, but that was already fixed in step 1

Proposed solution

As can be seen in the botocore source, we can

... optionally add ${filename} to the end to attach the submitted filename. ...

Perhaps it would be better to modify the example along these lines:

Specify a variable filename by adding ${filename} to the object_name, as in

# Generate a presigned S3 POST URL
object_name = 'my/path/${filename}'
response = create_presigned_post('BUCKET_NAME', object_name)

then specify a separate file name, to make the distinction clear

# Demonstrate how another Python program can use the presigned URL to upload a file
filename = 'my_file'
with open(filename, 'rb') as f:
    files = {'file': (filename, f)}
    http_response = requests.post(response['url'], data=response['fields'], files=files)

The HTML code can then remain unaltered, as the filename from the file input will be used properly.

Links

https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-presigned-urls.html#generating-a-presigned-url-to-upload-a-file

@dennisvang dennisvang added documentation This is a problem with documentation. needs-triage This issue or PR still needs to be triaged. labels Jan 11, 2024
@tim-finnigan tim-finnigan self-assigned this May 7, 2024
@tim-finnigan tim-finnigan added the investigating This issue is being investigated and/or work is in progress to resolve the issue. label May 7, 2024
@tim-finnigan
Copy link
Contributor

Thanks for the suggestion, there may be an opportunity to improve the documentation here. We can leave this open to track for now and get input from others.

@tim-finnigan tim-finnigan removed their assignment May 7, 2024
@tim-finnigan tim-finnigan added feature-request This issue requests a feature. p3 This is a minor priority issue and removed investigating This issue is being investigated and/or work is in progress to resolve the issue. needs-triage This issue or PR still needs to be triaged. labels May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation This is a problem with documentation. feature-request This issue requests a feature. p3 This is a minor priority issue
Projects
None yet
Development

No branches or pull requests

2 participants