UNPKG

19.6 kBMarkdownView Raw
1# Amazon S3 Construct Library
2<!--BEGIN STABILITY BANNER-->
3
4---
5
6![End-of-Support](https://img.shields.io/badge/End--of--Support-critical.svg?style=for-the-badge)
7
8> AWS CDK v1 has reached End-of-Support on 2023-06-01.
9> This package is no longer being updated, and users should migrate to AWS CDK v2.
10>
11> For more information on how to migrate, see the [_Migrating to AWS CDK v2_ guide][doc].
12>
13> [doc]: https://docs.aws.amazon.com/cdk/v2/guide/migrating-v2.html
14
15---
16
17<!--END STABILITY BANNER-->
18
19Define an unencrypted S3 bucket.
20
21```ts
22const bucket = new s3.Bucket(this, 'MyFirstBucket');
23```
24
25`Bucket` constructs expose the following deploy-time attributes:
26
27 * `bucketArn` - the ARN of the bucket (i.e. `arn:aws:s3:::bucket_name`)
28 * `bucketName` - the name of the bucket (i.e. `bucket_name`)
29 * `bucketWebsiteUrl` - the Website URL of the bucket (i.e.
30 `http://bucket_name.s3-website-us-west-1.amazonaws.com`)
31 * `bucketDomainName` - the URL of the bucket (i.e. `bucket_name.s3.amazonaws.com`)
32 * `bucketDualStackDomainName` - the dual-stack URL of the bucket (i.e.
33 `bucket_name.s3.dualstack.eu-west-1.amazonaws.com`)
34 * `bucketRegionalDomainName` - the regional URL of the bucket (i.e.
35 `bucket_name.s3.eu-west-1.amazonaws.com`)
36 * `arnForObjects(pattern)` - the ARN of an object or objects within the bucket (i.e.
37 `arn:aws:s3:::bucket_name/exampleobject.png` or
38 `arn:aws:s3:::bucket_name/Development/*`)
39 * `urlForObject(key)` - the HTTP URL of an object within the bucket (i.e.
40 `https://s3.cn-north-1.amazonaws.com.cn/china-bucket/mykey`)
41 * `virtualHostedUrlForObject(key)` - the virtual-hosted style HTTP URL of an object
42 within the bucket (i.e. `https://china-bucket-s3.cn-north-1.amazonaws.com.cn/mykey`)
43 * `s3UrlForObject(key)` - the S3 URL of an object within the bucket (i.e.
44 `s3://bucket/mykey`)
45
46## Encryption
47
48Define a KMS-encrypted bucket:
49
50```ts
51const bucket = new s3.Bucket(this, 'MyEncryptedBucket', {
52 encryption: s3.BucketEncryption.KMS,
53});
54
55// you can access the encryption key:
56assert(bucket.encryptionKey instanceof kms.Key);
57```
58
59You can also supply your own key:
60
61```ts
62const myKmsKey = new kms.Key(this, 'MyKey');
63
64const bucket = new s3.Bucket(this, 'MyEncryptedBucket', {
65 encryption: s3.BucketEncryption.KMS,
66 encryptionKey: myKmsKey,
67});
68
69assert(bucket.encryptionKey === myKmsKey);
70```
71
72Enable KMS-SSE encryption via [S3 Bucket Keys](https://docs.aws.amazon.com/AmazonS3/latest/dev/bucket-key.html):
73
74```ts
75const bucket = new s3.Bucket(this, 'MyEncryptedBucket', {
76 encryption: s3.BucketEncryption.KMS,
77 bucketKeyEnabled: true,
78});
79```
80
81Use `BucketEncryption.ManagedKms` to use the S3 master KMS key:
82
83```ts
84const bucket = new s3.Bucket(this, 'Buck', {
85 encryption: s3.BucketEncryption.KMS_MANAGED,
86});
87
88assert(bucket.encryptionKey == null);
89```
90
91## Permissions
92
93A bucket policy will be automatically created for the bucket upon the first call to
94`addToResourcePolicy(statement)`:
95
96```ts
97const bucket = new s3.Bucket(this, 'MyBucket');
98const result = bucket.addToResourcePolicy(new iam.PolicyStatement({
99 actions: ['s3:GetObject'],
100 resources: [bucket.arnForObjects('file.txt')],
101 principals: [new iam.AccountRootPrincipal()],
102}));
103```
104
105If you try to add a policy statement to an existing bucket, this method will
106not do anything:
107
108```ts
109const bucket = s3.Bucket.fromBucketName(this, 'existingBucket', 'bucket-name');
110
111// No policy statement will be added to the resource
112const result = bucket.addToResourcePolicy(new iam.PolicyStatement({
113 actions: ['s3:GetObject'],
114 resources: [bucket.arnForObjects('file.txt')],
115 principals: [new iam.AccountRootPrincipal()],
116}));
117```
118
119That's because it's not possible to tell whether the bucket
120already has a policy attached, let alone to re-use that policy to add more
121statements to it. We recommend that you always check the result of the call:
122
123```ts
124const bucket = new s3.Bucket(this, 'MyBucket');
125const result = bucket.addToResourcePolicy(new iam.PolicyStatement({
126 actions: ['s3:GetObject'],
127 resources: [bucket.arnForObjects('file.txt')],
128 principals: [new iam.AccountRootPrincipal()],
129}));
130
131if (!result.statementAdded) {
132 // Uh-oh! Someone probably made a mistake here.
133}
134```
135
136The bucket policy can be directly accessed after creation to add statements or
137adjust the removal policy.
138
139```ts
140const bucket = new s3.Bucket(this, 'MyBucket');
141bucket.policy?.applyRemovalPolicy(cdk.RemovalPolicy.RETAIN);
142```
143
144Most of the time, you won't have to manipulate the bucket policy directly.
145Instead, buckets have "grant" methods called to give prepackaged sets of permissions
146to other resources. For example:
147
148```ts
149declare const myLambda: lambda.Function;
150
151const bucket = new s3.Bucket(this, 'MyBucket');
152bucket.grantReadWrite(myLambda);
153```
154
155Will give the Lambda's execution role permissions to read and write
156from the bucket.
157
158## AWS Foundational Security Best Practices
159
160### Enforcing SSL
161
162To require all requests use Secure Socket Layer (SSL):
163
164```ts
165const bucket = new s3.Bucket(this, 'Bucket', {
166 enforceSSL: true,
167});
168```
169
170## Sharing buckets between stacks
171
172To use a bucket in a different stack in the same CDK application, pass the object to the other stack:
173
174[sharing bucket between stacks](test/integ.bucket-sharing.lit.ts)
175
176## Importing existing buckets
177
178To import an existing bucket into your CDK application, use the `Bucket.fromBucketAttributes`
179factory method. This method accepts `BucketAttributes` which describes the properties of an already
180existing bucket:
181
182```ts
183declare const myLambda: lambda.Function;
184const bucket = s3.Bucket.fromBucketAttributes(this, 'ImportedBucket', {
185 bucketArn: 'arn:aws:s3:::my-bucket',
186});
187
188// now you can just call methods on the bucket
189bucket.addEventNotification(s3.EventType.OBJECT_CREATED, new s3n.LambdaDestination(myLambda), {prefix: 'home/myusername/*'});
190```
191
192Alternatively, short-hand factories are available as `Bucket.fromBucketName` and
193`Bucket.fromBucketArn`, which will derive all bucket attributes from the bucket
194name or ARN respectively:
195
196```ts
197const byName = s3.Bucket.fromBucketName(this, 'BucketByName', 'my-bucket');
198const byArn = s3.Bucket.fromBucketArn(this, 'BucketByArn', 'arn:aws:s3:::my-bucket');
199```
200
201The bucket's region defaults to the current stack's region, but can also be explicitly set in cases where one of the bucket's
202regional properties needs to contain the correct values.
203
204```ts
205const myCrossRegionBucket = s3.Bucket.fromBucketAttributes(this, 'CrossRegionImport', {
206 bucketArn: 'arn:aws:s3:::my-bucket',
207 region: 'us-east-1',
208});
209// myCrossRegionBucket.bucketRegionalDomainName === 'my-bucket.s3.us-east-1.amazonaws.com'
210```
211
212## Bucket Notifications
213
214The Amazon S3 notification feature enables you to receive notifications when
215certain events happen in your bucket as described under [S3 Bucket
216Notifications] of the S3 Developer Guide.
217
218To subscribe for bucket notifications, use the `bucket.addEventNotification` method. The
219`bucket.addObjectCreatedNotification` and `bucket.addObjectRemovedNotification` can also be used for
220these common use cases.
221
222The following example will subscribe an SNS topic to be notified of all `s3:ObjectCreated:*` events:
223
224```ts
225const bucket = new s3.Bucket(this, 'MyBucket');
226const topic = new sns.Topic(this, 'MyTopic');
227bucket.addEventNotification(s3.EventType.OBJECT_CREATED, new s3n.SnsDestination(topic));
228```
229
230This call will also ensure that the topic policy can accept notifications for
231this specific bucket.
232
233Supported S3 notification targets are exposed by the `@aws-cdk/aws-s3-notifications` package.
234
235It is also possible to specify S3 object key filters when subscribing. The
236following example will notify `myQueue` when objects prefixed with `foo/` and
237have the `.jpg` suffix are removed from the bucket.
238
239```ts
240declare const myQueue: sqs.Queue;
241const bucket = new s3.Bucket(this, 'MyBucket');
242bucket.addEventNotification(s3.EventType.OBJECT_REMOVED,
243 new s3n.SqsDestination(myQueue),
244 { prefix: 'foo/', suffix: '.jpg' });
245```
246
247Adding notifications on existing buckets:
248
249```ts
250declare const topic: sns.Topic;
251const bucket = s3.Bucket.fromBucketAttributes(this, 'ImportedBucket', {
252 bucketArn: 'arn:aws:s3:::my-bucket',
253});
254bucket.addEventNotification(s3.EventType.OBJECT_CREATED, new s3n.SnsDestination(topic));
255```
256
257When you add an event notification to a bucket, a custom resource is created to
258manage the notifications. By default, a new role is created for the Lambda
259function that implements this feature. If you want to use your own role instead,
260you should provide it in the `Bucket` constructor:
261
262```ts
263declare const myRole: iam.IRole;
264const bucket = new s3.Bucket(this, 'MyBucket', {
265 notificationsHandlerRole: myRole,
266});
267```
268
269Whatever role you provide, the CDK will try to modify it by adding the
270permissions from `AWSLambdaBasicExecutionRole` (an AWS managed policy) as well
271as the permissions `s3:PutBucketNotification` and `s3:GetBucketNotification`.
272If you’re passing an imported role, and you don’t want this to happen, configure
273it to be immutable:
274
275```ts
276const importedRole = iam.Role.fromRoleArn(this, 'role', 'arn:aws:iam::123456789012:role/RoleName', {
277 mutable: false,
278});
279```
280
281> If you provide an imported immutable role, make sure that it has at least all
282> the permissions mentioned above. Otherwise, the deployment will fail!
283
284[S3 Bucket Notifications]: https://docs.aws.amazon.com/AmazonS3/latest/dev/NotificationHowTo.html
285
286
287### EventBridge notifications
288
289Amazon S3 can send events to Amazon EventBridge whenever certain events happen in your bucket.
290Unlike other destinations, you don't need to select which event types you want to deliver.
291
292The following example will enable EventBridge notifications:
293
294```ts
295const bucket = new s3.Bucket(this, 'MyEventBridgeBucket', {
296 eventBridgeEnabled: true,
297});
298```
299
300[S3 EventBridge notifications]: https://docs.aws.amazon.com/AmazonS3/latest/userguide/EventBridge.html
301
302## Block Public Access
303
304Use `blockPublicAccess` to specify [block public access settings] on the bucket.
305
306Enable all block public access settings:
307
308```ts
309const bucket = new s3.Bucket(this, 'MyBlockedBucket', {
310 blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL,
311});
312```
313
314Block and ignore public ACLs:
315
316```ts
317const bucket = new s3.Bucket(this, 'MyBlockedBucket', {
318 blockPublicAccess: s3.BlockPublicAccess.BLOCK_ACLS,
319});
320```
321
322Alternatively, specify the settings manually:
323
324```ts
325const bucket = new s3.Bucket(this, 'MyBlockedBucket', {
326 blockPublicAccess: new s3.BlockPublicAccess({ blockPublicPolicy: true }),
327});
328```
329
330When `blockPublicPolicy` is set to `true`, `grantPublicRead()` throws an error.
331
332[block public access settings]: https://docs.aws.amazon.com/AmazonS3/latest/dev/access-control-block-public-access.html
333
334## Logging configuration
335
336Use `serverAccessLogsBucket` to describe where server access logs are to be stored.
337
338```ts
339const accessLogsBucket = new s3.Bucket(this, 'AccessLogsBucket');
340
341const bucket = new s3.Bucket(this, 'MyBucket', {
342 serverAccessLogsBucket: accessLogsBucket,
343});
344```
345
346It's also possible to specify a prefix for Amazon S3 to assign to all log object keys.
347
348```ts
349const accessLogsBucket = new s3.Bucket(this, 'AccessLogsBucket');
350
351const bucket = new s3.Bucket(this, 'MyBucket', {
352 serverAccessLogsBucket: accessLogsBucket,
353 serverAccessLogsPrefix: 'logs',
354});
355```
356
357[S3 Server access logging]: https://docs.aws.amazon.com/AmazonS3/latest/dev/ServerLogs.html
358
359## S3 Inventory
360
361An [inventory](https://docs.aws.amazon.com/AmazonS3/latest/dev/storage-inventory.html) contains a list of the objects in the source bucket and metadata for each object. The inventory lists are stored in the destination bucket as a CSV file compressed with GZIP, as an Apache optimized row columnar (ORC) file compressed with ZLIB, or as an Apache Parquet (Parquet) file compressed with Snappy.
362
363You can configure multiple inventory lists for a bucket. You can configure what object metadata to include in the inventory, whether to list all object versions or only current versions, where to store the inventory list file output, and whether to generate the inventory on a daily or weekly basis.
364
365```ts
366const inventoryBucket = new s3.Bucket(this, 'InventoryBucket');
367
368const dataBucket = new s3.Bucket(this, 'DataBucket', {
369 inventories: [
370 {
371 frequency: s3.InventoryFrequency.DAILY,
372 includeObjectVersions: s3.InventoryObjectVersion.CURRENT,
373 destination: {
374 bucket: inventoryBucket,
375 },
376 },
377 {
378 frequency: s3.InventoryFrequency.WEEKLY,
379 includeObjectVersions: s3.InventoryObjectVersion.ALL,
380 destination: {
381 bucket: inventoryBucket,
382 prefix: 'with-all-versions',
383 },
384 },
385 ],
386});
387```
388
389If the destination bucket is created as part of the same CDK application, the necessary permissions will be automatically added to the bucket policy.
390However, if you use an imported bucket (i.e `Bucket.fromXXX()`), you'll have to make sure it contains the following policy document:
391
392```json
393{
394 "Version": "2012-10-17",
395 "Statement": [
396 {
397 "Sid": "InventoryAndAnalyticsExamplePolicy",
398 "Effect": "Allow",
399 "Principal": { "Service": "s3.amazonaws.com" },
400 "Action": "s3:PutObject",
401 "Resource": ["arn:aws:s3:::destinationBucket/*"]
402 }
403 ]
404}
405```
406
407## Website redirection
408
409You can use the two following properties to specify the bucket [redirection policy]. Please note that these methods cannot both be applied to the same bucket.
410
411[redirection policy]: https://docs.aws.amazon.com/AmazonS3/latest/dev/how-to-page-redirect.html#advanced-conditional-redirects
412
413### Static redirection
414
415You can statically redirect a to a given Bucket URL or any other host name with `websiteRedirect`:
416
417```ts
418const bucket = new s3.Bucket(this, 'MyRedirectedBucket', {
419 websiteRedirect: { hostName: 'www.example.com' },
420});
421```
422
423### Routing rules
424
425Alternatively, you can also define multiple `websiteRoutingRules`, to define complex, conditional redirections:
426
427```ts
428const bucket = new s3.Bucket(this, 'MyRedirectedBucket', {
429 websiteRoutingRules: [{
430 hostName: 'www.example.com',
431 httpRedirectCode: '302',
432 protocol: s3.RedirectProtocol.HTTPS,
433 replaceKey: s3.ReplaceKey.prefixWith('test/'),
434 condition: {
435 httpErrorCodeReturnedEquals: '200',
436 keyPrefixEquals: 'prefix',
437 },
438 }],
439});
440```
441
442## Filling the bucket as part of deployment
443
444To put files into a bucket as part of a deployment (for example, to host a
445website), see the `@aws-cdk/aws-s3-deployment` package, which provides a
446resource that can do just that.
447
448## The URL for objects
449
450S3 provides two types of URLs for accessing objects via HTTP(S). Path-Style and
451[Virtual Hosted-Style](https://docs.aws.amazon.com/AmazonS3/latest/dev/VirtualHosting.html)
452URL. Path-Style is a classic way and will be
453[deprecated](https://aws.amazon.com/jp/blogs/aws/amazon-s3-path-deprecation-plan-the-rest-of-the-story).
454We recommend to use Virtual Hosted-Style URL for newly made bucket.
455
456You can generate both of them.
457
458```ts
459const bucket = new s3.Bucket(this, 'MyBucket');
460bucket.urlForObject('objectname'); // Path-Style URL
461bucket.virtualHostedUrlForObject('objectname'); // Virtual Hosted-Style URL
462bucket.virtualHostedUrlForObject('objectname', { regional: false }); // Virtual Hosted-Style URL but non-regional
463```
464
465## Object Ownership
466
467You can use one of following properties to specify the bucket [object Ownership].
468
469[object Ownership]: https://docs.aws.amazon.com/AmazonS3/latest/dev/about-object-ownership.html
470
471### Object writer
472
473The Uploading account will own the object.
474
475```ts
476new s3.Bucket(this, 'MyBucket', {
477 objectOwnership: s3.ObjectOwnership.OBJECT_WRITER,
478});
479```
480
481### Bucket owner preferred
482
483The bucket owner will own the object if the object is uploaded with the bucket-owner-full-control canned ACL. Without this setting and canned ACL, the object is uploaded and remains owned by the uploading account.
484
485```ts
486new s3.Bucket(this, 'MyBucket', {
487 objectOwnership: s3.ObjectOwnership.BUCKET_OWNER_PREFERRED,
488});
489```
490
491### Bucket owner enforced (recommended)
492
493ACLs are disabled, and the bucket owner automatically owns and has full control over every object in the bucket. ACLs no longer affect permissions to data in the S3 bucket. The bucket uses policies to define access control.
494
495```ts
496new s3.Bucket(this, 'MyBucket', {
497 objectOwnership: s3.ObjectOwnership.BUCKET_OWNER_ENFORCED,
498});
499```
500
501## Bucket deletion
502
503When a bucket is removed from a stack (or the stack is deleted), the S3
504bucket will be removed according to its removal policy (which by default will
505simply orphan the bucket and leave it in your AWS account). If the removal
506policy is set to `RemovalPolicy.DESTROY`, the bucket will be deleted as long
507as it does not contain any objects.
508
509To override this and force all objects to get deleted during bucket deletion,
510enable the`autoDeleteObjects` option.
511
512```ts
513const bucket = new s3.Bucket(this, 'MyTempFileBucket', {
514 removalPolicy: cdk.RemovalPolicy.DESTROY,
515 autoDeleteObjects: true,
516});
517```
518
519**Warning** if you have deployed a bucket with `autoDeleteObjects: true`,
520switching this to `false` in a CDK version *before* `1.126.0` will lead to
521all objects in the bucket being deleted. Be sure to update your bucket resources
522by deploying with CDK version `1.126.0` or later **before** switching this value to `false`.
523
524## Transfer Acceleration
525
526[Transfer Acceleration](https://docs.aws.amazon.com/AmazonS3/latest/userguide/transfer-acceleration.html) can be configured to enable fast, easy, and secure transfers of files over long distances:
527
528```ts
529const bucket = new s3.Bucket(this, 'MyBucket', {
530 transferAcceleration: true,
531});
532```
533
534To access the bucket that is enabled for Transfer Acceleration, you must use a special endpoint. The URL can be generated using method `transferAccelerationUrlForObject`:
535
536```ts
537const bucket = new s3.Bucket(this, 'MyBucket', {
538 transferAcceleration: true,
539});
540bucket.transferAccelerationUrlForObject('objectname');
541```
542
543## Intelligent Tiering
544
545[Intelligent Tiering](https://docs.aws.amazon.com/AmazonS3/latest/userguide/intelligent-tiering.html) can be configured to automatically move files to glacier:
546
547```ts
548new s3.Bucket(this, 'MyBucket', {
549 intelligentTieringConfigurations: [{
550 name: 'foo',
551 prefix: 'folder/name',
552 archiveAccessTierTime: cdk.Duration.days(90),
553 deepArchiveAccessTierTime: cdk.Duration.days(180),
554 tags: [{key: 'tagname', value: 'tagvalue'}]
555 }],
556});
557
558```
559
560## Lifecycle Rule
561
562[Managing lifecycle](https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-lifecycle-mgmt.html) can be configured transition or expiration actions.
563
564```ts
565const bucket = new s3.Bucket(this, 'MyBucket', {
566 lifecycleRules: [{
567 abortIncompleteMultipartUploadAfter: cdk.Duration.minutes(30),
568 enabled: false,
569 expiration: cdk.Duration.days(30),
570 expirationDate: new Date(),
571 expiredObjectDeleteMarker: false,
572 id: 'id',
573 noncurrentVersionExpiration: cdk.Duration.days(30),
574
575 // the properties below are optional
576 noncurrentVersionsToRetain: 123,
577 noncurrentVersionTransitions: [{
578 storageClass: s3.StorageClass.GLACIER,
579 transitionAfter: cdk.Duration.days(30),
580
581 // the properties below are optional
582 noncurrentVersionsToRetain: 123,
583 }],
584 objectSizeGreaterThan: 500,
585 prefix: 'prefix',
586 objectSizeLessThan: 10000,
587 transitions: [{
588 storageClass: s3.StorageClass.GLACIER,
589
590 // the properties below are optional
591 transitionAfter: cdk.Duration.days(30),
592 transitionDate: new Date(),
593 }],
594 }]
595});
596```