github.com/aavshr/aws-sdk-go@v1.41.3/example/service/s3/usingPrivateLink/usingPrivateLink.go (about)

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