Append upload lets you add content to the end of an appendable object using the AppendObject method.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For details about supported regions and endpoints, see Regions and endpoints.
In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.
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 Configuration examples for common scenarios.
If the object does not exist, the AppendObject operation creates an appendable object.
If the object exists:
If the object is an appendable object and the specified append position matches the current length of the object, the content is appended to the end of the object.
If the object is an appendable object but the specified append position does not match the current length of the object, a PositionNotEqualToLength exception is thrown.
If the object is not an appendable object, such as a Normal object uploaded using simple upload, an ObjectNotAppendable exception is thrown.
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. |
Sample code
The following code shows how to perform an append upload:
const OSS = require('ali-oss')
const client = new OSS({
// Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
region: 'yourRegion',
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name. Example: examplebucket.
bucket: 'examplebucket',
});
const headers = {
// Specify the access permissions of the object.
'x-oss-object-acl': 'private',
// Specify the storage class of the object.
'x-oss-storage-class': 'Standard',
// Specify the server-side encryption method. In this example, server-side encryption with OSS-managed keys (SSE-OSS) is used.
'x-oss-server-side-encryption': 'AES256',
};
async function append () {
// Perform the first append upload. The return value indicates the position for the next append operation.
// objectName specifies the full path of the object without the bucket name. Example: destfolder/examplefile.txt.
// localFile specifies the full path of the local file, including the file extension. Example: /users/local/examplefile.txt.
let result = await client.append('objectName', 'localFile'
// Custom headers and metadata.
//,{headers}
)
// Perform the second append upload. The position for the next append operation is the length of the object before this append operation (Content-Length).
result = await client.append('objectName', 'localFile', {
position: result.nextAppendPosition
})
}
append();References
For more information about the AppendObject API operation, see AppendObject.