1package main 2 3import ( 4 "context" 5 "flag" 6 "fmt" 7 "io/ioutil" 8 "log" 9 "time" 10 11 "github.com/aws/aws-sdk-go-v2/aws" 12 "github.com/aws/aws-sdk-go-v2/aws/arn" 13 "github.com/aws/aws-sdk-go-v2/config" 14 "github.com/aws/aws-sdk-go-v2/service/s3" 15 "github.com/aws/aws-sdk-go-v2/service/s3control" 16) 17 18const ( 19 bucketName = "myBucketName" 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 39func main() { 40 if len(bucketName) == 0 { 41 flag.PrintDefaults() 42 log.Fatalf("invalid parameters, bucket name required") 43 } 44 45 // Load the SDK's configuration from environment and shared config, and 46 // create the client with this. 47 cfg, err := config.LoadDefaultConfig(context.TODO()) 48 if err != nil { 49 log.Fatalf("failed to load SDK configuration, %v", err) 50 } 51 52 s3Client := s3.NewFromConfig(cfg) 53 s3controlClient := s3control.NewFromConfig(cfg) 54 55 // Create an S3 Bucket 56 fmt.Println("create s3 bucket") 57 58 setVPCBucketEndpoint := s3.WithEndpointResolver(s3.EndpointResolverFromURL(vpcBucketEndpoint)) 59 createBucketParams := &s3.CreateBucketInput{ 60 Bucket: aws.String(bucketName), 61 } 62 _, err = s3Client.CreateBucket(context.TODO(), createBucketParams, setVPCBucketEndpoint) 63 if err != nil { 64 panic(fmt.Errorf("failed to create bucket: %v", err)) 65 } 66 67 // Wait for S3 Bucket to Exist 68 fmt.Println("wait for s3 bucket to exist") 69 waiter := s3.NewBucketExistsWaiter(s3Client) 70 err = waiter.Wait(context.TODO(), &s3.HeadBucketInput{ 71 Bucket: aws.String(bucketName), 72 }, 120*time.Second) 73 if err != nil { 74 panic(fmt.Sprintf("bucket failed to materialize: %v", err)) 75 } 76 77 // Create an Access Point referring to the bucket 78 fmt.Println("create an access point") 79 80 setVpcControlEndpoint := s3control.WithEndpointResolver(s3control.EndpointResolverFromURL(vpcControlEndpoint)) 81 createAccesspointInput := &s3control.CreateAccessPointInput{ 82 AccountId: aws.String(accountID), 83 Bucket: aws.String(bucketName), 84 Name: aws.String(accessPoint), 85 } 86 _, err = s3controlClient.CreateAccessPoint(context.TODO(), createAccesspointInput, setVpcControlEndpoint) 87 if err != nil { 88 panic(fmt.Sprintf("failed to create access point: %v", err)) 89 } 90 91 // build an arn 92 apARN := arn.ARN{ 93 Partition: "aws", 94 Service: "s3", 95 Region: cfg.Region, 96 AccountID: accountID, 97 Resource: "accesspoint/" + accessPoint, 98 } 99 100 // get object using access point ARN 101 fmt.Println("get object using access point") 102 103 setVPCAccesspointEndpoint := s3.WithEndpointResolver(s3.EndpointResolverFromURL(vpcAccesspointEndpoint)) 104 getObjectInput := &s3.GetObjectInput{ 105 Bucket: aws.String(apARN.String()), 106 Key: aws.String("somekey"), 107 } 108 109 getObjectOutput, err := s3Client.GetObject(context.TODO(), getObjectInput, setVPCAccesspointEndpoint) 110 if err != nil { 111 panic(fmt.Sprintf("failed get object request: %v", err)) 112 } 113 114 _, err = ioutil.ReadAll(getObjectOutput.Body) 115 if err != nil { 116 panic(fmt.Sprintf("failed to read object body: %v", err)) 117 } 118} 119