Skip navigation links

Package software.amazon.awscdk.services.s3

Amazon S3 Construct Library

See: Description

Package software.amazon.awscdk.services.s3 Description

Amazon S3 Construct Library

---

cfn-resources: Stable

cdk-constructs: Stable


Define an unencrypted S3 bucket.

 Bucket bucket = new Bucket(this, "MyFirstBucket");
 

Bucket constructs expose the following deploy-time attributes:

Encryption

Define a KMS-encrypted bucket:

 Bucket bucket = Bucket.Builder.create(this, "MyEncryptedBucket")
         .encryption(BucketEncryption.KMS)
         .build();
 
 // you can access the encryption key:
 assert(bucket.getEncryptionKey() instanceof Key);
 

You can also supply your own key:

 Key myKmsKey = new Key(this, "MyKey");
 
 Bucket bucket = Bucket.Builder.create(this, "MyEncryptedBucket")
         .encryption(BucketEncryption.KMS)
         .encryptionKey(myKmsKey)
         .build();
 
 assert(bucket.getEncryptionKey() == myKmsKey);
 

Enable KMS-SSE encryption via S3 Bucket Keys:

 Bucket bucket = Bucket.Builder.create(this, "MyEncryptedBucket")
         .encryption(BucketEncryption.KMS)
         .bucketKeyEnabled(true)
         .build();
 

Use BucketEncryption.ManagedKms to use the S3 master KMS key:

 Bucket bucket = Bucket.Builder.create(this, "Buck")
         .encryption(BucketEncryption.KMS_MANAGED)
         .build();
 
 assert(bucket.getEncryptionKey() == null);
 

Permissions

A bucket policy will be automatically created for the bucket upon the first call to addToResourcePolicy(statement):

 Bucket bucket = new Bucket(this, "MyBucket");
 AddToResourcePolicyResult result = bucket.addToResourcePolicy(PolicyStatement.Builder.create()
         .actions(List.of("s3:GetObject"))
         .resources(List.of(bucket.arnForObjects("file.txt")))
         .principals(List.of(new AccountRootPrincipal()))
         .build());
 

If you try to add a policy statement to an existing bucket, this method will not do anything:

 IBucket bucket = Bucket.fromBucketName(this, "existingBucket", "bucket-name");
 
 // No policy statement will be added to the resource
 AddToResourcePolicyResult result = bucket.addToResourcePolicy(PolicyStatement.Builder.create()
         .actions(List.of("s3:GetObject"))
         .resources(List.of(bucket.arnForObjects("file.txt")))
         .principals(List.of(new AccountRootPrincipal()))
         .build());
 

That's because it's not possible to tell whether the bucket already has a policy attached, let alone to re-use that policy to add more statements to it. We recommend that you always check the result of the call:

 Bucket bucket = new Bucket(this, "MyBucket");
 AddToResourcePolicyResult result = bucket.addToResourcePolicy(PolicyStatement.Builder.create()
         .actions(List.of("s3:GetObject"))
         .resources(List.of(bucket.arnForObjects("file.txt")))
         .principals(List.of(new AccountRootPrincipal()))
         .build());
 
 if (!result.getStatementAdded()) {
 }
 

The bucket policy can be directly accessed after creation to add statements or adjust the removal policy.

 Bucket bucket = new Bucket(this, "MyBucket");
 bucket.policy.applyRemovalPolicy(RemovalPolicy.RETAIN);
 

Most of the time, you won't have to manipulate the bucket policy directly. Instead, buckets have "grant" methods called to give prepackaged sets of permissions to other resources. For example:

 Function myLambda;
 
 
 Bucket bucket = new Bucket(this, "MyBucket");
 bucket.grantReadWrite(myLambda);
 

Will give the Lambda's execution role permissions to read and write from the bucket.

AWS Foundational Security Best Practices

Enforcing SSL

To require all requests use Secure Socket Layer (SSL):

 Bucket bucket = Bucket.Builder.create(this, "Bucket")
         .enforceSSL(true)
         .build();
 

Sharing buckets between stacks

To use a bucket in a different stack in the same CDK application, pass the object to the other stack:

 /**
  * Stack that defines the bucket
  */
 public class Producer extends Stack {
     public final Bucket myBucket;
 
     public Producer(App scope, String id) {
         this(scope, id, null);
     }
 
     public Producer(App scope, String id, StackProps props) {
         super(scope, id, props);
 
         Bucket bucket = Bucket.Builder.create(this, "MyBucket")
                 .removalPolicy(RemovalPolicy.DESTROY)
                 .build();
         this.myBucket = bucket;
     }
 }
 
 public class ConsumerProps extends StackProps {
     private IBucket userBucket;
     public IBucket getUserBucket() {
         return this.userBucket;
     }
     public ConsumerProps userBucket(IBucket userBucket) {
         this.userBucket = userBucket;
         return this;
     }
 }
 
 /**
  * Stack that consumes the bucket
  */
 public class Consumer extends Stack {
     public Consumer(App scope, String id, ConsumerProps props) {
         super(scope, id, props);
 
         User user = new User(this, "MyUser");
         props.userBucket.grantReadWrite(user);
     }
 }
 
 Producer producer = new Producer(app, "ProducerStack");
 new Consumer(app, "ConsumerStack", new ConsumerProps().userBucket(producer.getMyBucket()));
 

Importing existing buckets

To import an existing bucket into your CDK application, use the Bucket.fromBucketAttributes factory method. This method accepts BucketAttributes which describes the properties of an already existing bucket:

 Function myLambda;
 
 IBucket bucket = Bucket.fromBucketAttributes(this, "ImportedBucket", BucketAttributes.builder()
         .bucketArn("arn:aws:s3:::my-bucket")
         .build());
 
 // now you can just call methods on the bucket
 bucket.addEventNotification(EventType.OBJECT_CREATED, new LambdaDestination(myLambda), NotificationKeyFilter.builder().prefix("home/myusername/*").build());
 

Alternatively, short-hand factories are available as Bucket.fromBucketName and Bucket.fromBucketArn, which will derive all bucket attributes from the bucket name or ARN respectively:

 IBucket byName = Bucket.fromBucketName(this, "BucketByName", "my-bucket");
 IBucket 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.

 IBucket myCrossRegionBucket = Bucket.fromBucketAttributes(this, "CrossRegionImport", BucketAttributes.builder()
         .bucketArn("arn:aws:s3:::my-bucket")
         .region("us-east-1")
         .build());
 

Bucket Notifications

The Amazon S3 notification feature enables you to receive notifications when certain events happen in your bucket as described under [S3 Bucket Notifications] of the S3 Developer Guide.

To subscribe for bucket notifications, use the bucket.addEventNotification method. The bucket.addObjectCreatedNotification and bucket.addObjectRemovedNotification can also be used for these common use cases.

The following example will subscribe an SNS topic to be notified of all s3:ObjectCreated:* events:

 Bucket bucket = new Bucket(this, "MyBucket");
 Topic topic = new Topic(this, "MyTopic");
 bucket.addEventNotification(EventType.OBJECT_CREATED, new SnsDestination(topic));
 

This call will also ensure that the topic policy can accept notifications for this specific bucket.

Supported S3 notification targets are exposed by the @aws-cdk/aws-s3-notifications package.

It is also possible to specify S3 object key filters when subscribing. The following example will notify myQueue when objects prefixed with foo/ and have the .jpg suffix are removed from the bucket.

 Queue myQueue;
 
 Bucket bucket = new Bucket(this, "MyBucket");
 bucket.addEventNotification(EventType.OBJECT_REMOVED,
 new SqsDestination(myQueue), NotificationKeyFilter.builder().prefix("foo/").suffix(".jpg").build());
 

Adding notifications on existing buckets:

 Topic topic;
 
 IBucket bucket = Bucket.fromBucketAttributes(this, "ImportedBucket", BucketAttributes.builder()
         .bucketArn("arn:aws:s3:::my-bucket")
         .build());
 bucket.addEventNotification(EventType.OBJECT_CREATED, new SnsDestination(topic));
 

Block Public Access

Use blockPublicAccess to specify block public access settings on the bucket.

Enable all block public access settings:

 Bucket bucket = Bucket.Builder.create(this, "MyBlockedBucket")
         .blockPublicAccess(BlockPublicAccess.BLOCK_ALL)
         .build();
 

Block and ignore public ACLs:

 Bucket bucket = Bucket.Builder.create(this, "MyBlockedBucket")
         .blockPublicAccess(BlockPublicAccess.BLOCK_ACLS)
         .build();
 

Alternatively, specify the settings manually:

 Bucket bucket = Bucket.Builder.create(this, "MyBlockedBucket")
         .blockPublicAccess(BlockPublicAccess.Builder.create().blockPublicPolicy(true).build())
         .build();
 

When blockPublicPolicy is set to true, grantPublicRead() throws an error.

Logging configuration

Use serverAccessLogsBucket to describe where server access logs are to be stored.

 Bucket accessLogsBucket = new Bucket(this, "AccessLogsBucket");
 
 Bucket bucket = Bucket.Builder.create(this, "MyBucket")
         .serverAccessLogsBucket(accessLogsBucket)
         .build();
 

It's also possible to specify a prefix for Amazon S3 to assign to all log object keys.

 Bucket accessLogsBucket = new Bucket(this, "AccessLogsBucket");
 
 Bucket bucket = Bucket.Builder.create(this, "MyBucket")
         .serverAccessLogsBucket(accessLogsBucket)
         .serverAccessLogsPrefix("logs")
         .build();
 

S3 Inventory

An inventory 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.

You 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.

 Bucket inventoryBucket = new Bucket(this, "InventoryBucket");
 
 Bucket dataBucket = Bucket.Builder.create(this, "DataBucket")
         .inventories(List.of(Inventory.builder()
                 .frequency(InventoryFrequency.DAILY)
                 .includeObjectVersions(InventoryObjectVersion.CURRENT)
                 .destination(InventoryDestination.builder()
                         .bucket(inventoryBucket)
                         .build())
                 .build(), Inventory.builder()
                 .frequency(InventoryFrequency.WEEKLY)
                 .includeObjectVersions(InventoryObjectVersion.ALL)
                 .destination(InventoryDestination.builder()
                         .bucket(inventoryBucket)
                         .prefix("with-all-versions")
                         .build())
                 .build()))
         .build();
 

If the destination bucket is created as part of the same CDK application, the necessary permissions will be automatically added to the bucket policy. However, if you use an imported bucket (i.e Bucket.fromXXX()), you'll have to make sure it contains the following policy document:

 {
   "Version": "2012-10-17",
   "Statement": [
     {
       "Sid": "InventoryAndAnalyticsExamplePolicy",
       "Effect": "Allow",
       "Principal": { "Service": "s3.amazonaws.com" },
       "Action": "s3:PutObject",
       "Resource": ["arn:aws:s3:::destinationBucket/*"]
     }
   ]
 }
 

Website redirection

You 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.

Static redirection

You can statically redirect a to a given Bucket URL or any other host name with websiteRedirect:

 Bucket bucket = Bucket.Builder.create(this, "MyRedirectedBucket")
         .websiteRedirect(RedirectTarget.builder().hostName("www.example.com").build())
         .build();
 

Routing rules

Alternatively, you can also define multiple websiteRoutingRules, to define complex, conditional redirections:

 Bucket bucket = Bucket.Builder.create(this, "MyRedirectedBucket")
         .websiteRoutingRules(List.of(RoutingRule.builder()
                 .hostName("www.example.com")
                 .httpRedirectCode("302")
                 .protocol(RedirectProtocol.HTTPS)
                 .replaceKey(ReplaceKey.prefixWith("test/"))
                 .condition(RoutingRuleCondition.builder()
                         .httpErrorCodeReturnedEquals("200")
                         .keyPrefixEquals("prefix")
                         .build())
                 .build()))
         .build();
 

Filling the bucket as part of deployment

To put files into a bucket as part of a deployment (for example, to host a website), see the @aws-cdk/aws-s3-deployment package, which provides a resource that can do just that.

The URL for objects

S3 provides two types of URLs for accessing objects via HTTP(S). Path-Style and Virtual Hosted-Style URL. Path-Style is a classic way and will be deprecated. We recommend to use Virtual Hosted-Style URL for newly made bucket.

You can generate both of them.

 Bucket bucket = new Bucket(this, "MyBucket");
 bucket.urlForObject("objectname"); // Path-Style URL
 bucket.virtualHostedUrlForObject("objectname"); // Virtual Hosted-Style URL
 bucket.virtualHostedUrlForObject("objectname", VirtualHostedStyleUrlOptions.builder().regional(false).build());
 

Object Ownership

You can use the two following properties to specify the bucket object Ownership.

Object writer

The Uploading account will own the object.

 Bucket.Builder.create(this, "MyBucket")
         .objectOwnership(ObjectOwnership.OBJECT_WRITER)
         .build();
 

Bucket owner preferred

The 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.

 Bucket.Builder.create(this, "MyBucket")
         .objectOwnership(ObjectOwnership.BUCKET_OWNER_PREFERRED)
         .build();
 

Bucket deletion

When a bucket is removed from a stack (or the stack is deleted), the S3 bucket will be removed according to its removal policy (which by default will simply orphan the bucket and leave it in your AWS account). If the removal policy is set to RemovalPolicy.DESTROY, the bucket will be deleted as long as it does not contain any objects.

To override this and force all objects to get deleted during bucket deletion, enable theautoDeleteObjects option.

 Bucket bucket = Bucket.Builder.create(this, "MyTempFileBucket")
         .removalPolicy(RemovalPolicy.DESTROY)
         .autoDeleteObjects(true)
         .build();
 

Warning if you have deployed a bucket with autoDeleteObjects: true, switching this to false in a CDK version before 1.126.0 will lead to all objects in the bucket being deleted. Be sure to update your bucket resources by deploying with CDK version 1.126.0 or later before switching this value to false.

Skip navigation links