A bucket policy is an OSS authorization policy for buckets. You can use a bucket policy to grant or deny fine-grained access to specific OSS resources for anonymous users or specified users, such as Alibaba Cloud accounts, RAM users, and RAM roles. For example, you can grant read-only permissions on specific OSS resources to a RAM user that belongs to another Alibaba Cloud account.
Notes
Before you configure a bucket policy, make sure that you are familiar with this feature. For more information, see Bucket Policy.
The sample code in this topic uses the region ID
cn-hangzhoufor the China (Hangzhou) region. By default, the public Endpoint is used to access resources in a bucket. If you want to access resources in the bucket from other Alibaba Cloud products in the same region, use an internal Endpoint. For more information about the regions and Endpoints supported by OSS, see OSS regions and endpoints.In this topic, access credentials are read from environment variables. For more information about how to configure access credentials, see Configure access credentials.
To set a bucket policy, you must have the
oss:PutBucketPolicypermission. To retrieve a bucket policy, you must have theoss:GetBucketPolicypermission. To delete a bucket policy, you must have theoss:DeleteBucketPolicypermission. For more information, see Grant custom policies to RAM users.
Sample code
References
For the complete sample code for setting a bucket policy, see GitHub example.
For the API operation for setting a bucket policy, see PutBucketPolicy.
For the complete sample code for retrieving a bucket policy, see GitHub example.
For the API operation for retrieving a bucket policy, see GetBucketPolicy.
For the complete sample code for deleting a bucket policy, see GitHub example.
For the API operation for deleting a bucket policy, see DeleteBucketPolicy.