Skip to content

Latest commit

 

History

History
53 lines (43 loc) · 1.37 KB

put_bucket_policy.md

File metadata and controls

53 lines (43 loc) · 1.37 KB

PUT Bucket Policy

Request Elements

See API Docs for more information about request elements.

Code Snippet

Initialize the Qingstor object with your AccessKeyID and SecretAccessKey.

from qingstor.sdk.service.qingstor import QingStor
from qingstor.sdk.config import Config

config = Config('ACCESS_KEY_ID_EXAMPLE', 'SECRET_ACCESS_KEY_EXAMPLE')
qingstor = QingStor(config)

Initialize a Bucket object according to the bucket name you set for subsequent creation:

bucket_name = "your-bucket-name"
zone_name = "pek3b"
bucket_srv = qingstor.Bucket(bucket_name, zone_name)

then you can put Bucket Policy

stmts = [
    {
        "id": "allow certain site to get objects",
        "user": "*",
        "action": ["get_object"],
        "effect": "allow",
        "resource": [bucket_name + "/*"],
        "condition": {
            "string_like": {
                "Referer": [
                    "*.example1.com",
                    "*.example2.com"
                ]
            }
        }
    },
]
resp = bucket_srv.put_policy(statement=stmts)
if resp.status_code != 200:
    print("Set policy of bucket(name: %s) failed with given message: %s\n" % (
        bucket_name, str(resp.content, "utf-8")))
else:
    print("Put bucket policy successfully.")