最近更新时间:2025-09-30 14:34:47
Python SDK从V1.4.0版本开始支持项目id参数project_id。
以下代码用于创建存储空间:
from ks3.connection import Connection
from ks3.exception import S3ResponseError, S3CreateError
# 金山云主账号 AccessKey 拥有所有API的访问权限,风险很高。
# 强烈建议您创建并使用子账号进行 API 访问或日常运维,请登录 https://uc.console.ksyun.com/pro/iam/#/user/list 创建子账号。
# 通过指定 host(Endpoint),您可以在指定的地域创建新的存储空间。
c = Connection('<YOUR_ACCESS_KEY>', '<YOUR_SECRET_KEY>', host='<YOUR_REGION_ENDPOINT>')
# 这里如果出现409 conflict错误,说明请求的bucket name有冲突,因为bucket name是全局唯一的。
# 默认创建私有访问权限的存储空间。
# 下面以创建项目 id 为 0 ,存储类型为归档,数据冗余类型为本地冗余的存储空间为例
try:
resp = c.create_bucket(bucket_name, project_id=0, headers={'x-kss-bucket-type': 'ARCHIVE'}, data_redundancy_type='LRS')
print('createBucket, request_id: ', resp.response_metadata.request_id)
except Exception as e:
print('request_id: %s' % e.request_id)
if isinstance(e, S3CreateError):
print("create bucket error, bucket already exists, error: ", e)
else:
print("create bucket error, error: ", e)
纯净模式
