Help Center/ Object Storage Service/ SDK Reference/ Python/ FAQs (SDK for Python)/ Setting an Object Expiration Time (SDK for Python)
Updated on 2026-07-31 GMT+08:00
Setting an Object Expiration Time (SDK for Python)
This example sets the object expiration time using a header when uploading a file stream.
import os
import traceback
from obs import ObsClient, PutObjectHeader
# Obtain an AK/SK pair using environment variables (recommended) or import it in other ways. Using hard coding may result in leakage.
# Obtain an AK/SK pair on OBS console by referring to https://support.huaweicloud.com/en-us/usermanual-ca/ca_01_0003.html.
ak = os.getenv("AccessKeyID")
sk = os.getenv("SecretAccessKey")
# Set server to the endpoint corresponding to the bucket. CN North-Beijing4 is used here as an example. Replace it with the one currently in use.
server = "https://your-endpoint"
# Read a file stream.
content = open('localfile', 'rb')
bucket_name = "examplebucket"
object_key = "objectname"
header = PutObjectHeader()
#Set the expiration time.
header.expires=10
# Upload the file stream.
resp = obs_client.putContent(bucket_name, object_key, content,headers=header)
# If status code 2xx is returned, the API is called successfully. Otherwise, the API call fails.
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 Exception:
print(traceback.format_exc()) This example sets the object expiration time using a user-defined header when uploading a file stream.
import os
import traceback
from obs import ObsClient
# Obtain an AK/SK pair using environment variables (recommended) or import it in other ways. Using hard coding may result in leakage.
# Obtain an AK/SK pair on OBS console by referring to https://support.huaweicloud.com/en-us/usermanual-ca/ca_01_0003.html.
ak = os.getenv("AccessKeyID")
sk = os.getenv("SecretAccessKey")
# Set server to the endpoint corresponding to the bucket. CN North-Beijing4 is used here as an example. Replace it with the one currently in use.
server = "https://your-endpoint"
obs_client = ObsClient(access_key_id=ak, secret_access_key=sk, server=server)
try:
# Read a file stream.
content = open('localfile', 'rb')
bucket_name = "examplebucket"
object_key = "objectname"
header = PutObjectHeader()
# Use a user-defined header to set the expiration time.
extension_headers = {'x-obs-expires': 30}
# Upload the file stream.
resp = obs_client.putContent(bucket_name, object_key, content,extensionHeaders=extension_headers)
# If status code 2xx is returned, the API is called successfully. Otherwise, the API call fails.
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 Exception:
print(traceback.format_exc()) This example sets the expiration time for an uploaded object.
import os
import traceback
from obs import ObsClient
# Obtain an AK/SK pair using environment variables (recommended) or import it in other ways. Using hard coding may result in leakage.
# Obtain an AK/SK pair on OBS console by referring to https://support.huaweicloud.com/en-us/usermanual-ca/ca_01_0003.html.
ak = os.getenv("AccessKeyID")
sk = os.getenv("SecretAccessKey")
# Set server to the endpoint corresponding to the bucket. CN North-Beijing4 is used here as an example. Replace it with the one currently in use.
server = "https://your-endpoint"
obs_client = ObsClient(access_key_id=ak, secret_access_key=sk, server=server)
try:
bucket_name = "examplebucket"
object_key = "objectname"
# Use a user-defined header to set the expiration time.
extension_headers = {'x-obs-expires': 30}
# Configure metadata for the object.
resp = obs_client.setObjectMetadata(bucket_name, object_key, extensionHeaders=extension_headers)
# If status code 2xx is returned, the API is called successfully. Otherwise, the API call fails.
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 Exception:
print(traceback.format_exc()) Parent topic: FAQs (SDK for Python)
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
The system is busy. Please try again later.
For any further questions, feel free to contact us through the chatbot.
Chatbot