After you upload objects to a bucket, OSS automatically generates URLs based on the bucket's public endpoint. To serve those objects under a custom domain name, add a CNAME record that maps your domain to the bucket.
The OSS Python SDK V2 supports the full CNAME lifecycle:
Create a CNAME token (domain ownership verification)
Get a CNAME token
Add a CNAME record (with optional SSL certificate)
List CNAME records
Delete a CNAME record
Prerequisites
Before you begin, ensure that you have:
An OSS bucket
A custom domain name that you own and can manage DNS records for
The
alibabacloud_oss_v2Python package installedOSS credentials set as environment variables (
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRET)
Usage notes
The sample code in this topic uses the China (Hangzhou) region (
cn-hangzhou) and a public endpoint by default. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For supported regions and endpoints, see Regions and endpoints.
How it works
Binding a custom domain requires domain ownership verification before OSS accepts the CNAME record:
Call
create_cname_tokento get a CNAME token from OSS.Add the token as a DNS TXT record at your domain registrar to prove ownership.
After DNS propagation, call
put_cnameto register the CNAME record on the bucket.Use
list_cnameto confirm the binding is active.Use
delete_cnameto remove the record when needed.
Note: DNS changes can take minutes to hours to propagate. If put_cname fails immediately after adding the TXT record, wait for propagation and retry.Get a CNAME token
Retrieve an existing CNAME token by domain name to check its value or expiration.
import argparse
import alibabacloud_oss_v2 as oss
parser = argparse.ArgumentParser(description="get cname token sample")
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
parser.add_argument('--cname', help='The name of the CNAME record that is mapped to the bucket.', required=True)
def main():
args = parser.parse_args()
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region
if args.endpoint is not None:
cfg.endpoint = args.endpoint
client = oss.Client(cfg)
result = client.get_cname_token(oss.GetCnameTokenRequest(
bucket=args.bucket,
cname=args.cname,
))
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
f' cname: {result.cname_token.cname if hasattr(result.cname_token, "cname") else "Not set"},'
f' token: {result.cname_token.token if hasattr(result.cname_token, "token") else "Not set"},'
f' expire time: {result.cname_token.expire_time.strftime("%Y-%m-%dT%H:%M:%S.000Z") if hasattr(result.cname_token, "expire_time") else "Not set"},'
f' bucket: {result.cname_token.bucket if hasattr(result.cname_token, "bucket") else "Not set"},'
)
if __name__ == "__main__":
main()For the complete sample, see get_cname_token.py.