|
| 1 | +// +build example |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "io/ioutil" |
| 8 | + |
| 9 | + "github.com/aws/aws-sdk-go/aws" |
| 10 | + "github.com/aws/aws-sdk-go/aws/arn" |
| 11 | + "github.com/aws/aws-sdk-go/aws/session" |
| 12 | + "github.com/aws/aws-sdk-go/service/s3" |
| 13 | + "github.com/aws/aws-sdk-go/service/s3control" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + bucketName = "myBucketName" |
| 18 | + keyName = "myKeyName" |
| 19 | + accountID = "123456789012" |
| 20 | + accessPoint = "accesspointname" |
| 21 | + |
| 22 | + // vpcBucketEndpoint will be used by the SDK to resolve an endpoint, when making a call to |
| 23 | + // access `bucket` data using s3 interface endpoint. This endpoint may be mutated by the SDK, |
| 24 | + // as per the input provided to work with ARNs. |
| 25 | + vpcBucketEndpoint = "https://bucket.vpce-0xxxxxxx-xxx8xxg.s3.us-west-2.vpce.amazonaws.com" |
| 26 | + |
| 27 | + // vpcAccesspointEndpoint will be used by the SDK to resolve an endpoint, when making a call to |
| 28 | + // access `access-point` data using s3 interface endpoint. This endpoint may be mutated by the SDK, |
| 29 | + // as per the input provided to work with ARNs. |
| 30 | + vpcAccesspointEndpoint = "https://accesspoint.vpce-0xxxxxxx-xxx8xxg.s3.us-west-2.vpce.amazonaws.com" |
| 31 | + |
| 32 | + // vpcControlEndpoint will be used by the SDK to resolve an endpoint, when making a call to |
| 33 | + // access `control` data using s3 interface endpoint. This endpoint may be mutated by the SDK, |
| 34 | + // as per the input provided to work with ARNs. |
| 35 | + vpcControlEndpoint = "https://control.vpce-0xxxxxxx-xxx8xxg.s3.us-west-2.vpce.amazonaws.com" |
| 36 | +) |
| 37 | + |
| 38 | +func main() { |
| 39 | + sess := session.Must(session.NewSession()) |
| 40 | + |
| 41 | + s3BucketSvc := s3.New(sess, &aws.Config{ |
| 42 | + Endpoint: aws.String(vpcBucketEndpoint), |
| 43 | + }) |
| 44 | + |
| 45 | + s3AccesspointSvc := s3.New(sess, &aws.Config{ |
| 46 | + Endpoint: aws.String(vpcAccesspointEndpoint), |
| 47 | + }) |
| 48 | + |
| 49 | + s3ControlSvc := s3control.New(sess, &aws.Config{ |
| 50 | + Endpoint: aws.String(vpcControlEndpoint), |
| 51 | + }) |
| 52 | + |
| 53 | + // Create an S3 Bucket |
| 54 | + fmt.Println("create s3 bucket") |
| 55 | + _, err := s3BucketSvc.CreateBucket(&s3.CreateBucketInput{ |
| 56 | + Bucket: aws.String(bucketName), |
| 57 | + }) |
| 58 | + if err != nil { |
| 59 | + panic(fmt.Errorf("failed to create bucket: %v", err)) |
| 60 | + } |
| 61 | + |
| 62 | + // Wait for S3 Bucket to Exist |
| 63 | + fmt.Println("wait for s3 bucket to exist") |
| 64 | + err = s3BucketSvc.WaitUntilBucketExists(&s3.HeadBucketInput{ |
| 65 | + Bucket: aws.String(bucketName), |
| 66 | + }) |
| 67 | + if err != nil { |
| 68 | + panic(fmt.Sprintf("bucket failed to materialize: %v", err)) |
| 69 | + } |
| 70 | + |
| 71 | + // Create an Access Point referring to the bucket |
| 72 | + fmt.Println("create an access point") |
| 73 | + _, err = s3ControlSvc.CreateAccessPoint(&s3control.CreateAccessPointInput{ |
| 74 | + AccountId: aws.String(accountID), |
| 75 | + Bucket: aws.String(bucketName), |
| 76 | + Name: aws.String(accessPoint), |
| 77 | + }) |
| 78 | + if err != nil { |
| 79 | + panic(fmt.Sprintf("failed to create access point: %v", err)) |
| 80 | + } |
| 81 | + |
| 82 | + // Use the SDK's ARN builder to create an ARN for the Access Point. |
| 83 | + apARN := arn.ARN{ |
| 84 | + Partition: "aws", |
| 85 | + Service: "s3", |
| 86 | + Region: aws.StringValue(sess.Config.Region), |
| 87 | + AccountID: accountID, |
| 88 | + Resource: "accesspoint/" + accessPoint, |
| 89 | + } |
| 90 | + |
| 91 | + // And Use Access Point ARN where bucket parameters are accepted |
| 92 | + fmt.Println("get object using access point") |
| 93 | + getObjectOutput, err := s3AccesspointSvc.GetObject(&s3.GetObjectInput{ |
| 94 | + Bucket: aws.String(apARN.String()), |
| 95 | + Key: aws.String("somekey"), |
| 96 | + }) |
| 97 | + if err != nil { |
| 98 | + panic(fmt.Sprintf("failed get object request: %v", err)) |
| 99 | + } |
| 100 | + |
| 101 | + _, err = ioutil.ReadAll(getObjectOutput.Body) |
| 102 | + if err != nil { |
| 103 | + panic(fmt.Sprintf("failed to read object body: %v", err)) |
| 104 | + } |
| 105 | +} |
0 commit comments