Create Bucket Policy in AWS S3 Bucket with Python Last Updated : 28 Mar, 2023 Comments Improve Suggest changes Like Article Like Report Bucket policy of s3 bucket means permission and action which can be applied on the particular bucket. AWS S3 has an optional policy that can be used to restrict or grant access to an S3 bucket resource. It is important to note that bucket policies are defined in JSON format. For creating a bucket policy in python we will follow the below steps: Step 1: The first step for creating a bucket policy is we need to import python SDK boto3. This will provide methods to us by that we can access the resources of the AWS. And for the policy string dumping, we need to also import JSON. import json import boto3Step 2: The Second step will be we need to create a policy string. Policy string is a key-value pair dictionary. In which the first key will be the Version. And the second key will be the statement in the statement first key will be the sid which will store how to type the policy we want to add. And the second key will be the Effect which will store the access status and the third key will be the permission which will store who have permission to access and the fourth key will be an action which will what type of operation we can perform and the last parameter in this Resource on which we will apply this policy. Example: bucket_policy = { "Version": "2012-10-17", "Statement": [ { "Sid": "AddPerm", "Effect": "Allow", "Principal": "*", "Action": ["s3:*"], "Resource": ["arn:aws:s3:::gfgbucket/*"] } ] } Step 3: The third step will need to convert the bucket policy string in JSON. json.dumps(bucket_policy) Step 4: The fourth step will be for putting bucket policy to the bucket we need to call put_bucket_policy() function .this function will take the first parameter the bucket name and the second parameter will be the policy string. put_bucket_policy(Bucket,policy) Step 5: The last step will be to go to AWS->S3->Bucket->Permission->Bucket policy and verify. Complete code: Python3 import json import boto3 s3_client=boto3.client('s3') BUCKET_NAME='gfgbucket' def create_bucket_policy(): bucket_policy = { "Version": "2012-10-17", "Statement": [ { "Sid": "AddPerm", "Effect": "Allow", "Principal": "*", "Action": ["s3:*"], "Resource": ["arn:aws:s3:::gfgbucket/*"] } ] } policy_string = json.dumps(bucket_policy) s3_client().put_bucket_policy( Bucket=BUCKET_NAME, Policy=policy_string ) Output: Comment More infoAdvertise with us Next Article Create Bucket Policy in AWS S3 Bucket with Python C cse1604310056 Follow Improve Article Tags : Python Amazon Web Services DevOps Practice Tags : python Similar Reads How to Create Cloudfront Distribution in AWS for S3 Bucket ? CloudFront is a content delivery network (CDN) service provided by AWS that delivers data, videos, applications, and APIs to viewers with low latency and high transfer speeds. In this article, we will explore how to create a CloudFront distribution in AWS to serve content stored in an S3 bucket. Key 6 min read How to Create Subfolders in S3 Bucket using AWS CLI The AWS Simple Storage Service (S3) is a cloud service provided by Amazon Web Services (AWS) to store your data securely. There are different approaches to storing and retrieving data from AWS S3; one of them is using the AWS CLI provided by Amazon Web Services. In this article, we will provide you 7 min read Create AWS S3 Bucket using CLI Amazon S3 is a Simple Storage Service in AWS that stores files of different types like Photos, Audio, and videos as objects providing more scalability and security. It allows users to store and retrieve any amount of data at any point in time from anywhere on the web. It facilitates features such as 7 min read How to Delete S3 Bucket from AWS CLI ? Amazon Simple Storage Service (S3) is a versatile, scalable object storage service given by Amazon Web Services (AWS). It is intended to store and recover any amount of data from any place on the web, offering developers and IT teams a durable, dependable solution for their storage needs. S3 is gene 6 min read How To Create a Google Cloud Storage Bucket? Pre-requisite: Google Cloud Platform Google Cloud Storage Bucket is a service that allows you to store and retrieve large amounts of unstructured data, such as videos, images, audio files, and backups. The data is stored in objects, which are simply files and their metadata. Each object is associate 2 min read How To Create GCP Bucket Using CLI ? Users can store and manage their data in the cloud with Google Cloud Storage (GCS), the service is provided by the Google Cloud Platform (GCP). Having the big virtual hard drive to store files, documents, photos, movies, and other media is similar to that. Within Google Cloud Storage, a bucket refer 4 min read How to Read File Content from S3 Bucket with Boto3 ? AWS S3 (Simple Storage Service), a scalable and secure object storage service, is often the go-to solution for storing and retrieving any amount of data, at any time, from anywhere. Boto3 is the AWS Software Development Kit (SDK) for Python, which provides an object-oriented API for AWS infrastructu 5 min read How to Create AWS S3 Bucket Using Terraform? S3 stands for Simple Storage Service. S3 buckets are cloud storage services by Amazon Web Service. It is used to store objects, It consists of data in any format like documents, images, videos, and application code. These are highly scalable.Prerequisite:AWS AccountTerraform InstallationAWS CLIStep 3 min read How To List S3 Buckets With Boto3 ? Amazon S3 and Amazon Lambda are two crucial AWS services used in different organizations across the industry. Amazon S3 is used to store and manage data while Amazon Lambda provides serverless computing service to run code without any management of the server. In this guide, I will first discuss bri 5 min read How to Create ZIP File for AWS Lambda Python ? AWS lambda is a managed serverless deployment service of AWS which can be used for implementation of various applications in cloud. AWS lambda used lambda function code for deploying lambda functions. This code can be either provided via console or zip file. In this article, we will see how to creat 4 min read Like