Skip to content

Commit b93623d

Browse files
author
haixinchen
committed
Fixing S3 Copy Object Go SDK v1/v2 example to encode url
awsdocs#740
1 parent ed619d0 commit b93623d

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

go/example_code/s3/s3_copy_object.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ import (
3232
"github.com/aws/aws-sdk-go/aws/session"
3333
"github.com/aws/aws-sdk-go/service/s3"
3434
"fmt"
35-
"os"
35+
"net/url"
36+
"os"
3637
)
3738

3839
// Copies the item in the bucket to another bucket.
@@ -60,7 +61,8 @@ func main() {
6061
svc := s3.New(sess)
6162

6263
// Copy the item
63-
_, err = svc.CopyObject(&s3.CopyObjectInput{Bucket: aws.String(other), CopySource: aws.String(source), Key: aws.String(item)})
64+
_, err = svc.CopyObject(&s3.CopyObjectInput{Bucket: aws.String(other),
65+
CopySource: aws.String(url.PathEscape(source)), Key: aws.String(item)})
6466
if err != nil {
6567
exitErrorf("Unable to copy item from bucket %q to bucket %q, %v", bucket, other, err)
6668
}

go/s3/CopyObject/CopyObject.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ package main
1818
import (
1919
"flag"
2020
"fmt"
21+
"net/url"
2122

22-
"github.com/aws/aws-sdk-go/aws"
23+
"github.com/aws/aws-sdk-go/aws"
2324
"github.com/aws/aws-sdk-go/aws/session"
2425
"github.com/aws/aws-sdk-go/service/s3"
2526
)
@@ -42,7 +43,7 @@ func CopyItem(sess *session.Session, sourceBucket *string, targetBucket *string,
4243
// Copy the item
4344
_, err := svc.CopyObject(&s3.CopyObjectInput{
4445
Bucket: targetBucket,
45-
CopySource: aws.String(source),
46+
CopySource: aws.String(url.PathEscape(source)),
4647
Key: item,
4748
})
4849
// snippet-end:[s3.go.copy_object.call]

gov2/s3/CopyObject/CopyObjectv2.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import (
77
"context"
88
"flag"
99
"fmt"
10+
"net/url"
1011

1112
"github.com/aws/aws-sdk-go-v2/config"
1213
"github.com/aws/aws-sdk-go-v2/service/s3"
14+
"github.com/aws/aws-sdk-go/aws"
1315
)
1416

1517
// S3CopyObjectAPI defines the interface for the Amazon Simple Storage Service (Amazon S3) CopyObject function.
@@ -53,7 +55,7 @@ func main() {
5355
client := s3.NewFromConfig(cfg)
5456

5557
input := &s3.CopyObjectInput{
56-
Bucket: sourceBucket,
58+
Bucket: aws.String(url.PathEscape(*sourceBucket)),
5759
CopySource: destinationBucket,
5860
Key: objectName,
5961
}

gov2/s3/CopyObject/CopyObjectv2_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"encoding/json"
88
"errors"
99
"io/ioutil"
10+
"net/url"
1011
"testing"
1112
"time"
1213

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

7980
// Build the request with its input parameters
8081
input := s3.CopyObjectInput{
81-
CopySource: &globalConfig.SourceBucket,
82+
CopySource: aws.String(url.PathEscape(globalConfig.SourceBucket)),
8283
Bucket: &globalConfig.DestinationBucket,
8384
Key: &globalConfig.ObjectKey,
8485
}

0 commit comments

Comments
 (0)