Skip to content

Commit

Permalink
feat(s3): imported buckets can have an explicit region
Browse files Browse the repository at this point in the history
However, while this set the region on the object itself, it didn't adjust the
various region-aware properties of imported buckets (e.g., regional domain
names). This change makes the regional properties of the imported bucket use the
correct region.

fixes #9556
  • Loading branch information
njlynch committed Aug 24, 2020
1 parent fdddb10 commit 4e65de6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
11 changes: 11 additions & 0 deletions packages/@aws-cdk/aws-s3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ const byName = Bucket.fromBucketName(this, 'BucketByName', 'my-bucket');
const byArn = Bucket.fromBucketArn(this, 'BucketByArn', 'arn:aws:s3:::my-bucket');
```

The bucket's region defaults to the current stack's region, but can also be explicitly set in cases where one of the bucket's
regional properties needs to contain the correct values.

```ts
const myCrossRegionBucket = Bucket.fromBucketAttributes(this, 'CrossRegionImport', {
bucketArn: 'arn:aws:s3:::my-bucket',
region: 'us-east-1',
});
// myCrossRegionBucket.bucketRegionalDomainName === 'my-bucket.s3.us-east-1.amazonaws.com'
```

### Bucket Notifications

The Amazon S3 notification feature enables you to receive notifications when
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-s3/lib/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1125,7 +1125,7 @@ export class Bucket extends BucketBase {
*/
public static fromBucketAttributes(scope: Construct, id: string, attrs: BucketAttributes): IBucket {
const stack = Stack.of(scope);
const region = stack.region;
const region = attrs.region ?? stack.region;
const urlSuffix = stack.urlSuffix;

const bucketName = parseBucketName(scope, attrs);
Expand Down
16 changes: 16 additions & 0 deletions packages/@aws-cdk/aws-s3/test/test.bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,22 @@ export = {

test.done();
},

'import can explicitly set bucket region'(test: Test) {
const stack = new cdk.Stack(undefined, undefined, {
env: { region: 'us-east-1' },
});

const bucket = s3.Bucket.fromBucketAttributes(stack, 'ImportedBucket', {
bucketName: 'myBucket',
region: 'eu-west-1',
});

test.equals(bucket.bucketRegionalDomainName, `myBucket.s3.eu-west-1.${stack.urlSuffix}`);
test.equals(bucket.bucketWebsiteDomainName, `myBucket.s3-website-eu-west-1.${stack.urlSuffix}`);

test.done();
},
},

'grantRead'(test: Test) {
Expand Down

0 comments on commit 4e65de6

Please sign in to comment.