Skip to content

Commit

Permalink
Fixing S3 Copy Object Go SDK v1/v2 example to encode url
Browse files Browse the repository at this point in the history
  • Loading branch information
haixinchen committed Nov 30, 2020
1 parent ed619d0 commit b24001e
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
4 changes: 3 additions & 1 deletion go/example_code/s3/s3_copy_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"fmt"
"net/url"
"os"
)

Expand Down Expand Up @@ -60,7 +61,8 @@ func main() {
svc := s3.New(sess)

// Copy the item
_, err = svc.CopyObject(&s3.CopyObjectInput{Bucket: aws.String(other), CopySource: aws.String(source), Key: aws.String(item)})
_, err = svc.CopyObject(&s3.CopyObjectInput{Bucket: aws.String(other),
CopySource: aws.String(url.PathEscape(source)), Key: aws.String(item)})
if err != nil {
exitErrorf("Unable to copy item from bucket %q to bucket %q, %v", bucket, other, err)
}
Expand Down
3 changes: 2 additions & 1 deletion go/s3/CopyObject/CopyObject.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main
import (
"flag"
"fmt"
"net/url"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
Expand All @@ -42,7 +43,7 @@ func CopyItem(sess *session.Session, sourceBucket *string, targetBucket *string,
// Copy the item
_, err := svc.CopyObject(&s3.CopyObjectInput{
Bucket: targetBucket,
CopySource: aws.String(source),
CopySource: aws.String(url.PathEscape(source)),
Key: item,
})
// snippet-end:[s3.go.copy_object.call]
Expand Down
4 changes: 3 additions & 1 deletion gov2/s3/CopyObject/CopyObjectv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"context"
"flag"
"fmt"
"net/url"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
Expand Down Expand Up @@ -53,7 +55,7 @@ func main() {
client := s3.NewFromConfig(cfg)

input := &s3.CopyObjectInput{
Bucket: sourceBucket,
Bucket: aws.String(url.PathEscape(*sourceBucket)),
CopySource: destinationBucket,
Key: objectName,
}
Expand Down
3 changes: 2 additions & 1 deletion gov2/s3/CopyObject/CopyObjectv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/json"
"errors"
"io/ioutil"
"net/url"
"testing"
"time"

Expand Down Expand Up @@ -78,7 +79,7 @@ func TestCopyObject(t *testing.T) {

// Build the request with its input parameters
input := s3.CopyObjectInput{
CopySource: &globalConfig.SourceBucket,
CopySource: aws.String(url.PathEscape(globalConfig.SourceBucket)),
Bucket: &globalConfig.DestinationBucket,
Key: &globalConfig.ObjectKey,
}
Expand Down

0 comments on commit b24001e

Please sign in to comment.