更新时间:2024-03-26 GMT+08:00

设置对象过期时间(Python SDK)

本示例用于上传文件流时通过header设置对象过期时间

obsClient = ObsClient(access_key_id=ak, secret_access_key=sk, server=server)
try:
    # 读取文件流
    content = open('localfile', 'rb')
    bucketName = "examplebucket"
    objectKey = "objectname"
    header=PutObjectHeader()
    #设置对象过期删除时间
    header.expires=10
    # 流式上传
    resp = obsClient.putContent(bucketName, objectkey, content,headers=header)
    # 返回码为2xx时,接口调用成功,否则接口调用失败
    if resp.status < 300:
        print('Put Content Succeeded')
        print('requestId:', resp.requestId)
    else:
        print('Put Content Failed')
        print('requestId:', resp.requestId)
        print('errorCode:', resp.errorCode)
        print('errorMessage:', resp.errorMessage)
except:
    print(traceback.format_exc())

本示例用于上传文件流时通过自定义头域设置对象过期时间

obsClient = ObsClient(access_key_id=ak, secret_access_key=sk, server=server)
try:
    # 读取文件流
    content = open('localfile', 'rb')
    bucketName = "examplebucket"
    objectKey = "objectname"
    header=PutObjectHeader()
    # 自定义头域设置对象过期删除时间
    extensionHeaders = {'x-obs-expires': 30}
    # 流式上传
    resp = obsClient.putContent(bucketName, objectkey, content,extensionHeaders=extensionHeaders)
    # 返回码为2xx时,接口调用成功,否则接口调用失败
    if resp.status < 300:
        print('Put Content Succeeded')
        print('requestId:', resp.requestId)
    else:
        print('Put Content Failed')
        print('requestId:', resp.requestId)
        print('errorCode:', resp.errorCode)
        print('errorMessage:', resp.errorMessage)
except:
    print(traceback.format_exc())

本示例用于设置已上传对象的过期时间

obsClient = ObsClient(access_key_id=ak, secret_access_key=sk, server=server)
try:
    bucketName = "examplebucket"
    objectKey = "objectname"
    # 自定义头域设置对象过期删除时间
    extensionHeaders = {'x-obs-expires': 30}
    # 设置对象元数据
    resp = obsClient.setObjectMetadata(bucketName, objectkey, extensionHeaders=extensionHeaders)
    # 返回码为2xx时,接口调用成功,否则接口调用失败
    if resp.status < 300:
        print('Set Object Metadata Succeeded')
        print('requestId:', resp.requestId)
    else:
        print('Set Object Metadata Failed')
        print('requestId:', resp.requestId)
        print('errorCode:', resp.errorCode)
        print('errorMessage:', resp.errorMessage)
except:
    print(traceback.format_exc())