aws-cdk-lib.aws_s3.BucketPolicy

class BucketPolicy (construct)

LanguageType name
.NETAmazon.CDK.AWS.S3.BucketPolicy
Gogithub.com/aws/aws-cdk-go/awscdk/v2/awss3#BucketPolicy
Javasoftware.amazon.awscdk.services.s3.BucketPolicy
Pythonaws_cdk.aws_s3.BucketPolicy
TypeScript (source)aws-cdk-lib » aws_s3 » BucketPolicy

Implements IConstruct, IDependable, IResource

The bucket policy for an Amazon S3 bucket.

Policies define the operations that are allowed on this resource.

You almost never need to define this construct directly.

All AWS resources that support resource policies have a method called addToResourcePolicy(), which will automatically create a new resource policy if one doesn't exist yet, otherwise it will add to the existing policy.

Prefer to use addToResourcePolicy() instead.

Example

// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
import * as cdk from 'aws-cdk-lib';
import { aws_s3 as s3 } from 'aws-cdk-lib';

declare const bucket: s3.Bucket;
const bucketPolicy = new s3.BucketPolicy(this, 'MyBucketPolicy', {
  bucket: bucket,

  // the properties below are optional
  removalPolicy: cdk.RemovalPolicy.DESTROY,
});

Initializer

new BucketPolicy(scope: Construct, id: string, props: BucketPolicyProps)

Parameters

  • scope Construct
  • id string
  • props BucketPolicyProps

Construct Props

NameTypeDescription
bucketIBucketThe Amazon S3 bucket that the policy applies to.
removalPolicy?RemovalPolicyPolicy to apply when the policy is removed from this stack.

bucket

Type: IBucket

The Amazon S3 bucket that the policy applies to.


removalPolicy?

Type: RemovalPolicy (optional, default: RemovalPolicy.DESTROY.)

Policy to apply when the policy is removed from this stack.

Properties

NameTypeDescription
bucketIBucketThe Bucket this Policy applies to.
documentPolicyDocumentA policy document containing permissions to add to the specified bucket.
envResourceEnvironmentThe environment this resource belongs to.
nodeNodeThe tree node.
stackStackThe stack in which this resource is defined.

bucket

Type: IBucket

The Bucket this Policy applies to.


document

Type: PolicyDocument

A policy document containing permissions to add to the specified bucket.

For more information, see Access Policy Language Overview in the Amazon Simple Storage Service Developer Guide.


env

Type: ResourceEnvironment

The environment this resource belongs to.

For resources that are created and managed by the CDK (generally, those created by creating new class instances like Role, Bucket, etc.), this is always the same as the environment of the stack they belong to; however, for imported resources (those obtained from static methods like fromRoleArn, fromBucketName, etc.), that might be different than the stack they were imported into.


node

Type: Node

The tree node.


stack

Type: Stack

The stack in which this resource is defined.

Methods

NameDescription
applyRemovalPolicy(removalPolicy)Sets the removal policy for the BucketPolicy.
toString()Returns a string representation of this construct.
static fromCfnBucketPolicy(cfnBucketPolicy)Create a mutable BucketPolicy from a CfnBucketPolicy.

applyRemovalPolicy(removalPolicy)

public applyRemovalPolicy(removalPolicy: RemovalPolicy): void

Parameters

  • removalPolicy RemovalPolicy — the RemovalPolicy to set.

Sets the removal policy for the BucketPolicy.


toString()

public toString(): string

Returns

  • string

Returns a string representation of this construct.


static fromCfnBucketPolicy(cfnBucketPolicy)

public static fromCfnBucketPolicy(cfnBucketPolicy: CfnBucketPolicy): BucketPolicy

Parameters

  • cfnBucketPolicy CfnBucketPolicy

Returns

  • BucketPolicy

Create a mutable BucketPolicy from a CfnBucketPolicy.