Skip to content

Commit b24001e

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

File tree

4 files changed

+10
-4
lines changed

4 files changed

+10
-4
lines changed

go/example_code/s3/s3_copy_object.go

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

@@ -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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package main
1818
import (
1919
"flag"
2020
"fmt"
21+
"net/url"
2122

2223
"github.com/aws/aws-sdk-go/aws"
2324
"github.com/aws/aws-sdk-go/aws/session"
@@ -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,7 +7,9 @@ import (
77
"context"
88
"flag"
99
"fmt"
10+
"net/url"
1011

12+
"github.com/aws/aws-sdk-go-v2/aws"
1113
"github.com/aws/aws-sdk-go-v2/config"
1214
"github.com/aws/aws-sdk-go-v2/service/s3"
1315
)
@@ -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)