awsdocs / aws-doc-sdk-examples

Welcome to the AWS Code Examples Repository. This repo contains code examples used in the AWS documentation, AWS SDK Developer Guides, and more. For more information, see the Readme.md file below.
Apache License 2.0
9.48k stars 5.61k forks source link

Gov2: Add presign post request support - S3 #6937

Closed Madrigal closed 1 day ago

Madrigal commented 1 day ago

This is Luis from the Go SDK team

We recently added support for s3 presign POST requests at aws/aws-sdk-go-v2#1224

We'd like to expand the existing examples to add presign post requests.

Let me know if there's anything I should change about the tags used to create the examples so they display correctly at https://docs.aws.amazon.com/AmazonS3/latest/userguide/example_s3_Scenario_PresignedUrl_section.html

This scenario has a bit more complex way of sending the request since multipart requests require more setup. However, this is mostly intended to be used by a browser sending a form rather than sending this directly from a Go server.

Tested this by

  1. Running go run ./cmd -scenario presigning and making sure it works as expected with a local file
 go run ./cmd -scenario presigning
----------------------------------------------------------------------------------------
Welcome to the Amazon S3 presigning demo.
----------------------------------------------------------------------------------------
We'll need a bucket. Enter a name for a bucket you own or one you want to create:
lmadrig-presign
Bucket lmadrig-presign exists and you already own it.
----------------------------------------------------------------------------------------
Let's presign a request to upload a file to your bucket.
Enter the path to a file you want to upload:
/Volumes/workplace/sample.txt
What would you like to name the uploaded object?
my-presign
Got a presigned PUT request to URL:
    https://lmadrig-presign.s3.us-west-2.amazonaws.com/my-presign?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=ASIAExample%2F20240927%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20240927T170511Z&X-Amz-Expires=60&X-Amz-Security-Token=XXXX&X-Amz-SignedHeaders=host&x-id=PutObject&X-Amz-Signature=YYYY
Using net/http to send the request...
PUT object my-presign with presigned URL returned 200.
----------------------------------------------------------------------------------------
Let's presign a request to download the object.
Press Enter when you're ready.

Got a presigned GET request to URL:
    https://lmadrig-presign.s3.us-west-2.amazonaws.com/my-presign?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=ASIAExample%2F20240927%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20240927T170513Z&X-Amz-Expires=60&X-Amz-Security-Token=XXXX&X-Amz-SignedHeaders=host&x-id=GetObject&X-Amz-Signature=YYYY
Using net/http to send the request...
GET object my-presign with presigned URL returned 200.
Downloaded 12 bytes. Here are the first 100 of them:
----------------------------------------------------------------------------------------
Hello world

----------------------------------------------------------------------------------------
Now we'll create a new request to put the same object using a presigned post request
Got a presigned post request to url https://lmadrig-presign.s3.us-west-2.amazonaws.com with values map[X-Amz-Algorithm:AWS4-HMAC-SHA256 X-Amz-Credential:ASIAExample/20240927/us-west-2/s3/aws4_request X-Amz-Date:20240927T170513Z X-Amz-Security-Token:XXXX key:my-presign policy:base64str]
Using net/http multipart to send the request...
Presign post object my-presign with presigned URL returned 204.
Let's presign a request to delete the object.
Press Enter when you're ready.

Got a presigned DELETE request to URL:
    https://lmadrig-presign.s3.us-west-2.amazonaws.com/my-presign?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=ASIAExample%2F20240927%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20240927T170516Z&X-Amz-Expires=900&X-Amz-Security-Token=XXXX&X-Amz-SignedHeaders=host&x-id=DeleteObject&X-Amz-Signature=YYYY
Using net/http to send the request...
DELETE object my-presign with presigned URL returned 204.
----------------------------------------------------------------------------------------
Thanks for watching!
----------------------------------------------------------------------------------------
  1. Running go test inside the s3 folder
go test ./...
?       github.com/awsdocs/aws-doc-sdk-examples/gov2/s3/cmd [no test files]
?       github.com/awsdocs/aws-doc-sdk-examples/gov2/s3/hello   [no test files]
?       github.com/awsdocs/aws-doc-sdk-examples/gov2/s3/stubs   [no test files]
ok      github.com/awsdocs/aws-doc-sdk-examples/gov2/s3/actions (cached)
ok      github.com/awsdocs/aws-doc-sdk-examples/gov2/s3/scenarios   (cached)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.