• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. API Reference (Python SDK)
  4. Bucket-Related APIs
  5. PUT Bucket policy

PUT Bucket policy

API Description

You can use this API to set a bucket policy. If the bucket already has a policy, the policy will be overwritten by the one specified in this request.

Method Definition

ObsClient.setBucketPolicy(bucketName, policyJSON)

Request Parameter

Field

Type

Optional or Mandatory

Description

bucketName

str

Mandatory

Bucket name

policyJSON

str

Mandatory

Policy information in JSON format.

Returned Result

Type

Description

GetResult

SDK common result object

Sample Code

try:
    resp = obsClient.setBucketPolicy('bucketname', '{"Version":"2008-10-17","Id": "Policy1","Statement": [{"Sid": "Stmt1375240018061","Action": ["s3:GetBucketPolicy"],"Effect": "Allow","Resource": "arn:aws:s3:::bucketname","Principal": { "CanonicalUser": "userid" } }]}') 
     
    if resp.status < 300: 
        print('requestId:', resp.requestId) 
    else: 
        print('errorCode:', resp.errorCode) 
        print('errorMessage:', resp.errorMessage)
except:
    import traceback
    print(traceback.format_exc())

The bucket name contained in the Resource field of policyJSON must be the one specified for the bucket policy.