aws / aws-sdk-go-v2

AWS SDK for the Go programming language.
https://aws.github.io/aws-sdk-go-v2/docs/
Apache License 2.0
2.68k stars 651 forks source link

Add a `PresignPostObject` to the `PresignClient` for `s3.PutObject` operation #2758

Closed Madrigal closed 3 months ago

Madrigal commented 3 months ago

For changes to files under the /codegen/aws-models folder, and manual edits to autogenerated code (e.g. /service/s3/api.go) please create an Issue instead of a PR for those type of changes.

If the PR addresses an existing bug or feature, please reference it here.

To help speed up the process and reduce the time to merge please ensure that Allow edits by maintainers is checked before submitting your PR. This will allow the project maintainers to make minor adjustments or improvements to the submitted PR, allow us to reduce the roundtrip time for merging your request.

This is used as follows

// conditions that you want your request to have
// see https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-HTTPPOSTConstructPolicy.html#sigv4-PolicyConditions
conditions := []interface{}{
    []interface{}{
        "content-length-range",
        1,
        10485760,
    },
}
// Duration on which you want this request to
expiresIn = 20 * time.Minute
req, err := presignClient.PresignPostObject(examplePresigner.ctx, &s3.PutObjectInput{
    Bucket: aws.String(bucketName),
    Key:    aws.String(uploadKey),
}, func(opts *s3.PresignPostOptions) {
    opts.Expires = expiresIn
    opts.Conditions = conditions
})
if err != nil {
    log.Printf("Couldn't get a presigned request to put %v:%v. Here's why: %v\n",
        bucketName, uploadKey, err)
}
fmt.Println(req.URL) 
// https://test-bucket.s3.us-west-2.amazonaws.com
fmt.Println(req.Values)
// map[X-Amz-Algorithm:AWS4-HMAC-SHA256
// X-Amz-Credential:ASIAXXXX/20240822/us-west-2/s3/aws4_request
// X-Amz-Date:20240822T215442Z
// X-Amz-Signature:d7c4f16a9a0b8d60a7ed4f01234b0f9c2e58f12d0d3a847f91f1b3e7d6f8192a
// key:my_file.json
// policy:base64encodedValue]

fixes #1224