forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request awsdocs#1556 from awsdocs/doug-s3-presign-tag
Added new Go v2 code example to get pre-signed URL for S3 object
- Loading branch information
Showing
8 changed files
with
256 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX - License - Identifier: Apache - 2.0 | ||
// snippet-start:[s3.go-v2.generate_presigned_url] | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
|
||
v4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4" | ||
"github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/aws/aws-sdk-go-v2/service/s3" | ||
) | ||
|
||
// S3PresignGetObjectAPI defines the interface for the PresignGetObject function. | ||
// We use this interface to test the function using a mocked service. | ||
type S3PresignGetObjectAPI interface { | ||
PresignGetObject( | ||
ctx context.Context, | ||
params *s3.GetObjectInput, | ||
optFns ...func(*s3.PresignOptions)) (*v4.PresignedHTTPRequest, error) | ||
} | ||
|
||
// GetPresignedURL retrieves a presigned URL for an Amazon S3 bucket object. | ||
// Inputs: | ||
// c is the context of the method call, which includes the AWS Region. | ||
// api is the interface that defines the method call. | ||
// input defines the input arguments to the service call. | ||
// Output: | ||
// If successful, the presigned URL for the object and nil. | ||
// Otherwise, nil and an error from the call to PresignGetObject. | ||
func GetPresignedURL(c context.Context, api S3PresignGetObjectAPI, input *s3.GetObjectInput) (*v4.PresignedHTTPRequest, error) { | ||
resp, err := api.PresignGetObject(c, input) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return resp, nil | ||
} | ||
|
||
func main() { | ||
bucket := flag.String("b", "", "The bucket") | ||
key := flag.String("k", "", "The object key") | ||
|
||
flag.Parse() | ||
|
||
if *bucket == "" || *key == "" { | ||
fmt.Println("You must supply a bucket name (-b BUCKET) and object key (-k KEY)") | ||
return | ||
} | ||
|
||
cfg, err := config.LoadDefaultConfig(context.TODO()) | ||
if err != nil { | ||
panic("configuration error, " + err.Error()) | ||
} | ||
|
||
client := s3.NewFromConfig(cfg) | ||
|
||
input := &s3.GetObjectInput{ | ||
Bucket: bucket, | ||
Key: key, | ||
} | ||
|
||
psClient := s3.NewPresignClient(client) | ||
|
||
resp, err := GetPresignedURL(context.Background(), psClient, input) | ||
if err != nil { | ||
fmt.Println("Got an error retrieving pre-signed object:") | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
fmt.Println("The URL: ") | ||
fmt.Println(resp.URL) | ||
} | ||
|
||
// snippet-end:[s3.go-v2.generate_presigned_url] |
107 changes: 107 additions & 0 deletions
107
gov2/s3/GeneratePresignedURL/GeneratePresignedURLv2_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"io/ioutil" | ||
"testing" | ||
"time" | ||
|
||
v4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4" | ||
"github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/aws/aws-sdk-go-v2/service/s3" | ||
) | ||
|
||
type S3PresignGetObjectImpl struct{} | ||
|
||
func (dt S3PresignGetObjectImpl) PresignGetObject( | ||
ctx context.Context, | ||
params *s3.GetObjectInput, | ||
optFns ...func(*s3.PresignOptions)) (*v4.PresignedHTTPRequest, error) { | ||
|
||
// Get the region: | ||
cfg, err := config.LoadDefaultConfig(context.TODO()) | ||
if err != nil { | ||
panic("configuration error, " + err.Error()) | ||
} | ||
|
||
region := cfg.Region | ||
|
||
string64 := "1234567890123456789012345678901234567890123456789012345678901234" | ||
|
||
url := "https://" + | ||
*params.Bucket + | ||
".s3." + region + | ||
".amazonaws.com/" + | ||
*params.Key + | ||
"?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=CREDENTIALS" + | ||
region + | ||
"%2Fs3%2Faws4_request&X-Amz-Date=20210104T220556Z&X-Amz-Expires=900&X-Amz-SignedHeaders=host&x-id=GetObject&X-Amz-Signature=" + string64 | ||
|
||
output := &v4.PresignedHTTPRequest{ | ||
URL: url, | ||
} | ||
|
||
return output, nil | ||
} | ||
|
||
type Config struct { | ||
Bucket string `json:"Bucket"` | ||
Key string `json:"Key"` | ||
} | ||
|
||
var configFileName = "config.json" | ||
|
||
var globalConfig Config | ||
|
||
func populateConfiguration() error { | ||
content, err := ioutil.ReadFile(configFileName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
text := string(content) | ||
|
||
err = json.Unmarshal([]byte(text), &globalConfig) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if globalConfig.Bucket == "" || globalConfig.Key == "" { | ||
msg := "You must supply a value for Bucket and Key in " + configFileName | ||
return errors.New(msg) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func TestPresignGetObject(t *testing.T) { | ||
thisTime := time.Now() | ||
nowString := thisTime.Format("2006-01-02 15:04:05 Monday") | ||
t.Log("Starting unit test at " + nowString) | ||
|
||
err := populateConfiguration() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
api := &S3PresignGetObjectImpl{} | ||
|
||
input := &s3.GetObjectInput{ | ||
Bucket: &globalConfig.Bucket, | ||
Key: &globalConfig.Key, | ||
} | ||
|
||
resp, err := GetPresignedURL(context.Background(), *api, input) | ||
if err != nil { | ||
t.Log("Got an error ...:") | ||
t.Log(err) | ||
return | ||
} | ||
|
||
t.Log("URL:") | ||
t.Log(resp.URL) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
### GeneratePresignedURLv2.go | ||
|
||
This example retrieves a presigned URL for an Amazon Simple Storage Service (Amazon S3) bucket object. | ||
|
||
`go run GeneratePresignedURLv2.go -b BUCKET -k KEY` | ||
|
||
- _BUCKET_ is the name of the bucket. | ||
- _KEY_ is the name of the object (key). | ||
|
||
The unit test accepts a similar value in _config.json_. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"Bucket": "aws-docs-example-bucket", | ||
"Key": "aws-docs-example-key" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module mymain | ||
|
||
go 1.15 | ||
|
||
require ( | ||
github.com/aws/aws-sdk-go-v2 v0.31.0 | ||
github.com/aws/aws-sdk-go-v2/config v0.4.0 | ||
github.com/aws/aws-sdk-go-v2/service/s3 v0.31.0 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
github.com/aws/aws-sdk-go-v2 v0.31.0 h1:TNTDsz+Xq80nYzZPUFS4a2Oyjz9jKHKcTuNAXtcW8b8= | ||
github.com/aws/aws-sdk-go-v2 v0.31.0/go.mod h1:IQw4KL7QIoaNDT3WoEBV1fDlVRhp/WTRteoaplV3SHo= | ||
github.com/aws/aws-sdk-go-v2/config v0.4.0 h1:16lwnZRhleaPbDesZgEJbHxuOv4wy12A372mkhmiktc= | ||
github.com/aws/aws-sdk-go-v2/config v0.4.0/go.mod h1:5uxQPUBCF+TwwWYo2xau4N+rSOS47ZH+QvLbae1Cckc= | ||
github.com/aws/aws-sdk-go-v2/credentials v0.2.0 h1:YDv/0/8BzaZtpS4jfptcyIPh5zlhmIhbM2RtNscn/bo= | ||
github.com/aws/aws-sdk-go-v2/credentials v0.2.0/go.mod h1:U81m6Xb5IpJ66ZnotiG7/6JJFuwrc8q8rWpXQxYP0hI= | ||
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v0.1.0 h1:zwhJDxNht/+a0QGy3RCveUFf6REXcmaQIHcYS11m5KY= | ||
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v0.1.0/go.mod h1:d3o/QBgbYw2OYmbv/EGYs0zFH47qsCKCTDbaOgdQGH8= | ||
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v0.4.0 h1:osjGuGbk/aVW8u2yhOijPhxLU1ardwPDsga5HHyQMbw= | ||
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v0.4.0/go.mod h1:Pjv1Z+nRaluwWCMuB6OQeqGRyiGk2WTrKG8RHs5wZug= | ||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v0.2.0 h1:xeqwfWQGg3hjLMs61PlixdxhU+qqrd7S50j/3kV1j/0= | ||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v0.2.0/go.mod h1:Ef71w/O9Ulhxj8gD9Pq2S0lXMvyNzFLMRuIxWpDRQxk= | ||
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v0.4.0 h1:OiRO8lneLP7wJ80dqAwHpIRcMInUO04HzcQUVtPwk3U= | ||
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v0.4.0/go.mod h1:fMsE4Rr6OkxQUmZxi4Amj8LS0BakLZw05Rog0jYStQM= | ||
github.com/aws/aws-sdk-go-v2/service/s3 v0.31.0 h1:KDBpodyszhL8F83QYv36qBzDj99oPlt1coZNGSFKTXU= | ||
github.com/aws/aws-sdk-go-v2/service/s3 v0.31.0/go.mod h1:KnvsmhsdHaJJZatMeNscBKaEmz0V5oXe1N5ZaLmGDBs= | ||
github.com/aws/aws-sdk-go-v2/service/sts v0.31.0 h1:iJwlIyswoW4VM8RUmhC3397jdGa6QhMUtUf5daX+/a0= | ||
github.com/aws/aws-sdk-go-v2/service/sts v0.31.0/go.mod h1:gliVu4/DZsKINvBoEcMIlxMIQft/yPYQhnSLxwiWqFM= | ||
github.com/aws/smithy-go v0.5.0 h1:ArsdWUrb1n6/V/REXhuwq2TZv+kuqOBpMlGBd2EkDYM= | ||
github.com/aws/smithy-go v0.5.0/go.mod h1:EzMw8dbp/YJL4A5/sbhGddag+NPT7q084agLbB9LgIw= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= | ||
github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M= | ||
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= | ||
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= | ||
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters