Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get bucket name from Bucket object in AWS CDK for python

I've create an S3 bucket for hosting my website. For that I've used the below code from the AWS CDK for python docs

self.bucket = s3.Bucket(
    self,
    "my-bucket-name",
    bucket_name="my-bucket-name",
    removal_policy=core.RemovalPolicy.DESTROY,
    website_index_document="index.html",
    public_read_access=True
)

For a reason, I want to send this bucket object as an argument to another object and get the bucket name from the argument. So, I've tried

self.bucket.bucket_name
self.bucket.bucket_arn

nothing seems working, instead the object returns ${Token[TOKEN.189]}. Could anyone guide me through this?

like image 422
Balu Avatar asked Oct 29 '25 02:10

Balu


1 Answers

There is a handy method called fromBucketName you can use:

const bucket = aws_s3.Bucket.fromBucketName(this, "bucketLabel", "nameYouGaveBucket")

Otherwise, I believe you are looking for bucket.bucketName (typescript) or bucket.bucket_name (python).

See typescript docs python docs. This is also available in the CDK wrappers in other languages.

Note that there are similar methods for all sorts of CDK constructs, so you should refer often to the API docs, as there is lots like this you can find easily there.

like image 81
ryanjdillon Avatar answered Oct 30 '25 18:10

ryanjdillon