You can call the AppendObject operation to append content to the existing append objects.
- If the object to which you want to append content does not exist, an append object is created when you call the AppendObject operation.
- If the object to which you want to append content exists, the following scenarios may occur when you call the AppendObject operation: If the object is an append object, and the specified position from which the append operation starts is equal to the current object length, the content is appended to the end of the object. If the object is an append object, and the specified position from which the append operation starts is not equal to the current object length, a PositionNotEqualToLength exception is thrown. If the object is not an append object, an ObjectNotAppendable exception is thrown.
- The CopyObject operation cannot be performed on append objects.
The following code provides an example on how to perform append upload:
# -*- coding: utf-8 -*- import oss2 # Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access Object Storage Service (OSS) because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. 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 the endpoint based on your business requirements. # 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 message content is the same as the content of the message when it was sent. # 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, this parameter is set to OBJECT_ACL_PRIVATE, which indicates 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 method that is used to encrypt objects on the OSS server. In this example, the method is set to server-side encryption by using OSS-managed keys (SSE-OSS) # 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 append object. These parameters cannot be included in the requests when you append content to the existing append objects. 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. The full path of the object cannot contain the bucket name. 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 the bucket.head_object method. bucket.append_object('<yourObjectName>', result.next_position, 'content of second append')