Home > Enterprise >  How to get bucket policy using Airflow s3 hook
How to get bucket policy using Airflow s3 hook

Time:04-06

I would like to find out what is the bucket policy programmatically using Airflow S3 Hook, for a specific S3 bucket. boto3 has get_bucket_policy method, but S3 hook doesn't seem to have it. Is there any way to do that using the S3 hook? Thanks!

CodePudding user response:

You get an instance of boto3.S3.Bucket calling S3Hook.get_bucket instance method.

This instance has a Policy method that can be used to access to policy resource associated to the bucket.

from airflow.providers.amazon.aws.hooks.s3 import S3Hook

s3_hook = S3Hook(aws_conn_id='aws_conn_id')
bucket = s3_hook.get_bucket('your-bucket-name')
policy_resource = bucket.Policy()

policy_json = policy_resource.policy
  • Related