-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds support for s3 and s3control private link (#3770)
- Loading branch information
1 parent
4a3fa39
commit b2cc34f
Showing
11 changed files
with
528 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Example | ||
|
||
This example demonstrates how you can use the AWS SDK for Go's Amazon S3 client | ||
to use AWS PrivateLink for Amazon S3. | ||
|
||
# Usage | ||
|
||
To access S3 bucket data using the s3 interface endpoints, prefix the vpc | ||
endpoint with `bucket`. For eg, use endpoint url as `https://bucket.vpce-0xxxxxxx-xxx8xxg.s3.us-west-2.vpce.amazonaws.com` | ||
to access S3 bucket data via the associated vpc endpoint. The SDK may mutate | ||
this endpoint as per the input provided to work with ARNs. | ||
|
||
To access S3 access point data using the s3 interface endpoints, prefix the vpc | ||
endpoint with `accesspoint`. For eg, use endpoint url as `https://accesspoint.vpce-0xxxxxxx-xxxx8xxg.s3.us-west-2.vpce.amazonaws.com` | ||
to access S3 access point data via the associated vpc endpoint. The SDK may | ||
mutate this endpoint as per the input provided to work with ARNs. | ||
|
||
To work with S3 control using the s3 interface endpoints, prefix the vpc endpoint | ||
with `control`. For eg, use endpoint url as `https://control.vpce-0xxxxxxx-xxx8xxg.s3.us-west-2.vpce.amazonaws.com` | ||
to use S3 Control operations with the associated vpc endpoint. The SDK may mutate | ||
this endpoint as per the input provided to work with ARNs. | ||
|
||
The example will create s3 client's that use appropriate vpc endpoint url. The example | ||
will then create a bucket of the name provided in code. Replace the value of | ||
the `accountID` const with the account ID for your AWS account. The | ||
`vpcBucketEndpointUrl`, `vpcAccesspointEndpoint`, `vpcControlEndpoint`, `bucket`, | ||
`keyName`, and `accessPoint` const variables need to be updated to match the name | ||
of the appropriate vpc endpoint, Bucket, Object Key, and Access Point that will be | ||
created by the example. | ||
|
||
```sh | ||
AWS_REGION=<region> go run -tags example usingPrivateLink.go | ||
``` |
105 changes: 105 additions & 0 deletions
105
example/service/s3/usingPrivateLink/usingPrivateLink.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// +build example | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/arn" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/s3" | ||
"github.com/aws/aws-sdk-go/service/s3control" | ||
) | ||
|
||
const ( | ||
bucketName = "myBucketName" | ||
keyName = "myKeyName" | ||
accountID = "123456789012" | ||
accessPoint = "accesspointname" | ||
|
||
// vpcBucketEndpoint will be used by the SDK to resolve an endpoint, when making a call to | ||
// access `bucket` data using s3 interface endpoint. This endpoint may be mutated by the SDK, | ||
// as per the input provided to work with ARNs. | ||
vpcBucketEndpoint = "https://bucket.vpce-0xxxxxxx-xxx8xxg.s3.us-west-2.vpce.amazonaws.com" | ||
|
||
// vpcAccesspointEndpoint will be used by the SDK to resolve an endpoint, when making a call to | ||
// access `access-point` data using s3 interface endpoint. This endpoint may be mutated by the SDK, | ||
// as per the input provided to work with ARNs. | ||
vpcAccesspointEndpoint = "https://accesspoint.vpce-0xxxxxxx-xxx8xxg.s3.us-west-2.vpce.amazonaws.com" | ||
|
||
// vpcControlEndpoint will be used by the SDK to resolve an endpoint, when making a call to | ||
// access `control` data using s3 interface endpoint. This endpoint may be mutated by the SDK, | ||
// as per the input provided to work with ARNs. | ||
vpcControlEndpoint = "https://control.vpce-0xxxxxxx-xxx8xxg.s3.us-west-2.vpce.amazonaws.com" | ||
) | ||
|
||
func main() { | ||
sess := session.Must(session.NewSession()) | ||
|
||
s3BucketSvc := s3.New(sess, &aws.Config{ | ||
Endpoint: aws.String(vpcBucketEndpoint), | ||
}) | ||
|
||
s3AccesspointSvc := s3.New(sess, &aws.Config{ | ||
Endpoint: aws.String(vpcAccesspointEndpoint), | ||
}) | ||
|
||
s3ControlSvc := s3control.New(sess, &aws.Config{ | ||
Endpoint: aws.String(vpcControlEndpoint), | ||
}) | ||
|
||
// Create an S3 Bucket | ||
fmt.Println("create s3 bucket") | ||
_, err := s3BucketSvc.CreateBucket(&s3.CreateBucketInput{ | ||
Bucket: aws.String(bucketName), | ||
}) | ||
if err != nil { | ||
panic(fmt.Errorf("failed to create bucket: %v", err)) | ||
} | ||
|
||
// Wait for S3 Bucket to Exist | ||
fmt.Println("wait for s3 bucket to exist") | ||
err = s3BucketSvc.WaitUntilBucketExists(&s3.HeadBucketInput{ | ||
Bucket: aws.String(bucketName), | ||
}) | ||
if err != nil { | ||
panic(fmt.Sprintf("bucket failed to materialize: %v", err)) | ||
} | ||
|
||
// Create an Access Point referring to the bucket | ||
fmt.Println("create an access point") | ||
_, err = s3ControlSvc.CreateAccessPoint(&s3control.CreateAccessPointInput{ | ||
AccountId: aws.String(accountID), | ||
Bucket: aws.String(bucketName), | ||
Name: aws.String(accessPoint), | ||
}) | ||
if err != nil { | ||
panic(fmt.Sprintf("failed to create access point: %v", err)) | ||
} | ||
|
||
// Use the SDK's ARN builder to create an ARN for the Access Point. | ||
apARN := arn.ARN{ | ||
Partition: "aws", | ||
Service: "s3", | ||
Region: aws.StringValue(sess.Config.Region), | ||
AccountID: accountID, | ||
Resource: "accesspoint/" + accessPoint, | ||
} | ||
|
||
// And Use Access Point ARN where bucket parameters are accepted | ||
fmt.Println("get object using access point") | ||
getObjectOutput, err := s3AccesspointSvc.GetObject(&s3.GetObjectInput{ | ||
Bucket: aws.String(apARN.String()), | ||
Key: aws.String("somekey"), | ||
}) | ||
if err != nil { | ||
panic(fmt.Sprintf("failed get object request: %v", err)) | ||
} | ||
|
||
_, err = ioutil.ReadAll(getObjectOutput.Body) | ||
if err != nil { | ||
panic(fmt.Sprintf("failed to read object body: %v", err)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.