func ExampleS3_PutBucketTagging() { svc := s3.New(nil) params := &s3.PutBucketTaggingInput{ Bucket: aws.String("BucketName"), // Required Tagging: &s3.Tagging{ // Required TagSet: []*s3.Tag{ // Required { // Required Key: aws.String("ObjectKey"), // Required Value: aws.String("Value"), // Required }, // More values... }, }, } resp, err := svc.PutBucketTagging(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
func dlLoggingSvc(data []byte) (*s3.S3, *[]string, *[]string) { var m sync.Mutex names := []string{} ranges := []string{} svc := s3.New(nil) svc.Handlers.Send.Clear() svc.Handlers.Send.PushBack(func(r *request.Request) { m.Lock() defer m.Unlock() names = append(names, r.Operation.Name) ranges = append(ranges, *r.Params.(*s3.GetObjectInput).Range) rerng := regexp.MustCompile(`bytes=(\d+)-(\d+)`) rng := rerng.FindStringSubmatch(r.HTTPRequest.Header.Get("Range")) start, _ := strconv.ParseInt(rng[1], 10, 64) fin, _ := strconv.ParseInt(rng[2], 10, 64) fin++ if fin > int64(len(data)) { fin = int64(len(data)) } r.HTTPResponse = &http.Response{ StatusCode: 200, Body: ioutil.NopCloser(bytes.NewReader(data[start:fin])), Header: http.Header{}, } r.HTTPResponse.Header.Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", start, fin, len(data))) }) return svc, &names, &ranges }
func TestPresignHandler(t *testing.T) { svc := s3.New(nil) req, _ := svc.PutObjectRequest(&s3.PutObjectInput{ Bucket: aws.String("bucket"), Key: aws.String("key"), ContentDisposition: aws.String("a+b c$d"), ACL: aws.String("public-read"), }) req.Time = time.Unix(0, 0) urlstr, err := req.Presign(5 * time.Minute) assert.NoError(t, err) expectedDate := "19700101T000000Z" expectedHeaders := "host;x-amz-acl" expectedSig := "7edcb4e3a1bf12f4989018d75acbe3a7f03df24bd6f3112602d59fc551f0e4e2" expectedCred := "AKID/19700101/mock-region/s3/aws4_request" u, _ := url.Parse(urlstr) urlQ := u.Query() assert.Equal(t, expectedSig, urlQ.Get("X-Amz-Signature")) assert.Equal(t, expectedCred, urlQ.Get("X-Amz-Credential")) assert.Equal(t, expectedHeaders, urlQ.Get("X-Amz-SignedHeaders")) assert.Equal(t, expectedDate, urlQ.Get("X-Amz-Date")) assert.Equal(t, "300", urlQ.Get("X-Amz-Expires")) assert.NotContains(t, urlstr, "+") // + encoded as %20 }
func ExampleS3_UploadPartCopy() { svc := s3.New(nil) params := &s3.UploadPartCopyInput{ Bucket: aws.String("BucketName"), // Required CopySource: aws.String("CopySource"), // Required Key: aws.String("ObjectKey"), // Required PartNumber: aws.Int64(1), // Required UploadId: aws.String("MultipartUploadId"), // Required CopySourceIfMatch: aws.String("CopySourceIfMatch"), CopySourceIfModifiedSince: aws.Time(time.Now()), CopySourceIfNoneMatch: aws.String("CopySourceIfNoneMatch"), CopySourceIfUnmodifiedSince: aws.Time(time.Now()), CopySourceRange: aws.String("CopySourceRange"), CopySourceSSECustomerAlgorithm: aws.String("CopySourceSSECustomerAlgorithm"), CopySourceSSECustomerKey: aws.String("CopySourceSSECustomerKey"), CopySourceSSECustomerKeyMD5: aws.String("CopySourceSSECustomerKeyMD5"), RequestPayer: aws.String("RequestPayer"), SSECustomerAlgorithm: aws.String("SSECustomerAlgorithm"), SSECustomerKey: aws.String("SSECustomerKey"), SSECustomerKeyMD5: aws.String("SSECustomerKeyMD5"), } resp, err := svc.UploadPartCopy(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
func ExampleS3_HeadObject() { svc := s3.New(nil) params := &s3.HeadObjectInput{ Bucket: aws.String("BucketName"), // Required Key: aws.String("ObjectKey"), // Required IfMatch: aws.String("IfMatch"), IfModifiedSince: aws.Time(time.Now()), IfNoneMatch: aws.String("IfNoneMatch"), IfUnmodifiedSince: aws.Time(time.Now()), Range: aws.String("Range"), RequestPayer: aws.String("RequestPayer"), SSECustomerAlgorithm: aws.String("SSECustomerAlgorithm"), SSECustomerKey: aws.String("SSECustomerKey"), SSECustomerKeyMD5: aws.String("SSECustomerKeyMD5"), VersionId: aws.String("ObjectVersionId"), } resp, err := svc.HeadObject(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
func ExampleS3_CompleteMultipartUpload() { svc := s3.New(nil) params := &s3.CompleteMultipartUploadInput{ Bucket: aws.String("BucketName"), // Required Key: aws.String("ObjectKey"), // Required UploadId: aws.String("MultipartUploadId"), // Required MultipartUpload: &s3.CompletedMultipartUpload{ Parts: []*s3.CompletedPart{ { // Required ETag: aws.String("ETag"), PartNumber: aws.Int64(1), }, // More values... }, }, RequestPayer: aws.String("RequestPayer"), } resp, err := svc.CompleteMultipartUpload(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
func ExampleS3_UploadPart() { svc := s3.New(nil) params := &s3.UploadPartInput{ Bucket: aws.String("BucketName"), // Required Key: aws.String("ObjectKey"), // Required PartNumber: aws.Int64(1), // Required UploadId: aws.String("MultipartUploadId"), // Required Body: bytes.NewReader([]byte("PAYLOAD")), ContentLength: aws.Int64(1), RequestPayer: aws.String("RequestPayer"), SSECustomerAlgorithm: aws.String("SSECustomerAlgorithm"), SSECustomerKey: aws.String("SSECustomerKey"), SSECustomerKeyMD5: aws.String("SSECustomerKeyMD5"), } resp, err := svc.UploadPart(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
func ExampleS3_CreateBucket() { svc := s3.New(nil) params := &s3.CreateBucketInput{ Bucket: aws.String("BucketName"), // Required ACL: aws.String("BucketCannedACL"), CreateBucketConfiguration: &s3.CreateBucketConfiguration{ LocationConstraint: aws.String("BucketLocationConstraint"), }, GrantFullControl: aws.String("GrantFullControl"), GrantRead: aws.String("GrantRead"), GrantReadACP: aws.String("GrantReadACP"), GrantWrite: aws.String("GrantWrite"), GrantWriteACP: aws.String("GrantWriteACP"), } resp, err := svc.CreateBucket(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
func TestUploadFailCleanup(t *testing.T) { svc := s3.New(nil) // Break checksum on 2nd part so it fails part := 0 svc.Handlers.Build.PushBack(func(r *request.Request) { if r.Operation.Name == "UploadPart" { if part == 1 { r.HTTPRequest.Header.Set("X-Amz-Content-Sha256", "000") } part++ } }) key := "12mb-leave" mgr := s3manager.NewUploader(&s3manager.UploadOptions{ S3: svc, LeavePartsOnError: false, }) _, err := mgr.Upload(&s3manager.UploadInput{ Bucket: bucketName, Key: &key, Body: bytes.NewReader(integBuf12MB), }) assert.Error(t, err) uploadID := "" if merr, ok := err.(s3manager.MultiUploadFailure); ok { uploadID = merr.UploadID() } assert.NotEmpty(t, uploadID) _, err = svc.ListParts(&s3.ListPartsInput{ Bucket: bucketName, Key: &key, UploadId: &uploadID}) assert.Error(t, err) }
func ExampleS3_PutBucketReplication() { svc := s3.New(nil) params := &s3.PutBucketReplicationInput{ Bucket: aws.String("BucketName"), // Required ReplicationConfiguration: &s3.ReplicationConfiguration{ // Required Role: aws.String("Role"), // Required Rules: []*s3.ReplicationRule{ // Required { // Required Destination: &s3.Destination{ // Required Bucket: aws.String("BucketName"), // Required }, Prefix: aws.String("Prefix"), // Required Status: aws.String("ReplicationRuleStatus"), // Required ID: aws.String("ID"), }, // More values... }, }, } resp, err := svc.PutBucketReplication(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
func ExampleS3_DeleteObjects() { svc := s3.New(nil) params := &s3.DeleteObjectsInput{ Bucket: aws.String("BucketName"), // Required Delete: &s3.Delete{ // Required Objects: []*s3.ObjectIdentifier{ // Required { // Required Key: aws.String("ObjectKey"), // Required VersionId: aws.String("ObjectVersionId"), }, // More values... }, Quiet: aws.Bool(true), }, MFA: aws.String("MFA"), RequestPayer: aws.String("RequestPayer"), } resp, err := svc.DeleteObjects(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
func TestMD5InPutBucketPolicy(t *testing.T) { svc := s3.New(nil) req, _ := svc.PutBucketPolicyRequest(&s3.PutBucketPolicyInput{ Bucket: aws.String("bucketname"), Policy: aws.String("{}"), }) assertMD5(t, req) }
func TestNoPopulateLocationConstraintIfClassic(t *testing.T) { s := s3.New(&aws.Config{Region: aws.String("us-east-1")}) req, _ := s.CreateBucketRequest(&s3.CreateBucketInput{ Bucket: aws.String("bucket"), }) err := req.Build() assert.NoError(t, err) assert.Equal(t, 0, len(awsutil.ValuesAtPath(req.Params, "CreateBucketConfiguration.LocationConstraint"))) }
func TestNoPopulateLocationConstraintIfProvided(t *testing.T) { s := s3.New(nil) req, _ := s.CreateBucketRequest(&s3.CreateBucketInput{ Bucket: aws.String("bucket"), CreateBucketConfiguration: &s3.CreateBucketConfiguration{}, }) err := req.Build() assert.NoError(t, err) assert.Equal(t, 0, len(awsutil.ValuesAtPath(req.Params, "CreateBucketConfiguration.LocationConstraint"))) }
func TestPopulateLocationConstraint(t *testing.T) { s := s3.New(nil) in := &s3.CreateBucketInput{ Bucket: aws.String("bucket"), } req, _ := s.CreateBucketRequest(in) err := req.Build() assert.NoError(t, err) assert.Equal(t, "mock-region", awsutil.ValuesAtPath(req.Params, "CreateBucketConfiguration.LocationConstraint")[0]) assert.Nil(t, in.CreateBucketConfiguration) // don't modify original params }
func TestMD5InDeleteObjects(t *testing.T) { svc := s3.New(nil) req, _ := svc.DeleteObjectsRequest(&s3.DeleteObjectsInput{ Bucket: aws.String("bucketname"), Delete: &s3.Delete{ Objects: []*s3.ObjectIdentifier{ {Key: aws.String("key")}, }, }, }) assertMD5(t, req) }
func TestMD5InPutBucketTagging(t *testing.T) { svc := s3.New(nil) req, _ := svc.PutBucketTaggingRequest(&s3.PutBucketTaggingInput{ Bucket: aws.String("bucketname"), Tagging: &s3.Tagging{ TagSet: []*s3.Tag{ {Key: aws.String("KEY"), Value: aws.String("VALUE")}, }, }, }) assertMD5(t, req) }
func TestMD5InPutBucketCors(t *testing.T) { svc := s3.New(nil) req, _ := svc.PutBucketCorsRequest(&s3.PutBucketCorsInput{ Bucket: aws.String("bucketname"), CORSConfiguration: &s3.CORSConfiguration{ CORSRules: []*s3.CORSRule{ {AllowedMethods: []*string{aws.String("GET")}}, }, }, }) assertMD5(t, req) }
func ExampleS3_CopyObject() { svc := s3.New(nil) params := &s3.CopyObjectInput{ Bucket: aws.String("BucketName"), // Required CopySource: aws.String("CopySource"), // Required Key: aws.String("ObjectKey"), // Required ACL: aws.String("ObjectCannedACL"), CacheControl: aws.String("CacheControl"), ContentDisposition: aws.String("ContentDisposition"), ContentEncoding: aws.String("ContentEncoding"), ContentLanguage: aws.String("ContentLanguage"), ContentType: aws.String("ContentType"), CopySourceIfMatch: aws.String("CopySourceIfMatch"), CopySourceIfModifiedSince: aws.Time(time.Now()), CopySourceIfNoneMatch: aws.String("CopySourceIfNoneMatch"), CopySourceIfUnmodifiedSince: aws.Time(time.Now()), CopySourceSSECustomerAlgorithm: aws.String("CopySourceSSECustomerAlgorithm"), CopySourceSSECustomerKey: aws.String("CopySourceSSECustomerKey"), CopySourceSSECustomerKeyMD5: aws.String("CopySourceSSECustomerKeyMD5"), Expires: aws.Time(time.Now()), GrantFullControl: aws.String("GrantFullControl"), GrantRead: aws.String("GrantRead"), GrantReadACP: aws.String("GrantReadACP"), GrantWriteACP: aws.String("GrantWriteACP"), Metadata: map[string]*string{ "Key": aws.String("MetadataValue"), // Required // More values... }, MetadataDirective: aws.String("MetadataDirective"), RequestPayer: aws.String("RequestPayer"), SSECustomerAlgorithm: aws.String("SSECustomerAlgorithm"), SSECustomerKey: aws.String("SSECustomerKey"), SSECustomerKeyMD5: aws.String("SSECustomerKeyMD5"), SSEKMSKeyId: aws.String("SSEKMSKeyId"), ServerSideEncryption: aws.String("ServerSideEncryption"), StorageClass: aws.String("StorageClass"), WebsiteRedirectLocation: aws.String("WebsiteRedirectLocation"), } resp, err := svc.CopyObject(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
func TestCopySourceSSECustomerKeyOverHTTPError(t *testing.T) { s := s3.New(&aws.Config{DisableSSL: aws.Bool(true)}) req, _ := s.CopyObjectRequest(&s3.CopyObjectInput{ Bucket: aws.String("bucket"), CopySource: aws.String("bucket/source"), Key: aws.String("dest"), CopySourceSSECustomerKey: aws.String("key"), }) err := req.Build() assert.Error(t, err) assert.Equal(t, "ConfigError", err.(awserr.Error).Code()) assert.Contains(t, err.(awserr.Error).Message(), "cannot send SSE keys over HTTP") }
// init will initialize all default options. func (u *uploader) init() { if u.opts.S3 == nil { u.opts.S3 = s3.New(nil) } if u.opts.Concurrency == 0 { u.opts.Concurrency = DefaultUploadConcurrency } if u.opts.PartSize == 0 { u.opts.PartSize = DefaultUploadPartSize } // Try to get the total size for some optimizations u.initSize() }
// init initializes the downloader with default options. func (d *downloader) init() { d.totalBytes = -1 if d.opts.Concurrency == 0 { d.opts.Concurrency = DefaultDownloadConcurrency } if d.opts.PartSize == 0 { d.opts.PartSize = DefaultDownloadPartSize } if d.opts.S3 == nil { d.opts.S3 = s3.New(nil) } }
func ExampleS3_PutBucketNotification() { svc := s3.New(nil) params := &s3.PutBucketNotificationInput{ Bucket: aws.String("BucketName"), // Required NotificationConfiguration: &s3.NotificationConfigurationDeprecated{ // Required CloudFunctionConfiguration: &s3.CloudFunctionConfiguration{ CloudFunction: aws.String("CloudFunction"), Event: aws.String("Event"), Events: []*string{ aws.String("Event"), // Required // More values... }, Id: aws.String("NotificationId"), InvocationRole: aws.String("CloudFunctionInvocationRole"), }, QueueConfiguration: &s3.QueueConfigurationDeprecated{ Event: aws.String("Event"), Events: []*string{ aws.String("Event"), // Required // More values... }, Id: aws.String("NotificationId"), Queue: aws.String("QueueArn"), }, TopicConfiguration: &s3.TopicConfigurationDeprecated{ Event: aws.String("Event"), Events: []*string{ aws.String("Event"), // Required // More values... }, Id: aws.String("NotificationId"), Topic: aws.String("TopicArn"), }, }, } resp, err := svc.PutBucketNotification(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
func ExampleS3_ListBuckets() { svc := s3.New(nil) var params *s3.ListBucketsInput resp, err := svc.ListBuckets(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
func TestMD5InPutBucketLifecycle(t *testing.T) { svc := s3.New(nil) req, _ := svc.PutBucketLifecycleRequest(&s3.PutBucketLifecycleInput{ Bucket: aws.String("bucketname"), LifecycleConfiguration: &s3.LifecycleConfiguration{ Rules: []*s3.LifecycleRule{ { ID: aws.String("ID"), Prefix: aws.String("Prefix"), Status: aws.String("Enabled"), }, }, }, }) assertMD5(t, req) }
// Use S3 for simplicity func TestPaginationTruncation(t *testing.T) { count := 0 client := s3.New(nil) reqNum := &count resps := []*s3.ListObjectsOutput{ {IsTruncated: aws.Bool(true), Contents: []*s3.Object{{Key: aws.String("Key1")}}}, {IsTruncated: aws.Bool(true), Contents: []*s3.Object{{Key: aws.String("Key2")}}}, {IsTruncated: aws.Bool(false), Contents: []*s3.Object{{Key: aws.String("Key3")}}}, {IsTruncated: aws.Bool(true), Contents: []*s3.Object{{Key: aws.String("Key4")}}}, } client.Handlers.Send.Clear() // mock sending client.Handlers.Unmarshal.Clear() client.Handlers.UnmarshalMeta.Clear() client.Handlers.ValidateResponse.Clear() client.Handlers.Unmarshal.PushBack(func(r *request.Request) { r.Data = resps[*reqNum] *reqNum++ }) params := &s3.ListObjectsInput{Bucket: aws.String("bucket")} results := []string{} err := client.ListObjectsPages(params, func(p *s3.ListObjectsOutput, last bool) bool { results = append(results, *p.Contents[0].Key) return true }) assert.Equal(t, []string{"Key1", "Key2", "Key3"}, results) assert.Nil(t, err) // Try again without truncation token at all count = 0 resps[1].IsTruncated = nil resps[2].IsTruncated = aws.Bool(true) results = []string{} err = client.ListObjectsPages(params, func(p *s3.ListObjectsOutput, last bool) bool { results = append(results, *p.Contents[0].Key) return true }) assert.Equal(t, []string{"Key1", "Key2"}, results) assert.Nil(t, err) }
func TestComputeSSEKeys(t *testing.T) { s := s3.New(nil) req, _ := s.CopyObjectRequest(&s3.CopyObjectInput{ Bucket: aws.String("bucket"), CopySource: aws.String("bucket/source"), Key: aws.String("dest"), SSECustomerKey: aws.String("key"), CopySourceSSECustomerKey: aws.String("key"), }) err := req.Build() assert.NoError(t, err) assert.Equal(t, "a2V5", req.HTTPRequest.Header.Get("x-amz-server-side-encryption-customer-key")) assert.Equal(t, "a2V5", req.HTTPRequest.Header.Get("x-amz-copy-source-server-side-encryption-customer-key")) assert.Equal(t, "PG4LipwVIkqCKLmpjKFTHQ==", req.HTTPRequest.Header.Get("x-amz-server-side-encryption-customer-key-md5")) assert.Equal(t, "PG4LipwVIkqCKLmpjKFTHQ==", req.HTTPRequest.Header.Get("x-amz-copy-source-server-side-encryption-customer-key-md5")) }
func ExampleS3_PutBucketWebsite() { svc := s3.New(nil) params := &s3.PutBucketWebsiteInput{ Bucket: aws.String("BucketName"), // Required WebsiteConfiguration: &s3.WebsiteConfiguration{ // Required ErrorDocument: &s3.ErrorDocument{ Key: aws.String("ObjectKey"), // Required }, IndexDocument: &s3.IndexDocument{ Suffix: aws.String("Suffix"), // Required }, RedirectAllRequestsTo: &s3.RedirectAllRequestsTo{ HostName: aws.String("HostName"), // Required Protocol: aws.String("Protocol"), }, RoutingRules: []*s3.RoutingRule{ { // Required Redirect: &s3.Redirect{ // Required HostName: aws.String("HostName"), HttpRedirectCode: aws.String("HttpRedirectCode"), Protocol: aws.String("Protocol"), ReplaceKeyPrefixWith: aws.String("ReplaceKeyPrefixWith"), ReplaceKeyWith: aws.String("ReplaceKeyWith"), }, Condition: &s3.Condition{ HttpErrorCodeReturnedEquals: aws.String("HttpErrorCodeReturnedEquals"), KeyPrefixEquals: aws.String("KeyPrefixEquals"), }, }, // More values... }, }, } resp, err := svc.PutBucketWebsite(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
func ExampleS3_PutObjectAcl() { svc := s3.New(nil) params := &s3.PutObjectAclInput{ Bucket: aws.String("BucketName"), // Required Key: aws.String("ObjectKey"), // Required ACL: aws.String("ObjectCannedACL"), AccessControlPolicy: &s3.AccessControlPolicy{ Grants: []*s3.Grant{ { // Required Grantee: &s3.Grantee{ Type: aws.String("Type"), // Required DisplayName: aws.String("DisplayName"), EmailAddress: aws.String("EmailAddress"), ID: aws.String("ID"), URI: aws.String("URI"), }, Permission: aws.String("Permission"), }, // More values... }, Owner: &s3.Owner{ DisplayName: aws.String("DisplayName"), ID: aws.String("ID"), }, }, GrantFullControl: aws.String("GrantFullControl"), GrantRead: aws.String("GrantRead"), GrantReadACP: aws.String("GrantReadACP"), GrantWrite: aws.String("GrantWrite"), GrantWriteACP: aws.String("GrantWriteACP"), RequestPayer: aws.String("RequestPayer"), } resp, err := svc.PutObjectAcl(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
func ExampleS3_HeadBucket() { svc := s3.New(nil) params := &s3.HeadBucketInput{ Bucket: aws.String("BucketName"), // Required } resp, err := svc.HeadBucket(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }