1// Code generated by private/model/cli/gen-api/main.go. DO NOT EDIT. 2 3package s3_test 4 5import ( 6 "fmt" 7 "strings" 8 "time" 9 10 "github.com/aws/aws-sdk-go/aws" 11 "github.com/aws/aws-sdk-go/aws/awserr" 12 "github.com/aws/aws-sdk-go/aws/session" 13 "github.com/aws/aws-sdk-go/service/s3" 14) 15 16var _ time.Duration 17var _ strings.Reader 18var _ aws.Config 19 20func parseTime(layout, value string) *time.Time { 21 t, err := time.Parse(layout, value) 22 if err != nil { 23 panic(err) 24 } 25 return &t 26} 27 28// To abort a multipart upload 29// 30// The following example aborts a multipart upload. 31func ExampleS3_AbortMultipartUpload_shared00() { 32 svc := s3.New(session.New()) 33 input := &s3.AbortMultipartUploadInput{ 34 Bucket: aws.String("examplebucket"), 35 Key: aws.String("bigobject"), 36 UploadId: aws.String("xadcOB_7YPBOJuoFiQ9cz4P3Pe6FIZwO4f7wN93uHsNBEw97pl5eNwzExg0LAT2dUN91cOmrEQHDsP3WA60CEg--"), 37 } 38 39 result, err := svc.AbortMultipartUpload(input) 40 if err != nil { 41 if aerr, ok := err.(awserr.Error); ok { 42 switch aerr.Code() { 43 case s3.ErrCodeNoSuchUpload: 44 fmt.Println(s3.ErrCodeNoSuchUpload, aerr.Error()) 45 default: 46 fmt.Println(aerr.Error()) 47 } 48 } else { 49 // Print the error, cast err to awserr.Error to get the Code and 50 // Message from an error. 51 fmt.Println(err.Error()) 52 } 53 return 54 } 55 56 fmt.Println(result) 57} 58 59// To complete multipart upload 60// 61// The following example completes a multipart upload. 62func ExampleS3_CompleteMultipartUpload_shared00() { 63 svc := s3.New(session.New()) 64 input := &s3.CompleteMultipartUploadInput{ 65 Bucket: aws.String("examplebucket"), 66 Key: aws.String("bigobject"), 67 MultipartUpload: &s3.CompletedMultipartUpload{ 68 Parts: []*s3.CompletedPart{ 69 { 70 ETag: aws.String("\"d8c2eafd90c266e19ab9dcacc479f8af\""), 71 PartNumber: aws.Int64(1), 72 }, 73 { 74 ETag: aws.String("\"d8c2eafd90c266e19ab9dcacc479f8af\""), 75 PartNumber: aws.Int64(2), 76 }, 77 }, 78 }, 79 UploadId: aws.String("7YPBOJuoFiQ9cz4P3Pe6FIZwO4f7wN93uHsNBEw97pl5eNwzExg0LAT2dUN91cOmrEQHDsP3WA60CEg--"), 80 } 81 82 result, err := svc.CompleteMultipartUpload(input) 83 if err != nil { 84 if aerr, ok := err.(awserr.Error); ok { 85 switch aerr.Code() { 86 default: 87 fmt.Println(aerr.Error()) 88 } 89 } else { 90 // Print the error, cast err to awserr.Error to get the Code and 91 // Message from an error. 92 fmt.Println(err.Error()) 93 } 94 return 95 } 96 97 fmt.Println(result) 98} 99 100// To copy an object 101// 102// The following example copies an object from one bucket to another. 103func ExampleS3_CopyObject_shared00() { 104 svc := s3.New(session.New()) 105 input := &s3.CopyObjectInput{ 106 Bucket: aws.String("destinationbucket"), 107 CopySource: aws.String("/sourcebucket/HappyFacejpg"), 108 Key: aws.String("HappyFaceCopyjpg"), 109 } 110 111 result, err := svc.CopyObject(input) 112 if err != nil { 113 if aerr, ok := err.(awserr.Error); ok { 114 switch aerr.Code() { 115 case s3.ErrCodeObjectNotInActiveTierError: 116 fmt.Println(s3.ErrCodeObjectNotInActiveTierError, aerr.Error()) 117 default: 118 fmt.Println(aerr.Error()) 119 } 120 } else { 121 // Print the error, cast err to awserr.Error to get the Code and 122 // Message from an error. 123 fmt.Println(err.Error()) 124 } 125 return 126 } 127 128 fmt.Println(result) 129} 130 131// To create a bucket 132// 133// The following example creates a bucket. 134func ExampleS3_CreateBucket_shared00() { 135 svc := s3.New(session.New()) 136 input := &s3.CreateBucketInput{ 137 Bucket: aws.String("examplebucket"), 138 } 139 140 result, err := svc.CreateBucket(input) 141 if err != nil { 142 if aerr, ok := err.(awserr.Error); ok { 143 switch aerr.Code() { 144 case s3.ErrCodeBucketAlreadyExists: 145 fmt.Println(s3.ErrCodeBucketAlreadyExists, aerr.Error()) 146 case s3.ErrCodeBucketAlreadyOwnedByYou: 147 fmt.Println(s3.ErrCodeBucketAlreadyOwnedByYou, aerr.Error()) 148 default: 149 fmt.Println(aerr.Error()) 150 } 151 } else { 152 // Print the error, cast err to awserr.Error to get the Code and 153 // Message from an error. 154 fmt.Println(err.Error()) 155 } 156 return 157 } 158 159 fmt.Println(result) 160} 161 162// To create a bucket in a specific region 163// 164// The following example creates a bucket. The request specifies an AWS region where 165// to create the bucket. 166func ExampleS3_CreateBucket_shared01() { 167 svc := s3.New(session.New()) 168 input := &s3.CreateBucketInput{ 169 Bucket: aws.String("examplebucket"), 170 CreateBucketConfiguration: &s3.CreateBucketConfiguration{ 171 LocationConstraint: aws.String("eu-west-1"), 172 }, 173 } 174 175 result, err := svc.CreateBucket(input) 176 if err != nil { 177 if aerr, ok := err.(awserr.Error); ok { 178 switch aerr.Code() { 179 case s3.ErrCodeBucketAlreadyExists: 180 fmt.Println(s3.ErrCodeBucketAlreadyExists, aerr.Error()) 181 case s3.ErrCodeBucketAlreadyOwnedByYou: 182 fmt.Println(s3.ErrCodeBucketAlreadyOwnedByYou, aerr.Error()) 183 default: 184 fmt.Println(aerr.Error()) 185 } 186 } else { 187 // Print the error, cast err to awserr.Error to get the Code and 188 // Message from an error. 189 fmt.Println(err.Error()) 190 } 191 return 192 } 193 194 fmt.Println(result) 195} 196 197// To initiate a multipart upload 198// 199// The following example initiates a multipart upload. 200func ExampleS3_CreateMultipartUpload_shared00() { 201 svc := s3.New(session.New()) 202 input := &s3.CreateMultipartUploadInput{ 203 Bucket: aws.String("examplebucket"), 204 Key: aws.String("largeobject"), 205 } 206 207 result, err := svc.CreateMultipartUpload(input) 208 if err != nil { 209 if aerr, ok := err.(awserr.Error); ok { 210 switch aerr.Code() { 211 default: 212 fmt.Println(aerr.Error()) 213 } 214 } else { 215 // Print the error, cast err to awserr.Error to get the Code and 216 // Message from an error. 217 fmt.Println(err.Error()) 218 } 219 return 220 } 221 222 fmt.Println(result) 223} 224 225// To delete a bucket 226// 227// The following example deletes the specified bucket. 228func ExampleS3_DeleteBucket_shared00() { 229 svc := s3.New(session.New()) 230 input := &s3.DeleteBucketInput{ 231 Bucket: aws.String("forrandall2"), 232 } 233 234 result, err := svc.DeleteBucket(input) 235 if err != nil { 236 if aerr, ok := err.(awserr.Error); ok { 237 switch aerr.Code() { 238 default: 239 fmt.Println(aerr.Error()) 240 } 241 } else { 242 // Print the error, cast err to awserr.Error to get the Code and 243 // Message from an error. 244 fmt.Println(err.Error()) 245 } 246 return 247 } 248 249 fmt.Println(result) 250} 251 252// To delete cors configuration on a bucket. 253// 254// The following example deletes CORS configuration on a bucket. 255func ExampleS3_DeleteBucketCors_shared00() { 256 svc := s3.New(session.New()) 257 input := &s3.DeleteBucketCorsInput{ 258 Bucket: aws.String("examplebucket"), 259 } 260 261 result, err := svc.DeleteBucketCors(input) 262 if err != nil { 263 if aerr, ok := err.(awserr.Error); ok { 264 switch aerr.Code() { 265 default: 266 fmt.Println(aerr.Error()) 267 } 268 } else { 269 // Print the error, cast err to awserr.Error to get the Code and 270 // Message from an error. 271 fmt.Println(err.Error()) 272 } 273 return 274 } 275 276 fmt.Println(result) 277} 278 279// To delete lifecycle configuration on a bucket. 280// 281// The following example deletes lifecycle configuration on a bucket. 282func ExampleS3_DeleteBucketLifecycle_shared00() { 283 svc := s3.New(session.New()) 284 input := &s3.DeleteBucketLifecycleInput{ 285 Bucket: aws.String("examplebucket"), 286 } 287 288 result, err := svc.DeleteBucketLifecycle(input) 289 if err != nil { 290 if aerr, ok := err.(awserr.Error); ok { 291 switch aerr.Code() { 292 default: 293 fmt.Println(aerr.Error()) 294 } 295 } else { 296 // Print the error, cast err to awserr.Error to get the Code and 297 // Message from an error. 298 fmt.Println(err.Error()) 299 } 300 return 301 } 302 303 fmt.Println(result) 304} 305 306// To delete bucket policy 307// 308// The following example deletes bucket policy on the specified bucket. 309func ExampleS3_DeleteBucketPolicy_shared00() { 310 svc := s3.New(session.New()) 311 input := &s3.DeleteBucketPolicyInput{ 312 Bucket: aws.String("examplebucket"), 313 } 314 315 result, err := svc.DeleteBucketPolicy(input) 316 if err != nil { 317 if aerr, ok := err.(awserr.Error); ok { 318 switch aerr.Code() { 319 default: 320 fmt.Println(aerr.Error()) 321 } 322 } else { 323 // Print the error, cast err to awserr.Error to get the Code and 324 // Message from an error. 325 fmt.Println(err.Error()) 326 } 327 return 328 } 329 330 fmt.Println(result) 331} 332 333// To delete bucket replication configuration 334// 335// The following example deletes replication configuration set on bucket. 336func ExampleS3_DeleteBucketReplication_shared00() { 337 svc := s3.New(session.New()) 338 input := &s3.DeleteBucketReplicationInput{ 339 Bucket: aws.String("example"), 340 } 341 342 result, err := svc.DeleteBucketReplication(input) 343 if err != nil { 344 if aerr, ok := err.(awserr.Error); ok { 345 switch aerr.Code() { 346 default: 347 fmt.Println(aerr.Error()) 348 } 349 } else { 350 // Print the error, cast err to awserr.Error to get the Code and 351 // Message from an error. 352 fmt.Println(err.Error()) 353 } 354 return 355 } 356 357 fmt.Println(result) 358} 359 360// To delete bucket tags 361// 362// The following example deletes bucket tags. 363func ExampleS3_DeleteBucketTagging_shared00() { 364 svc := s3.New(session.New()) 365 input := &s3.DeleteBucketTaggingInput{ 366 Bucket: aws.String("examplebucket"), 367 } 368 369 result, err := svc.DeleteBucketTagging(input) 370 if err != nil { 371 if aerr, ok := err.(awserr.Error); ok { 372 switch aerr.Code() { 373 default: 374 fmt.Println(aerr.Error()) 375 } 376 } else { 377 // Print the error, cast err to awserr.Error to get the Code and 378 // Message from an error. 379 fmt.Println(err.Error()) 380 } 381 return 382 } 383 384 fmt.Println(result) 385} 386 387// To delete bucket website configuration 388// 389// The following example deletes bucket website configuration. 390func ExampleS3_DeleteBucketWebsite_shared00() { 391 svc := s3.New(session.New()) 392 input := &s3.DeleteBucketWebsiteInput{ 393 Bucket: aws.String("examplebucket"), 394 } 395 396 result, err := svc.DeleteBucketWebsite(input) 397 if err != nil { 398 if aerr, ok := err.(awserr.Error); ok { 399 switch aerr.Code() { 400 default: 401 fmt.Println(aerr.Error()) 402 } 403 } else { 404 // Print the error, cast err to awserr.Error to get the Code and 405 // Message from an error. 406 fmt.Println(err.Error()) 407 } 408 return 409 } 410 411 fmt.Println(result) 412} 413 414// To delete an object (from a non-versioned bucket) 415// 416// The following example deletes an object from a non-versioned bucket. 417func ExampleS3_DeleteObject_shared00() { 418 svc := s3.New(session.New()) 419 input := &s3.DeleteObjectInput{ 420 Bucket: aws.String("ExampleBucket"), 421 Key: aws.String("HappyFace.jpg"), 422 } 423 424 result, err := svc.DeleteObject(input) 425 if err != nil { 426 if aerr, ok := err.(awserr.Error); ok { 427 switch aerr.Code() { 428 default: 429 fmt.Println(aerr.Error()) 430 } 431 } else { 432 // Print the error, cast err to awserr.Error to get the Code and 433 // Message from an error. 434 fmt.Println(err.Error()) 435 } 436 return 437 } 438 439 fmt.Println(result) 440} 441 442// To delete an object 443// 444// The following example deletes an object from an S3 bucket. 445func ExampleS3_DeleteObject_shared01() { 446 svc := s3.New(session.New()) 447 input := &s3.DeleteObjectInput{ 448 Bucket: aws.String("examplebucket"), 449 Key: aws.String("objectkey.jpg"), 450 } 451 452 result, err := svc.DeleteObject(input) 453 if err != nil { 454 if aerr, ok := err.(awserr.Error); ok { 455 switch aerr.Code() { 456 default: 457 fmt.Println(aerr.Error()) 458 } 459 } else { 460 // Print the error, cast err to awserr.Error to get the Code and 461 // Message from an error. 462 fmt.Println(err.Error()) 463 } 464 return 465 } 466 467 fmt.Println(result) 468} 469 470// To remove tag set from an object version 471// 472// The following example removes tag set associated with the specified object version. 473// The request specifies both the object key and object version. 474func ExampleS3_DeleteObjectTagging_shared00() { 475 svc := s3.New(session.New()) 476 input := &s3.DeleteObjectTaggingInput{ 477 Bucket: aws.String("examplebucket"), 478 Key: aws.String("HappyFace.jpg"), 479 VersionId: aws.String("ydlaNkwWm0SfKJR.T1b1fIdPRbldTYRI"), 480 } 481 482 result, err := svc.DeleteObjectTagging(input) 483 if err != nil { 484 if aerr, ok := err.(awserr.Error); ok { 485 switch aerr.Code() { 486 default: 487 fmt.Println(aerr.Error()) 488 } 489 } else { 490 // Print the error, cast err to awserr.Error to get the Code and 491 // Message from an error. 492 fmt.Println(err.Error()) 493 } 494 return 495 } 496 497 fmt.Println(result) 498} 499 500// To remove tag set from an object 501// 502// The following example removes tag set associated with the specified object. If the 503// bucket is versioning enabled, the operation removes tag set from the latest object 504// version. 505func ExampleS3_DeleteObjectTagging_shared01() { 506 svc := s3.New(session.New()) 507 input := &s3.DeleteObjectTaggingInput{ 508 Bucket: aws.String("examplebucket"), 509 Key: aws.String("HappyFace.jpg"), 510 } 511 512 result, err := svc.DeleteObjectTagging(input) 513 if err != nil { 514 if aerr, ok := err.(awserr.Error); ok { 515 switch aerr.Code() { 516 default: 517 fmt.Println(aerr.Error()) 518 } 519 } else { 520 // Print the error, cast err to awserr.Error to get the Code and 521 // Message from an error. 522 fmt.Println(err.Error()) 523 } 524 return 525 } 526 527 fmt.Println(result) 528} 529 530// To delete multiple object versions from a versioned bucket 531// 532// The following example deletes objects from a bucket. The request specifies object 533// versions. S3 deletes specific object versions and returns the key and versions of 534// deleted objects in the response. 535func ExampleS3_DeleteObjects_shared00() { 536 svc := s3.New(session.New()) 537 input := &s3.DeleteObjectsInput{ 538 Bucket: aws.String("examplebucket"), 539 Delete: &s3.Delete{ 540 Objects: []*s3.ObjectIdentifier{ 541 { 542 Key: aws.String("HappyFace.jpg"), 543 VersionId: aws.String("2LWg7lQLnY41.maGB5Z6SWW.dcq0vx7b"), 544 }, 545 { 546 Key: aws.String("HappyFace.jpg"), 547 VersionId: aws.String("yoz3HB.ZhCS_tKVEmIOr7qYyyAaZSKVd"), 548 }, 549 }, 550 Quiet: aws.Bool(false), 551 }, 552 } 553 554 result, err := svc.DeleteObjects(input) 555 if err != nil { 556 if aerr, ok := err.(awserr.Error); ok { 557 switch aerr.Code() { 558 default: 559 fmt.Println(aerr.Error()) 560 } 561 } else { 562 // Print the error, cast err to awserr.Error to get the Code and 563 // Message from an error. 564 fmt.Println(err.Error()) 565 } 566 return 567 } 568 569 fmt.Println(result) 570} 571 572// To delete multiple objects from a versioned bucket 573// 574// The following example deletes objects from a bucket. The bucket is versioned, and 575// the request does not specify the object version to delete. In this case, all versions 576// remain in the bucket and S3 adds a delete marker. 577func ExampleS3_DeleteObjects_shared01() { 578 svc := s3.New(session.New()) 579 input := &s3.DeleteObjectsInput{ 580 Bucket: aws.String("examplebucket"), 581 Delete: &s3.Delete{ 582 Objects: []*s3.ObjectIdentifier{ 583 { 584 Key: aws.String("objectkey1"), 585 }, 586 { 587 Key: aws.String("objectkey2"), 588 }, 589 }, 590 Quiet: aws.Bool(false), 591 }, 592 } 593 594 result, err := svc.DeleteObjects(input) 595 if err != nil { 596 if aerr, ok := err.(awserr.Error); ok { 597 switch aerr.Code() { 598 default: 599 fmt.Println(aerr.Error()) 600 } 601 } else { 602 // Print the error, cast err to awserr.Error to get the Code and 603 // Message from an error. 604 fmt.Println(err.Error()) 605 } 606 return 607 } 608 609 fmt.Println(result) 610} 611 612// To get cors configuration set on a bucket 613// 614// The following example returns cross-origin resource sharing (CORS) configuration 615// set on a bucket. 616func ExampleS3_GetBucketCors_shared00() { 617 svc := s3.New(session.New()) 618 input := &s3.GetBucketCorsInput{ 619 Bucket: aws.String("examplebucket"), 620 } 621 622 result, err := svc.GetBucketCors(input) 623 if err != nil { 624 if aerr, ok := err.(awserr.Error); ok { 625 switch aerr.Code() { 626 default: 627 fmt.Println(aerr.Error()) 628 } 629 } else { 630 // Print the error, cast err to awserr.Error to get the Code and 631 // Message from an error. 632 fmt.Println(err.Error()) 633 } 634 return 635 } 636 637 fmt.Println(result) 638} 639 640// To get a bucket acl 641// 642// The following example gets ACL on the specified bucket. 643func ExampleS3_GetBucketLifecycle_shared00() { 644 svc := s3.New(session.New()) 645 input := &s3.GetBucketLifecycleInput{ 646 Bucket: aws.String("acl1"), 647 } 648 649 result, err := svc.GetBucketLifecycle(input) 650 if err != nil { 651 if aerr, ok := err.(awserr.Error); ok { 652 switch aerr.Code() { 653 default: 654 fmt.Println(aerr.Error()) 655 } 656 } else { 657 // Print the error, cast err to awserr.Error to get the Code and 658 // Message from an error. 659 fmt.Println(err.Error()) 660 } 661 return 662 } 663 664 fmt.Println(result) 665} 666 667// To get lifecycle configuration on a bucket 668// 669// The following example retrieves lifecycle configuration on set on a bucket. 670func ExampleS3_GetBucketLifecycleConfiguration_shared00() { 671 svc := s3.New(session.New()) 672 input := &s3.GetBucketLifecycleConfigurationInput{ 673 Bucket: aws.String("examplebucket"), 674 } 675 676 result, err := svc.GetBucketLifecycleConfiguration(input) 677 if err != nil { 678 if aerr, ok := err.(awserr.Error); ok { 679 switch aerr.Code() { 680 default: 681 fmt.Println(aerr.Error()) 682 } 683 } else { 684 // Print the error, cast err to awserr.Error to get the Code and 685 // Message from an error. 686 fmt.Println(err.Error()) 687 } 688 return 689 } 690 691 fmt.Println(result) 692} 693 694// To get bucket location 695// 696// The following example returns bucket location. 697func ExampleS3_GetBucketLocation_shared00() { 698 svc := s3.New(session.New()) 699 input := &s3.GetBucketLocationInput{ 700 Bucket: aws.String("examplebucket"), 701 } 702 703 result, err := svc.GetBucketLocation(input) 704 if err != nil { 705 if aerr, ok := err.(awserr.Error); ok { 706 switch aerr.Code() { 707 default: 708 fmt.Println(aerr.Error()) 709 } 710 } else { 711 // Print the error, cast err to awserr.Error to get the Code and 712 // Message from an error. 713 fmt.Println(err.Error()) 714 } 715 return 716 } 717 718 fmt.Println(result) 719} 720 721// To get notification configuration set on a bucket 722// 723// The following example returns notification configuration set on a bucket. 724func ExampleS3_GetBucketNotification_shared00() { 725 svc := s3.New(session.New()) 726 input := &s3.GetBucketNotificationConfigurationRequest{ 727 Bucket: aws.String("examplebucket"), 728 } 729 730 result, err := svc.GetBucketNotification(input) 731 if err != nil { 732 if aerr, ok := err.(awserr.Error); ok { 733 switch aerr.Code() { 734 default: 735 fmt.Println(aerr.Error()) 736 } 737 } else { 738 // Print the error, cast err to awserr.Error to get the Code and 739 // Message from an error. 740 fmt.Println(err.Error()) 741 } 742 return 743 } 744 745 fmt.Println(result) 746} 747 748// To get notification configuration set on a bucket 749// 750// The following example returns notification configuration set on a bucket. 751func ExampleS3_GetBucketNotification_shared01() { 752 svc := s3.New(session.New()) 753 input := &s3.GetBucketNotificationConfigurationRequest{ 754 Bucket: aws.String("examplebucket"), 755 } 756 757 result, err := svc.GetBucketNotification(input) 758 if err != nil { 759 if aerr, ok := err.(awserr.Error); ok { 760 switch aerr.Code() { 761 default: 762 fmt.Println(aerr.Error()) 763 } 764 } else { 765 // Print the error, cast err to awserr.Error to get the Code and 766 // Message from an error. 767 fmt.Println(err.Error()) 768 } 769 return 770 } 771 772 fmt.Println(result) 773} 774 775// To get bucket policy 776// 777// The following example returns bucket policy associated with a bucket. 778func ExampleS3_GetBucketPolicy_shared00() { 779 svc := s3.New(session.New()) 780 input := &s3.GetBucketPolicyInput{ 781 Bucket: aws.String("examplebucket"), 782 } 783 784 result, err := svc.GetBucketPolicy(input) 785 if err != nil { 786 if aerr, ok := err.(awserr.Error); ok { 787 switch aerr.Code() { 788 default: 789 fmt.Println(aerr.Error()) 790 } 791 } else { 792 // Print the error, cast err to awserr.Error to get the Code and 793 // Message from an error. 794 fmt.Println(err.Error()) 795 } 796 return 797 } 798 799 fmt.Println(result) 800} 801 802// To get replication configuration set on a bucket 803// 804// The following example returns replication configuration set on a bucket. 805func ExampleS3_GetBucketReplication_shared00() { 806 svc := s3.New(session.New()) 807 input := &s3.GetBucketReplicationInput{ 808 Bucket: aws.String("examplebucket"), 809 } 810 811 result, err := svc.GetBucketReplication(input) 812 if err != nil { 813 if aerr, ok := err.(awserr.Error); ok { 814 switch aerr.Code() { 815 default: 816 fmt.Println(aerr.Error()) 817 } 818 } else { 819 // Print the error, cast err to awserr.Error to get the Code and 820 // Message from an error. 821 fmt.Println(err.Error()) 822 } 823 return 824 } 825 826 fmt.Println(result) 827} 828 829// To get bucket versioning configuration 830// 831// The following example retrieves bucket versioning configuration. 832func ExampleS3_GetBucketRequestPayment_shared00() { 833 svc := s3.New(session.New()) 834 input := &s3.GetBucketRequestPaymentInput{ 835 Bucket: aws.String("examplebucket"), 836 } 837 838 result, err := svc.GetBucketRequestPayment(input) 839 if err != nil { 840 if aerr, ok := err.(awserr.Error); ok { 841 switch aerr.Code() { 842 default: 843 fmt.Println(aerr.Error()) 844 } 845 } else { 846 // Print the error, cast err to awserr.Error to get the Code and 847 // Message from an error. 848 fmt.Println(err.Error()) 849 } 850 return 851 } 852 853 fmt.Println(result) 854} 855 856// To get tag set associated with a bucket 857// 858// The following example returns tag set associated with a bucket 859func ExampleS3_GetBucketTagging_shared00() { 860 svc := s3.New(session.New()) 861 input := &s3.GetBucketTaggingInput{ 862 Bucket: aws.String("examplebucket"), 863 } 864 865 result, err := svc.GetBucketTagging(input) 866 if err != nil { 867 if aerr, ok := err.(awserr.Error); ok { 868 switch aerr.Code() { 869 default: 870 fmt.Println(aerr.Error()) 871 } 872 } else { 873 // Print the error, cast err to awserr.Error to get the Code and 874 // Message from an error. 875 fmt.Println(err.Error()) 876 } 877 return 878 } 879 880 fmt.Println(result) 881} 882 883// To get bucket versioning configuration 884// 885// The following example retrieves bucket versioning configuration. 886func ExampleS3_GetBucketVersioning_shared00() { 887 svc := s3.New(session.New()) 888 input := &s3.GetBucketVersioningInput{ 889 Bucket: aws.String("examplebucket"), 890 } 891 892 result, err := svc.GetBucketVersioning(input) 893 if err != nil { 894 if aerr, ok := err.(awserr.Error); ok { 895 switch aerr.Code() { 896 default: 897 fmt.Println(aerr.Error()) 898 } 899 } else { 900 // Print the error, cast err to awserr.Error to get the Code and 901 // Message from an error. 902 fmt.Println(err.Error()) 903 } 904 return 905 } 906 907 fmt.Println(result) 908} 909 910// To get bucket website configuration 911// 912// The following example retrieves website configuration of a bucket. 913func ExampleS3_GetBucketWebsite_shared00() { 914 svc := s3.New(session.New()) 915 input := &s3.GetBucketWebsiteInput{ 916 Bucket: aws.String("examplebucket"), 917 } 918 919 result, err := svc.GetBucketWebsite(input) 920 if err != nil { 921 if aerr, ok := err.(awserr.Error); ok { 922 switch aerr.Code() { 923 default: 924 fmt.Println(aerr.Error()) 925 } 926 } else { 927 // Print the error, cast err to awserr.Error to get the Code and 928 // Message from an error. 929 fmt.Println(err.Error()) 930 } 931 return 932 } 933 934 fmt.Println(result) 935} 936 937// To retrieve an object 938// 939// The following example retrieves an object for an S3 bucket. 940func ExampleS3_GetObject_shared00() { 941 svc := s3.New(session.New()) 942 input := &s3.GetObjectInput{ 943 Bucket: aws.String("examplebucket"), 944 Key: aws.String("HappyFace.jpg"), 945 } 946 947 result, err := svc.GetObject(input) 948 if err != nil { 949 if aerr, ok := err.(awserr.Error); ok { 950 switch aerr.Code() { 951 case s3.ErrCodeNoSuchKey: 952 fmt.Println(s3.ErrCodeNoSuchKey, aerr.Error()) 953 case s3.ErrCodeInvalidObjectState: 954 fmt.Println(s3.ErrCodeInvalidObjectState, aerr.Error()) 955 default: 956 fmt.Println(aerr.Error()) 957 } 958 } else { 959 // Print the error, cast err to awserr.Error to get the Code and 960 // Message from an error. 961 fmt.Println(err.Error()) 962 } 963 return 964 } 965 966 fmt.Println(result) 967} 968 969// To retrieve a byte range of an object 970// 971// The following example retrieves an object for an S3 bucket. The request specifies 972// the range header to retrieve a specific byte range. 973func ExampleS3_GetObject_shared01() { 974 svc := s3.New(session.New()) 975 input := &s3.GetObjectInput{ 976 Bucket: aws.String("examplebucket"), 977 Key: aws.String("SampleFile.txt"), 978 Range: aws.String("bytes=0-9"), 979 } 980 981 result, err := svc.GetObject(input) 982 if err != nil { 983 if aerr, ok := err.(awserr.Error); ok { 984 switch aerr.Code() { 985 case s3.ErrCodeNoSuchKey: 986 fmt.Println(s3.ErrCodeNoSuchKey, aerr.Error()) 987 case s3.ErrCodeInvalidObjectState: 988 fmt.Println(s3.ErrCodeInvalidObjectState, aerr.Error()) 989 default: 990 fmt.Println(aerr.Error()) 991 } 992 } else { 993 // Print the error, cast err to awserr.Error to get the Code and 994 // Message from an error. 995 fmt.Println(err.Error()) 996 } 997 return 998 } 999 1000 fmt.Println(result) 1001} 1002 1003// To retrieve object ACL 1004// 1005// The following example retrieves access control list (ACL) of an object. 1006func ExampleS3_GetObjectAcl_shared00() { 1007 svc := s3.New(session.New()) 1008 input := &s3.GetObjectAclInput{ 1009 Bucket: aws.String("examplebucket"), 1010 Key: aws.String("HappyFace.jpg"), 1011 } 1012 1013 result, err := svc.GetObjectAcl(input) 1014 if err != nil { 1015 if aerr, ok := err.(awserr.Error); ok { 1016 switch aerr.Code() { 1017 case s3.ErrCodeNoSuchKey: 1018 fmt.Println(s3.ErrCodeNoSuchKey, aerr.Error()) 1019 default: 1020 fmt.Println(aerr.Error()) 1021 } 1022 } else { 1023 // Print the error, cast err to awserr.Error to get the Code and 1024 // Message from an error. 1025 fmt.Println(err.Error()) 1026 } 1027 return 1028 } 1029 1030 fmt.Println(result) 1031} 1032 1033// To retrieve tag set of a specific object version 1034// 1035// The following example retrieves tag set of an object. The request specifies object 1036// version. 1037func ExampleS3_GetObjectTagging_shared00() { 1038 svc := s3.New(session.New()) 1039 input := &s3.GetObjectTaggingInput{ 1040 Bucket: aws.String("examplebucket"), 1041 Key: aws.String("exampleobject"), 1042 VersionId: aws.String("ydlaNkwWm0SfKJR.T1b1fIdPRbldTYRI"), 1043 } 1044 1045 result, err := svc.GetObjectTagging(input) 1046 if err != nil { 1047 if aerr, ok := err.(awserr.Error); ok { 1048 switch aerr.Code() { 1049 default: 1050 fmt.Println(aerr.Error()) 1051 } 1052 } else { 1053 // Print the error, cast err to awserr.Error to get the Code and 1054 // Message from an error. 1055 fmt.Println(err.Error()) 1056 } 1057 return 1058 } 1059 1060 fmt.Println(result) 1061} 1062 1063// To retrieve tag set of an object 1064// 1065// The following example retrieves tag set of an object. 1066func ExampleS3_GetObjectTagging_shared01() { 1067 svc := s3.New(session.New()) 1068 input := &s3.GetObjectTaggingInput{ 1069 Bucket: aws.String("examplebucket"), 1070 Key: aws.String("HappyFace.jpg"), 1071 } 1072 1073 result, err := svc.GetObjectTagging(input) 1074 if err != nil { 1075 if aerr, ok := err.(awserr.Error); ok { 1076 switch aerr.Code() { 1077 default: 1078 fmt.Println(aerr.Error()) 1079 } 1080 } else { 1081 // Print the error, cast err to awserr.Error to get the Code and 1082 // Message from an error. 1083 fmt.Println(err.Error()) 1084 } 1085 return 1086 } 1087 1088 fmt.Println(result) 1089} 1090 1091// To retrieve torrent files for an object 1092// 1093// The following example retrieves torrent files of an object. 1094func ExampleS3_GetObjectTorrent_shared00() { 1095 svc := s3.New(session.New()) 1096 input := &s3.GetObjectTorrentInput{ 1097 Bucket: aws.String("examplebucket"), 1098 Key: aws.String("HappyFace.jpg"), 1099 } 1100 1101 result, err := svc.GetObjectTorrent(input) 1102 if err != nil { 1103 if aerr, ok := err.(awserr.Error); ok { 1104 switch aerr.Code() { 1105 default: 1106 fmt.Println(aerr.Error()) 1107 } 1108 } else { 1109 // Print the error, cast err to awserr.Error to get the Code and 1110 // Message from an error. 1111 fmt.Println(err.Error()) 1112 } 1113 return 1114 } 1115 1116 fmt.Println(result) 1117} 1118 1119// To determine if bucket exists 1120// 1121// This operation checks to see if a bucket exists. 1122func ExampleS3_HeadBucket_shared00() { 1123 svc := s3.New(session.New()) 1124 input := &s3.HeadBucketInput{ 1125 Bucket: aws.String("acl1"), 1126 } 1127 1128 result, err := svc.HeadBucket(input) 1129 if err != nil { 1130 if aerr, ok := err.(awserr.Error); ok { 1131 switch aerr.Code() { 1132 case s3.ErrCodeNoSuchBucket: 1133 fmt.Println(s3.ErrCodeNoSuchBucket, aerr.Error()) 1134 default: 1135 fmt.Println(aerr.Error()) 1136 } 1137 } else { 1138 // Print the error, cast err to awserr.Error to get the Code and 1139 // Message from an error. 1140 fmt.Println(err.Error()) 1141 } 1142 return 1143 } 1144 1145 fmt.Println(result) 1146} 1147 1148// To retrieve metadata of an object without returning the object itself 1149// 1150// The following example retrieves an object metadata. 1151func ExampleS3_HeadObject_shared00() { 1152 svc := s3.New(session.New()) 1153 input := &s3.HeadObjectInput{ 1154 Bucket: aws.String("examplebucket"), 1155 Key: aws.String("HappyFace.jpg"), 1156 } 1157 1158 result, err := svc.HeadObject(input) 1159 if err != nil { 1160 if aerr, ok := err.(awserr.Error); ok { 1161 switch aerr.Code() { 1162 default: 1163 fmt.Println(aerr.Error()) 1164 } 1165 } else { 1166 // Print the error, cast err to awserr.Error to get the Code and 1167 // Message from an error. 1168 fmt.Println(err.Error()) 1169 } 1170 return 1171 } 1172 1173 fmt.Println(result) 1174} 1175 1176// To list object versions 1177// 1178// The following example return versions of an object with specific key name prefix. 1179// The request limits the number of items returned to two. If there are are more than 1180// two object version, S3 returns NextToken in the response. You can specify this token 1181// value in your next request to fetch next set of object versions. 1182func ExampleS3_ListBuckets_shared00() { 1183 svc := s3.New(session.New()) 1184 input := &s3.ListBucketsInput{} 1185 1186 result, err := svc.ListBuckets(input) 1187 if err != nil { 1188 if aerr, ok := err.(awserr.Error); ok { 1189 switch aerr.Code() { 1190 default: 1191 fmt.Println(aerr.Error()) 1192 } 1193 } else { 1194 // Print the error, cast err to awserr.Error to get the Code and 1195 // Message from an error. 1196 fmt.Println(err.Error()) 1197 } 1198 return 1199 } 1200 1201 fmt.Println(result) 1202} 1203 1204// To list in-progress multipart uploads on a bucket 1205// 1206// The following example lists in-progress multipart uploads on a specific bucket. 1207func ExampleS3_ListMultipartUploads_shared00() { 1208 svc := s3.New(session.New()) 1209 input := &s3.ListMultipartUploadsInput{ 1210 Bucket: aws.String("examplebucket"), 1211 } 1212 1213 result, err := svc.ListMultipartUploads(input) 1214 if err != nil { 1215 if aerr, ok := err.(awserr.Error); ok { 1216 switch aerr.Code() { 1217 default: 1218 fmt.Println(aerr.Error()) 1219 } 1220 } else { 1221 // Print the error, cast err to awserr.Error to get the Code and 1222 // Message from an error. 1223 fmt.Println(err.Error()) 1224 } 1225 return 1226 } 1227 1228 fmt.Println(result) 1229} 1230 1231// List next set of multipart uploads when previous result is truncated 1232// 1233// The following example specifies the upload-id-marker and key-marker from previous 1234// truncated response to retrieve next setup of multipart uploads. 1235func ExampleS3_ListMultipartUploads_shared01() { 1236 svc := s3.New(session.New()) 1237 input := &s3.ListMultipartUploadsInput{ 1238 Bucket: aws.String("examplebucket"), 1239 KeyMarker: aws.String("nextkeyfrompreviousresponse"), 1240 MaxUploads: aws.Int64(2), 1241 UploadIdMarker: aws.String("valuefrompreviousresponse"), 1242 } 1243 1244 result, err := svc.ListMultipartUploads(input) 1245 if err != nil { 1246 if aerr, ok := err.(awserr.Error); ok { 1247 switch aerr.Code() { 1248 default: 1249 fmt.Println(aerr.Error()) 1250 } 1251 } else { 1252 // Print the error, cast err to awserr.Error to get the Code and 1253 // Message from an error. 1254 fmt.Println(err.Error()) 1255 } 1256 return 1257 } 1258 1259 fmt.Println(result) 1260} 1261 1262// To list object versions 1263// 1264// The following example return versions of an object with specific key name prefix. 1265// The request limits the number of items returned to two. If there are are more than 1266// two object version, S3 returns NextToken in the response. You can specify this token 1267// value in your next request to fetch next set of object versions. 1268func ExampleS3_ListObjectVersions_shared00() { 1269 svc := s3.New(session.New()) 1270 input := &s3.ListObjectVersionsInput{ 1271 Bucket: aws.String("examplebucket"), 1272 Prefix: aws.String("HappyFace.jpg"), 1273 } 1274 1275 result, err := svc.ListObjectVersions(input) 1276 if err != nil { 1277 if aerr, ok := err.(awserr.Error); ok { 1278 switch aerr.Code() { 1279 default: 1280 fmt.Println(aerr.Error()) 1281 } 1282 } else { 1283 // Print the error, cast err to awserr.Error to get the Code and 1284 // Message from an error. 1285 fmt.Println(err.Error()) 1286 } 1287 return 1288 } 1289 1290 fmt.Println(result) 1291} 1292 1293// To list objects in a bucket 1294// 1295// The following example list two objects in a bucket. 1296func ExampleS3_ListObjects_shared00() { 1297 svc := s3.New(session.New()) 1298 input := &s3.ListObjectsInput{ 1299 Bucket: aws.String("examplebucket"), 1300 MaxKeys: aws.Int64(2), 1301 } 1302 1303 result, err := svc.ListObjects(input) 1304 if err != nil { 1305 if aerr, ok := err.(awserr.Error); ok { 1306 switch aerr.Code() { 1307 case s3.ErrCodeNoSuchBucket: 1308 fmt.Println(s3.ErrCodeNoSuchBucket, aerr.Error()) 1309 default: 1310 fmt.Println(aerr.Error()) 1311 } 1312 } else { 1313 // Print the error, cast err to awserr.Error to get the Code and 1314 // Message from an error. 1315 fmt.Println(err.Error()) 1316 } 1317 return 1318 } 1319 1320 fmt.Println(result) 1321} 1322 1323// To get object list 1324// 1325// The following example retrieves object list. The request specifies max keys to limit 1326// response to include only 2 object keys. 1327func ExampleS3_ListObjectsV2_shared00() { 1328 svc := s3.New(session.New()) 1329 input := &s3.ListObjectsV2Input{ 1330 Bucket: aws.String("examplebucket"), 1331 MaxKeys: aws.Int64(2), 1332 } 1333 1334 result, err := svc.ListObjectsV2(input) 1335 if err != nil { 1336 if aerr, ok := err.(awserr.Error); ok { 1337 switch aerr.Code() { 1338 case s3.ErrCodeNoSuchBucket: 1339 fmt.Println(s3.ErrCodeNoSuchBucket, aerr.Error()) 1340 default: 1341 fmt.Println(aerr.Error()) 1342 } 1343 } else { 1344 // Print the error, cast err to awserr.Error to get the Code and 1345 // Message from an error. 1346 fmt.Println(err.Error()) 1347 } 1348 return 1349 } 1350 1351 fmt.Println(result) 1352} 1353 1354// To list parts of a multipart upload. 1355// 1356// The following example lists parts uploaded for a specific multipart upload. 1357func ExampleS3_ListParts_shared00() { 1358 svc := s3.New(session.New()) 1359 input := &s3.ListPartsInput{ 1360 Bucket: aws.String("examplebucket"), 1361 Key: aws.String("bigobject"), 1362 UploadId: aws.String("example7YPBOJuoFiQ9cz4P3Pe6FIZwO4f7wN93uHsNBEw97pl5eNwzExg0LAT2dUN91cOmrEQHDsP3WA60CEg--"), 1363 } 1364 1365 result, err := svc.ListParts(input) 1366 if err != nil { 1367 if aerr, ok := err.(awserr.Error); ok { 1368 switch aerr.Code() { 1369 default: 1370 fmt.Println(aerr.Error()) 1371 } 1372 } else { 1373 // Print the error, cast err to awserr.Error to get the Code and 1374 // Message from an error. 1375 fmt.Println(err.Error()) 1376 } 1377 return 1378 } 1379 1380 fmt.Println(result) 1381} 1382 1383// Put bucket acl 1384// 1385// The following example replaces existing ACL on a bucket. The ACL grants the bucket 1386// owner (specified using the owner ID) and write permission to the LogDelivery group. 1387// Because this is a replace operation, you must specify all the grants in your request. 1388// To incrementally add or remove ACL grants, you might use the console. 1389func ExampleS3_PutBucketAcl_shared00() { 1390 svc := s3.New(session.New()) 1391 input := &s3.PutBucketAclInput{ 1392 Bucket: aws.String("examplebucket"), 1393 GrantFullControl: aws.String("id=examplee7a2f25102679df27bb0ae12b3f85be6f290b936c4393484"), 1394 GrantWrite: aws.String("uri=http://acs.amazonaws.com/groups/s3/LogDelivery"), 1395 } 1396 1397 result, err := svc.PutBucketAcl(input) 1398 if err != nil { 1399 if aerr, ok := err.(awserr.Error); ok { 1400 switch aerr.Code() { 1401 default: 1402 fmt.Println(aerr.Error()) 1403 } 1404 } else { 1405 // Print the error, cast err to awserr.Error to get the Code and 1406 // Message from an error. 1407 fmt.Println(err.Error()) 1408 } 1409 return 1410 } 1411 1412 fmt.Println(result) 1413} 1414 1415// To set cors configuration on a bucket. 1416// 1417// The following example enables PUT, POST, and DELETE requests from www.example.com, 1418// and enables GET requests from any domain. 1419func ExampleS3_PutBucketCors_shared00() { 1420 svc := s3.New(session.New()) 1421 input := &s3.PutBucketCorsInput{ 1422 Bucket: aws.String(""), 1423 CORSConfiguration: &s3.CORSConfiguration{ 1424 CORSRules: []*s3.CORSRule{ 1425 { 1426 AllowedHeaders: []*string{ 1427 aws.String("*"), 1428 }, 1429 AllowedMethods: []*string{ 1430 aws.String("PUT"), 1431 aws.String("POST"), 1432 aws.String("DELETE"), 1433 }, 1434 AllowedOrigins: []*string{ 1435 aws.String("http://www.example.com"), 1436 }, 1437 ExposeHeaders: []*string{ 1438 aws.String("x-amz-server-side-encryption"), 1439 }, 1440 MaxAgeSeconds: aws.Int64(3000), 1441 }, 1442 { 1443 AllowedHeaders: []*string{ 1444 aws.String("Authorization"), 1445 }, 1446 AllowedMethods: []*string{ 1447 aws.String("GET"), 1448 }, 1449 AllowedOrigins: []*string{ 1450 aws.String("*"), 1451 }, 1452 MaxAgeSeconds: aws.Int64(3000), 1453 }, 1454 }, 1455 }, 1456 } 1457 1458 result, err := svc.PutBucketCors(input) 1459 if err != nil { 1460 if aerr, ok := err.(awserr.Error); ok { 1461 switch aerr.Code() { 1462 default: 1463 fmt.Println(aerr.Error()) 1464 } 1465 } else { 1466 // Print the error, cast err to awserr.Error to get the Code and 1467 // Message from an error. 1468 fmt.Println(err.Error()) 1469 } 1470 return 1471 } 1472 1473 fmt.Println(result) 1474} 1475 1476// Put bucket lifecycle 1477// 1478// The following example replaces existing lifecycle configuration, if any, on the specified 1479// bucket. 1480func ExampleS3_PutBucketLifecycleConfiguration_shared00() { 1481 svc := s3.New(session.New()) 1482 input := &s3.PutBucketLifecycleConfigurationInput{ 1483 Bucket: aws.String("examplebucket"), 1484 LifecycleConfiguration: &s3.BucketLifecycleConfiguration{ 1485 Rules: []*s3.LifecycleRule{ 1486 { 1487 Expiration: &s3.LifecycleExpiration{ 1488 Days: aws.Int64(3650), 1489 }, 1490 Filter: &s3.LifecycleRuleFilter{ 1491 Prefix: aws.String("documents/"), 1492 }, 1493 ID: aws.String("TestOnly"), 1494 Status: aws.String("Enabled"), 1495 Transitions: []*s3.Transition{ 1496 { 1497 Days: aws.Int64(365), 1498 StorageClass: aws.String("GLACIER"), 1499 }, 1500 }, 1501 }, 1502 }, 1503 }, 1504 } 1505 1506 result, err := svc.PutBucketLifecycleConfiguration(input) 1507 if err != nil { 1508 if aerr, ok := err.(awserr.Error); ok { 1509 switch aerr.Code() { 1510 default: 1511 fmt.Println(aerr.Error()) 1512 } 1513 } else { 1514 // Print the error, cast err to awserr.Error to get the Code and 1515 // Message from an error. 1516 fmt.Println(err.Error()) 1517 } 1518 return 1519 } 1520 1521 fmt.Println(result) 1522} 1523 1524// Set logging configuration for a bucket 1525// 1526// The following example sets logging policy on a bucket. For the Log Delivery group 1527// to deliver logs to the destination bucket, it needs permission for the READ_ACP action 1528// which the policy grants. 1529func ExampleS3_PutBucketLogging_shared00() { 1530 svc := s3.New(session.New()) 1531 input := &s3.PutBucketLoggingInput{ 1532 Bucket: aws.String("sourcebucket"), 1533 BucketLoggingStatus: &s3.BucketLoggingStatus{ 1534 LoggingEnabled: &s3.LoggingEnabled{ 1535 TargetBucket: aws.String("targetbucket"), 1536 TargetGrants: []*s3.TargetGrant{ 1537 { 1538 Grantee: &s3.Grantee{ 1539 Type: aws.String("Group"), 1540 URI: aws.String("http://acs.amazonaws.com/groups/global/AllUsers"), 1541 }, 1542 Permission: aws.String("READ"), 1543 }, 1544 }, 1545 TargetPrefix: aws.String("MyBucketLogs/"), 1546 }, 1547 }, 1548 } 1549 1550 result, err := svc.PutBucketLogging(input) 1551 if err != nil { 1552 if aerr, ok := err.(awserr.Error); ok { 1553 switch aerr.Code() { 1554 default: 1555 fmt.Println(aerr.Error()) 1556 } 1557 } else { 1558 // Print the error, cast err to awserr.Error to get the Code and 1559 // Message from an error. 1560 fmt.Println(err.Error()) 1561 } 1562 return 1563 } 1564 1565 fmt.Println(result) 1566} 1567 1568// Set notification configuration for a bucket 1569// 1570// The following example sets notification configuration on a bucket to publish the 1571// object created events to an SNS topic. 1572func ExampleS3_PutBucketNotificationConfiguration_shared00() { 1573 svc := s3.New(session.New()) 1574 input := &s3.PutBucketNotificationConfigurationInput{ 1575 Bucket: aws.String("examplebucket"), 1576 NotificationConfiguration: &s3.NotificationConfiguration{ 1577 TopicConfigurations: []*s3.TopicConfiguration{ 1578 { 1579 Events: []*string{ 1580 aws.String("s3:ObjectCreated:*"), 1581 }, 1582 TopicArn: aws.String("arn:aws:sns:us-west-2:123456789012:s3-notification-topic"), 1583 }, 1584 }, 1585 }, 1586 } 1587 1588 result, err := svc.PutBucketNotificationConfiguration(input) 1589 if err != nil { 1590 if aerr, ok := err.(awserr.Error); ok { 1591 switch aerr.Code() { 1592 default: 1593 fmt.Println(aerr.Error()) 1594 } 1595 } else { 1596 // Print the error, cast err to awserr.Error to get the Code and 1597 // Message from an error. 1598 fmt.Println(err.Error()) 1599 } 1600 return 1601 } 1602 1603 fmt.Println(result) 1604} 1605 1606// Set bucket policy 1607// 1608// The following example sets a permission policy on a bucket. 1609func ExampleS3_PutBucketPolicy_shared00() { 1610 svc := s3.New(session.New()) 1611 input := &s3.PutBucketPolicyInput{ 1612 Bucket: aws.String("examplebucket"), 1613 Policy: aws.String("{\"Version\": \"2012-10-17\", \"Statement\": [{ \"Sid\": \"id-1\",\"Effect\": \"Allow\",\"Principal\": {\"AWS\": \"arn:aws:iam::123456789012:root\"}, \"Action\": [ \"s3:PutObject\",\"s3:PutObjectAcl\"], \"Resource\": [\"arn:aws:s3:::acl3/*\" ] } ]}"), 1614 } 1615 1616 result, err := svc.PutBucketPolicy(input) 1617 if err != nil { 1618 if aerr, ok := err.(awserr.Error); ok { 1619 switch aerr.Code() { 1620 default: 1621 fmt.Println(aerr.Error()) 1622 } 1623 } else { 1624 // Print the error, cast err to awserr.Error to get the Code and 1625 // Message from an error. 1626 fmt.Println(err.Error()) 1627 } 1628 return 1629 } 1630 1631 fmt.Println(result) 1632} 1633 1634// Set replication configuration on a bucket 1635// 1636// The following example sets replication configuration on a bucket. 1637func ExampleS3_PutBucketReplication_shared00() { 1638 svc := s3.New(session.New()) 1639 input := &s3.PutBucketReplicationInput{ 1640 Bucket: aws.String("examplebucket"), 1641 ReplicationConfiguration: &s3.ReplicationConfiguration{ 1642 Role: aws.String("arn:aws:iam::123456789012:role/examplerole"), 1643 Rules: []*s3.ReplicationRule{ 1644 { 1645 Destination: &s3.Destination{ 1646 Bucket: aws.String("arn:aws:s3:::destinationbucket"), 1647 StorageClass: aws.String("STANDARD"), 1648 }, 1649 Prefix: aws.String(""), 1650 Status: aws.String("Enabled"), 1651 }, 1652 }, 1653 }, 1654 } 1655 1656 result, err := svc.PutBucketReplication(input) 1657 if err != nil { 1658 if aerr, ok := err.(awserr.Error); ok { 1659 switch aerr.Code() { 1660 default: 1661 fmt.Println(aerr.Error()) 1662 } 1663 } else { 1664 // Print the error, cast err to awserr.Error to get the Code and 1665 // Message from an error. 1666 fmt.Println(err.Error()) 1667 } 1668 return 1669 } 1670 1671 fmt.Println(result) 1672} 1673 1674// Set request payment configuration on a bucket. 1675// 1676// The following example sets request payment configuration on a bucket so that person 1677// requesting the download is charged. 1678func ExampleS3_PutBucketRequestPayment_shared00() { 1679 svc := s3.New(session.New()) 1680 input := &s3.PutBucketRequestPaymentInput{ 1681 Bucket: aws.String("examplebucket"), 1682 RequestPaymentConfiguration: &s3.RequestPaymentConfiguration{ 1683 Payer: aws.String("Requester"), 1684 }, 1685 } 1686 1687 result, err := svc.PutBucketRequestPayment(input) 1688 if err != nil { 1689 if aerr, ok := err.(awserr.Error); ok { 1690 switch aerr.Code() { 1691 default: 1692 fmt.Println(aerr.Error()) 1693 } 1694 } else { 1695 // Print the error, cast err to awserr.Error to get the Code and 1696 // Message from an error. 1697 fmt.Println(err.Error()) 1698 } 1699 return 1700 } 1701 1702 fmt.Println(result) 1703} 1704 1705// Set tags on a bucket 1706// 1707// The following example sets tags on a bucket. Any existing tags are replaced. 1708func ExampleS3_PutBucketTagging_shared00() { 1709 svc := s3.New(session.New()) 1710 input := &s3.PutBucketTaggingInput{ 1711 Bucket: aws.String("examplebucket"), 1712 Tagging: &s3.Tagging{ 1713 TagSet: []*s3.Tag{ 1714 { 1715 Key: aws.String("Key1"), 1716 Value: aws.String("Value1"), 1717 }, 1718 { 1719 Key: aws.String("Key2"), 1720 Value: aws.String("Value2"), 1721 }, 1722 }, 1723 }, 1724 } 1725 1726 result, err := svc.PutBucketTagging(input) 1727 if err != nil { 1728 if aerr, ok := err.(awserr.Error); ok { 1729 switch aerr.Code() { 1730 default: 1731 fmt.Println(aerr.Error()) 1732 } 1733 } else { 1734 // Print the error, cast err to awserr.Error to get the Code and 1735 // Message from an error. 1736 fmt.Println(err.Error()) 1737 } 1738 return 1739 } 1740 1741 fmt.Println(result) 1742} 1743 1744// Set versioning configuration on a bucket 1745// 1746// The following example sets versioning configuration on bucket. The configuration 1747// enables versioning on the bucket. 1748func ExampleS3_PutBucketVersioning_shared00() { 1749 svc := s3.New(session.New()) 1750 input := &s3.PutBucketVersioningInput{ 1751 Bucket: aws.String("examplebucket"), 1752 VersioningConfiguration: &s3.VersioningConfiguration{ 1753 MFADelete: aws.String("Disabled"), 1754 Status: aws.String("Enabled"), 1755 }, 1756 } 1757 1758 result, err := svc.PutBucketVersioning(input) 1759 if err != nil { 1760 if aerr, ok := err.(awserr.Error); ok { 1761 switch aerr.Code() { 1762 default: 1763 fmt.Println(aerr.Error()) 1764 } 1765 } else { 1766 // Print the error, cast err to awserr.Error to get the Code and 1767 // Message from an error. 1768 fmt.Println(err.Error()) 1769 } 1770 return 1771 } 1772 1773 fmt.Println(result) 1774} 1775 1776// Set website configuration on a bucket 1777// 1778// The following example adds website configuration to a bucket. 1779func ExampleS3_PutBucketWebsite_shared00() { 1780 svc := s3.New(session.New()) 1781 input := &s3.PutBucketWebsiteInput{ 1782 Bucket: aws.String("examplebucket"), 1783 WebsiteConfiguration: &s3.WebsiteConfiguration{ 1784 ErrorDocument: &s3.ErrorDocument{ 1785 Key: aws.String("error.html"), 1786 }, 1787 IndexDocument: &s3.IndexDocument{ 1788 Suffix: aws.String("index.html"), 1789 }, 1790 }, 1791 } 1792 1793 result, err := svc.PutBucketWebsite(input) 1794 if err != nil { 1795 if aerr, ok := err.(awserr.Error); ok { 1796 switch aerr.Code() { 1797 default: 1798 fmt.Println(aerr.Error()) 1799 } 1800 } else { 1801 // Print the error, cast err to awserr.Error to get the Code and 1802 // Message from an error. 1803 fmt.Println(err.Error()) 1804 } 1805 return 1806 } 1807 1808 fmt.Println(result) 1809} 1810 1811// To upload an object and specify optional tags 1812// 1813// The following example uploads an object. The request specifies optional object tags. 1814// The bucket is versioned, therefore S3 returns version ID of the newly created object. 1815func ExampleS3_PutObject_shared00() { 1816 svc := s3.New(session.New()) 1817 input := &s3.PutObjectInput{ 1818 Body: aws.ReadSeekCloser(strings.NewReader("c:\\HappyFace.jpg")), 1819 Bucket: aws.String("examplebucket"), 1820 Key: aws.String("HappyFace.jpg"), 1821 Tagging: aws.String("key1=value1&key2=value2"), 1822 } 1823 1824 result, err := svc.PutObject(input) 1825 if err != nil { 1826 if aerr, ok := err.(awserr.Error); ok { 1827 switch aerr.Code() { 1828 default: 1829 fmt.Println(aerr.Error()) 1830 } 1831 } else { 1832 // Print the error, cast err to awserr.Error to get the Code and 1833 // Message from an error. 1834 fmt.Println(err.Error()) 1835 } 1836 return 1837 } 1838 1839 fmt.Println(result) 1840} 1841 1842// To upload an object and specify canned ACL. 1843// 1844// The following example uploads and object. The request specifies optional canned ACL 1845// (access control list) to all READ access to authenticated users. If the bucket is 1846// versioning enabled, S3 returns version ID in response. 1847func ExampleS3_PutObject_shared01() { 1848 svc := s3.New(session.New()) 1849 input := &s3.PutObjectInput{ 1850 ACL: aws.String("authenticated-read"), 1851 Body: aws.ReadSeekCloser(strings.NewReader("filetoupload")), 1852 Bucket: aws.String("examplebucket"), 1853 Key: aws.String("exampleobject"), 1854 } 1855 1856 result, err := svc.PutObject(input) 1857 if err != nil { 1858 if aerr, ok := err.(awserr.Error); ok { 1859 switch aerr.Code() { 1860 default: 1861 fmt.Println(aerr.Error()) 1862 } 1863 } else { 1864 // Print the error, cast err to awserr.Error to get the Code and 1865 // Message from an error. 1866 fmt.Println(err.Error()) 1867 } 1868 return 1869 } 1870 1871 fmt.Println(result) 1872} 1873 1874// To upload an object and specify server-side encryption and object tags 1875// 1876// The following example uploads and object. The request specifies the optional server-side 1877// encryption option. The request also specifies optional object tags. If the bucket 1878// is versioning enabled, S3 returns version ID in response. 1879func ExampleS3_PutObject_shared02() { 1880 svc := s3.New(session.New()) 1881 input := &s3.PutObjectInput{ 1882 Body: aws.ReadSeekCloser(strings.NewReader("filetoupload")), 1883 Bucket: aws.String("examplebucket"), 1884 Key: aws.String("exampleobject"), 1885 ServerSideEncryption: aws.String("AES256"), 1886 Tagging: aws.String("key1=value1&key2=value2"), 1887 } 1888 1889 result, err := svc.PutObject(input) 1890 if err != nil { 1891 if aerr, ok := err.(awserr.Error); ok { 1892 switch aerr.Code() { 1893 default: 1894 fmt.Println(aerr.Error()) 1895 } 1896 } else { 1897 // Print the error, cast err to awserr.Error to get the Code and 1898 // Message from an error. 1899 fmt.Println(err.Error()) 1900 } 1901 return 1902 } 1903 1904 fmt.Println(result) 1905} 1906 1907// To create an object. 1908// 1909// The following example creates an object. If the bucket is versioning enabled, S3 1910// returns version ID in response. 1911func ExampleS3_PutObject_shared03() { 1912 svc := s3.New(session.New()) 1913 input := &s3.PutObjectInput{ 1914 Body: aws.ReadSeekCloser(strings.NewReader("filetoupload")), 1915 Bucket: aws.String("examplebucket"), 1916 Key: aws.String("objectkey"), 1917 } 1918 1919 result, err := svc.PutObject(input) 1920 if err != nil { 1921 if aerr, ok := err.(awserr.Error); ok { 1922 switch aerr.Code() { 1923 default: 1924 fmt.Println(aerr.Error()) 1925 } 1926 } else { 1927 // Print the error, cast err to awserr.Error to get the Code and 1928 // Message from an error. 1929 fmt.Println(err.Error()) 1930 } 1931 return 1932 } 1933 1934 fmt.Println(result) 1935} 1936 1937// To upload an object 1938// 1939// The following example uploads an object to a versioning-enabled bucket. The source 1940// file is specified using Windows file syntax. S3 returns VersionId of the newly created 1941// object. 1942func ExampleS3_PutObject_shared04() { 1943 svc := s3.New(session.New()) 1944 input := &s3.PutObjectInput{ 1945 Body: aws.ReadSeekCloser(strings.NewReader("HappyFace.jpg")), 1946 Bucket: aws.String("examplebucket"), 1947 Key: aws.String("HappyFace.jpg"), 1948 } 1949 1950 result, err := svc.PutObject(input) 1951 if err != nil { 1952 if aerr, ok := err.(awserr.Error); ok { 1953 switch aerr.Code() { 1954 default: 1955 fmt.Println(aerr.Error()) 1956 } 1957 } else { 1958 // Print the error, cast err to awserr.Error to get the Code and 1959 // Message from an error. 1960 fmt.Println(err.Error()) 1961 } 1962 return 1963 } 1964 1965 fmt.Println(result) 1966} 1967 1968// To upload an object (specify optional headers) 1969// 1970// The following example uploads an object. The request specifies optional request headers 1971// to directs S3 to use specific storage class and use server-side encryption. 1972func ExampleS3_PutObject_shared05() { 1973 svc := s3.New(session.New()) 1974 input := &s3.PutObjectInput{ 1975 Body: aws.ReadSeekCloser(strings.NewReader("HappyFace.jpg")), 1976 Bucket: aws.String("examplebucket"), 1977 Key: aws.String("HappyFace.jpg"), 1978 ServerSideEncryption: aws.String("AES256"), 1979 StorageClass: aws.String("STANDARD_IA"), 1980 } 1981 1982 result, err := svc.PutObject(input) 1983 if err != nil { 1984 if aerr, ok := err.(awserr.Error); ok { 1985 switch aerr.Code() { 1986 default: 1987 fmt.Println(aerr.Error()) 1988 } 1989 } else { 1990 // Print the error, cast err to awserr.Error to get the Code and 1991 // Message from an error. 1992 fmt.Println(err.Error()) 1993 } 1994 return 1995 } 1996 1997 fmt.Println(result) 1998} 1999 2000// To upload object and specify user-defined metadata 2001// 2002// The following example creates an object. The request also specifies optional metadata. 2003// If the bucket is versioning enabled, S3 returns version ID in response. 2004func ExampleS3_PutObject_shared06() { 2005 svc := s3.New(session.New()) 2006 input := &s3.PutObjectInput{ 2007 Body: aws.ReadSeekCloser(strings.NewReader("filetoupload")), 2008 Bucket: aws.String("examplebucket"), 2009 Key: aws.String("exampleobject"), 2010 Metadata: map[string]*string{ 2011 "metadata1": aws.String("value1"), 2012 "metadata2": aws.String("value2"), 2013 }, 2014 } 2015 2016 result, err := svc.PutObject(input) 2017 if err != nil { 2018 if aerr, ok := err.(awserr.Error); ok { 2019 switch aerr.Code() { 2020 default: 2021 fmt.Println(aerr.Error()) 2022 } 2023 } else { 2024 // Print the error, cast err to awserr.Error to get the Code and 2025 // Message from an error. 2026 fmt.Println(err.Error()) 2027 } 2028 return 2029 } 2030 2031 fmt.Println(result) 2032} 2033 2034// To grant permissions using object ACL 2035// 2036// The following example adds grants to an object ACL. The first permission grants user1 2037// and user2 FULL_CONTROL and the AllUsers group READ permission. 2038func ExampleS3_PutObjectAcl_shared00() { 2039 svc := s3.New(session.New()) 2040 input := &s3.PutObjectAclInput{ 2041 AccessControlPolicy: &s3.AccessControlPolicy{}, 2042 Bucket: aws.String("examplebucket"), 2043 GrantFullControl: aws.String("emailaddress=user1@example.com,emailaddress=user2@example.com"), 2044 GrantRead: aws.String("uri=http://acs.amazonaws.com/groups/global/AllUsers"), 2045 Key: aws.String("HappyFace.jpg"), 2046 } 2047 2048 result, err := svc.PutObjectAcl(input) 2049 if err != nil { 2050 if aerr, ok := err.(awserr.Error); ok { 2051 switch aerr.Code() { 2052 case s3.ErrCodeNoSuchKey: 2053 fmt.Println(s3.ErrCodeNoSuchKey, aerr.Error()) 2054 default: 2055 fmt.Println(aerr.Error()) 2056 } 2057 } else { 2058 // Print the error, cast err to awserr.Error to get the Code and 2059 // Message from an error. 2060 fmt.Println(err.Error()) 2061 } 2062 return 2063 } 2064 2065 fmt.Println(result) 2066} 2067 2068// To add tags to an existing object 2069// 2070// The following example adds tags to an existing object. 2071func ExampleS3_PutObjectTagging_shared00() { 2072 svc := s3.New(session.New()) 2073 input := &s3.PutObjectTaggingInput{ 2074 Bucket: aws.String("examplebucket"), 2075 Key: aws.String("HappyFace.jpg"), 2076 Tagging: &s3.Tagging{ 2077 TagSet: []*s3.Tag{ 2078 { 2079 Key: aws.String("Key3"), 2080 Value: aws.String("Value3"), 2081 }, 2082 { 2083 Key: aws.String("Key4"), 2084 Value: aws.String("Value4"), 2085 }, 2086 }, 2087 }, 2088 } 2089 2090 result, err := svc.PutObjectTagging(input) 2091 if err != nil { 2092 if aerr, ok := err.(awserr.Error); ok { 2093 switch aerr.Code() { 2094 default: 2095 fmt.Println(aerr.Error()) 2096 } 2097 } else { 2098 // Print the error, cast err to awserr.Error to get the Code and 2099 // Message from an error. 2100 fmt.Println(err.Error()) 2101 } 2102 return 2103 } 2104 2105 fmt.Println(result) 2106} 2107 2108// To restore an archived object 2109// 2110// The following example restores for one day an archived copy of an object back into 2111// Amazon S3 bucket. 2112func ExampleS3_RestoreObject_shared00() { 2113 svc := s3.New(session.New()) 2114 input := &s3.RestoreObjectInput{ 2115 Bucket: aws.String("examplebucket"), 2116 Key: aws.String("archivedobjectkey"), 2117 RestoreRequest: &s3.RestoreRequest{ 2118 Days: aws.Int64(1), 2119 GlacierJobParameters: &s3.GlacierJobParameters{ 2120 Tier: aws.String("Expedited"), 2121 }, 2122 }, 2123 } 2124 2125 result, err := svc.RestoreObject(input) 2126 if err != nil { 2127 if aerr, ok := err.(awserr.Error); ok { 2128 switch aerr.Code() { 2129 case s3.ErrCodeObjectAlreadyInActiveTierError: 2130 fmt.Println(s3.ErrCodeObjectAlreadyInActiveTierError, aerr.Error()) 2131 default: 2132 fmt.Println(aerr.Error()) 2133 } 2134 } else { 2135 // Print the error, cast err to awserr.Error to get the Code and 2136 // Message from an error. 2137 fmt.Println(err.Error()) 2138 } 2139 return 2140 } 2141 2142 fmt.Println(result) 2143} 2144 2145// To upload a part 2146// 2147// The following example uploads part 1 of a multipart upload. The example specifies 2148// a file name for the part data. The Upload ID is same that is returned by the initiate 2149// multipart upload. 2150func ExampleS3_UploadPart_shared00() { 2151 svc := s3.New(session.New()) 2152 input := &s3.UploadPartInput{ 2153 Body: aws.ReadSeekCloser(strings.NewReader("fileToUpload")), 2154 Bucket: aws.String("examplebucket"), 2155 Key: aws.String("examplelargeobject"), 2156 PartNumber: aws.Int64(1), 2157 UploadId: aws.String("xadcOB_7YPBOJuoFiQ9cz4P3Pe6FIZwO4f7wN93uHsNBEw97pl5eNwzExg0LAT2dUN91cOmrEQHDsP3WA60CEg--"), 2158 } 2159 2160 result, err := svc.UploadPart(input) 2161 if err != nil { 2162 if aerr, ok := err.(awserr.Error); ok { 2163 switch aerr.Code() { 2164 default: 2165 fmt.Println(aerr.Error()) 2166 } 2167 } else { 2168 // Print the error, cast err to awserr.Error to get the Code and 2169 // Message from an error. 2170 fmt.Println(err.Error()) 2171 } 2172 return 2173 } 2174 2175 fmt.Println(result) 2176} 2177 2178// To upload a part by copying data from an existing object as data source 2179// 2180// The following example uploads a part of a multipart upload by copying data from an 2181// existing object as data source. 2182func ExampleS3_UploadPartCopy_shared00() { 2183 svc := s3.New(session.New()) 2184 input := &s3.UploadPartCopyInput{ 2185 Bucket: aws.String("examplebucket"), 2186 CopySource: aws.String("/bucketname/sourceobjectkey"), 2187 Key: aws.String("examplelargeobject"), 2188 PartNumber: aws.Int64(1), 2189 UploadId: aws.String("exampleuoh_10OhKhT7YukE9bjzTPRiuaCotmZM_pFngJFir9OZNrSr5cWa3cq3LZSUsfjI4FI7PkP91We7Nrw--"), 2190 } 2191 2192 result, err := svc.UploadPartCopy(input) 2193 if err != nil { 2194 if aerr, ok := err.(awserr.Error); ok { 2195 switch aerr.Code() { 2196 default: 2197 fmt.Println(aerr.Error()) 2198 } 2199 } else { 2200 // Print the error, cast err to awserr.Error to get the Code and 2201 // Message from an error. 2202 fmt.Println(err.Error()) 2203 } 2204 return 2205 } 2206 2207 fmt.Println(result) 2208} 2209 2210// To upload a part by copying byte range from an existing object as data source 2211// 2212// The following example uploads a part of a multipart upload by copying a specified 2213// byte range from an existing object as data source. 2214func ExampleS3_UploadPartCopy_shared01() { 2215 svc := s3.New(session.New()) 2216 input := &s3.UploadPartCopyInput{ 2217 Bucket: aws.String("examplebucket"), 2218 CopySource: aws.String("/bucketname/sourceobjectkey"), 2219 CopySourceRange: aws.String("bytes=1-100000"), 2220 Key: aws.String("examplelargeobject"), 2221 PartNumber: aws.Int64(2), 2222 UploadId: aws.String("exampleuoh_10OhKhT7YukE9bjzTPRiuaCotmZM_pFngJFir9OZNrSr5cWa3cq3LZSUsfjI4FI7PkP91We7Nrw--"), 2223 } 2224 2225 result, err := svc.UploadPartCopy(input) 2226 if err != nil { 2227 if aerr, ok := err.(awserr.Error); ok { 2228 switch aerr.Code() { 2229 default: 2230 fmt.Println(aerr.Error()) 2231 } 2232 } else { 2233 // Print the error, cast err to awserr.Error to get the Code and 2234 // Message from an error. 2235 fmt.Println(err.Error()) 2236 } 2237 return 2238 } 2239 2240 fmt.Println(result) 2241} 2242