All Products
Search
Document Center

Object Storage Service:Append upload

Last Updated:Sep 25, 2023

You can call the AppendObject operation to append content to existing appendable objects.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about the regions and endpoints supported by OSS, see Regions and endpoints.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.

  • To use append upload, you must have the oss:PutObject permission. For more information, see Common examples of RAM policies.

  • If the object to which you want to append content does not exist, an appendable object is created when you call the AppendObject operation.

  • If the object to which you want to append content exists:

    • If the object is an appendable object and the specified position from which the append operation starts is equal to the current object size, the data is appended to the end of the object.

    • If the object is an appendable object and the specified position from which the append operation starts is not equal to the current object size, the PositionNotEqualToLength error is returned.

    • If the object is not an appendable object, the ObjectNotAppendable error is returned.

Examples

The following sample code provides an example on how to perform an append upload operation:

# -*- coding: utf-8 -*-
import oss2

# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
# Specify the name of the bucket. Example: examplebucket. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')

# The following code provides an example on how to specify headers for the append upload: 
# headers = dict()
# Specify the caching behavior of the web page for the object. 
# headers['Cache-Control'] = 'no-cache'
# Specify the name of the object when the object is downloaded. 
# headers['Content-Disposition'] = 'oss_MultipartUpload.txt'
# Specify the encoding format for the content of the object. 
# headers['Content-Encoding'] = 'utf-8'
# Specify the request header that is used to check whether the content of the received message is the same as the content of the sent message. 
# headers['Content-MD5'] = 'ohhnqLBJFiKkPSBO1eNaUA=='
# Specify the expiration time. 
# headers['Expires'] = 'Wed, 08 Jul 2022 16:57:01 GMT'
# Specify the access control list (ACL) of the object. In this example, the ACL is set to OBJECT_ACL_PRIVATE. 
# headers['x-oss-object-acl'] = oss2.OBJECT_ACL_PRIVATE
# Specify whether to overwrite the object with the same name in the append upload. 
# headers['x-oss-forbid-overwrite'] = 'true'
# Specify the server-side encryption method. In this example, SSE-OSS is specified for server-side encryption. 
# headers[OSS_SERVER_SIDE_ENCRYPTION] = SERVER_SIDE_ENCRYPTION_AES256
# Specify the storage class of the object. 
# headers['x-oss-storage-class'] = oss2.BUCKET_STORAGE_CLASS_STANDARD
# You can add parameters whose names are prefixed with x-oss-meta- when you call the AppendObject operation to create an appendable object. These parameters cannot be included in the requests when you append content to an existing appendable object. Parameters whose names are prefixed with x-oss-meta- are considered the metadata of the object. 
# headers['x-oss-meta-author'] = 'Alice'
# result = bucket.append_object(exampledir/exampleobject.txt, 0, 'content of first append', headers=headers)

# Set the position from which the first append operation starts to 0. 
# Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. 
result = bucket.append_object('exampledir/exampleobject.txt', 0, 'content of first append')
# If you have appended content to the object, you can obtain the position from which the current append operation starts from the next_position field in the response returned by the last operation or by using bucket.head_object. 
bucket.append_object('<yourObjectName>', result.next_position, 'content of second append')        

References

  • For the complete sample code for append upload, visit GitHub.

  • For more information about the API operation that you can call to perform append upload, see AppendObject.