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 from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see OSS 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.
If the object to which you want to append content does not exist, the AppendObject operation creates an appendable object.
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.
Permissions
By default, an Alibaba Cloud account has full permissions. RAM users or RAM roles under an Alibaba Cloud account do not have any permissions by default. The Alibaba Cloud account or account administrator must grant operation permissions through RAM Policy or Bucket policies.
API | Action | Definition |
AppendObject |
| You can call this operation to upload an object by appending the object to an existing object. |
| When uploading an object by appending the object to an existing object, if you specify object tags through x-oss-tagging, this permission is required. |
Examples
The following code provides an example of how to append content to an object:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run the code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use Signature V4.
region = "cn-hangzhou"
# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
# The following code provides an example of how to specify headers for an 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'
# This request header is used to check whether the message content is the same as the content that was sent.
# headers['Content-MD5'] = 'ohhnqLBJFiKkPSBO1eNaUA=='
# Specify the expiration date.
# 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, which specifies the private access permission.
# headers['x-oss-object-acl'] = oss2.OBJECT_ACL_PRIVATE
# Specify whether to overwrite an object that has the same name during an append upload.
# headers['x-oss-forbid-overwrite'] = 'true'
# Specify the server-side encryption method. This example uses server-side encryption with 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
# Add parameters prefixed with x-oss-meta- when you call the AppendObject operation to create an appendable object. Do not include these parameters in requests to append content to an existing appendable object. Parameters prefixed with x-oss-meta- are considered object metadata.
# 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 (the Position parameter) 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 this is not the first append operation, obtain the append position from the next_position attribute in the return value of the last append operation, or by calling the bucket.head_object method.
bucket.append_object('<yourObjectName>', result.next_position, 'content of second append') References
For the complete sample code for append upload, see the example on GitHub.
For more information about the AppendObject API operation, see AppendObject.