An append upload adds data to an existing object. This operation applies to Appendable objects, which allow you to add data to the end of the object. Common use cases include live video streaming and continuous updates for log files.
A simple upload in CloudBox creates a normal object, and a multipart upload in CloudBox creates a multipart object. The content of these two object types is fixed after the upload is complete. You can only read them, not modify them. To update the content of an object, you must upload a new object with the same name to overwrite the existing one.
In scenarios such as video monitoring and ApsaraVideo Live, traditional simple and multipart uploads have drawbacks, such as complex architecture and high latency. To solve these problems, OSS provides the append upload (AppendObject) feature. This feature lets you append video stream data to an object in real time.
To upload a video stream to an object in real time, you can gather the video data on your local machine and then use the append upload (AppendObject) feature. This process creates an Appendable object. You can directly append content to Appendable objects. The data from each append operation is readable immediately.
Benefits
Append uploads allow you to upload video data to the same object as soon as the data is generated. A client can then periodically check the object's length and compare it with the previously read length. If the client finds new data, it can trigger a read operation to retrieve only the newly appended data. This method simplifies the architecture and improves extensibility.
Limits
Size limits
The object size cannot exceed 5 GB.
Naming limits
Use UTF-8 encoding.
The length must be between 1 and 1,023 characters.
The name cannot start with a forward slash (/) or a backslash (\).
Operation limits
You cannot use append upload to upload Deep Cold Archive objects.
You cannot copy Appendable objects. However, you can modify the metadata of the object.
Append uploads do not support upload callbacks.
Procedure
Use an Alibaba Cloud SDK
You can append content to an existing Appendable object by using the OSS SDK for Java, Python, or Go (Java SDK 3.15.0 or later, Python SDK V2 1.1.1 or later, or Go SDK V2 v1.2.1 or later). For OSS on CloudBox, the Python and Go SDKs are called in the same way as they are in public cloud OSS. For information about how to create a client, see SDK support. The following code examples use the Java, Python, and Go SDKs.
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.AppendObjectRequest;
import com.aliyun.oss.model.AppendObjectResult;
import com.aliyun.oss.model.ObjectMetadata;
import java.io.ByteArrayInputStream;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
public class Demo {
public static void main(String[] args) throws Exception {
// Specify the data endpoint of the OSS on CloudBox bucket.
String endpoint = "https://cb-f8z7yvzgwfkl9q0h****.cn-hangzhou.oss-cloudbox.aliyuncs.com";
// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// Specify the name of the OSS on CloudBox bucket. Example: examplebucket.
String bucketName = "examplebucket";
// Specify the region where the OSS on CloudBox bucket is located.
String region = "cn-hangzhou";
// Specify the CloudBox ID.
String cloudBoxId = "cb-f8z7yvzgwfkl9q0h****";
// Specify the full path of the object. The full path cannot contain the bucket name. Example: exampledir/exampleobject.txt.
String objectName = "exampledir/exampleobject.txt";
String content1 = "Hello OSS A \n";
String content2 = "Hello OSS B \n";
String content3 = "Hello OSS C \n";
// Create an OSSClient instance.
// When the OSSClient instance is no longer used, call the shutdown method to release resources.
ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
conf.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(new DefaultCredentialProvider(credentialsProvider.getCredentials()))
.clientConfiguration(conf)
.region(region)
.cloudBoxId(cloudBoxId)
.build();
try {
ObjectMetadata meta = new ObjectMetadata();
// Specify the content type of the uploaded object.
meta.setContentType("text/plain");
// Use AppendObjectRequest to set multiple parameters.
AppendObjectRequest appendObjectRequest = new AppendObjectRequest(bucketName, objectName, new ByteArrayInputStream(content1.getBytes()),meta);
// First append operation.
// Set the position where the append operation starts.
appendObjectRequest.setPosition(0L);
AppendObjectResult appendObjectResult = ossClient.appendObject(appendObjectRequest);
// The 64-bit CRC value of the file.
System.out.println(appendObjectResult.getObjectCRC());
// Second append operation.
// nextPosition indicates the position for the next request, which is the current length of the file.
appendObjectRequest.setPosition(appendObjectResult.getNextPosition());
appendObjectRequest.setInputStream(new ByteArrayInputStream(content2.getBytes()));
appendObjectResult = ossClient.appendObject(appendObjectRequest);
// Third append operation.
appendObjectRequest.setPosition(appendObjectResult.getNextPosition());
appendObjectRequest.setInputStream(new ByteArrayInputStream(content3.getBytes()));
appendObjectResult = ossClient.appendObject(appendObjectRequest);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
# -*- coding: utf-8 -*-
import alibabacloud_oss_v2 as oss
# Obtain access credentials from environment variables. Before running this code example, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Use the default configuration of the SDK.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
# Specify the region in which the CloudBox is located, such as cn-hangzhou.
cfg.region = "cn-hangzhou"
# Specify the CloudBox ID. After you set the CloudBox ID, the SDK automatically switches the signing product to oss-cloudbox.
cfg.cloud_box_id = "cb-f8z7yvzgwfkl9q0h****"
# Specify the data domain in the CloudboxId.Region.oss-cloudbox.aliyuncs.com format.
cfg.endpoint = "cb-f8z7yvzgwfkl9q0h****.cn-hangzhou.oss-cloudbox.aliyuncs.com"
# Create an OSS client.
client = oss.Client(cfg)
# Specify the name of the bucket, such as examplebucket.
bucket_name = "examplebucket"
# Specify the full path of the object, such as exampledir/exampleobject.txt. The full path of the object cannot contain the bucket name.
object_name = "exampledir/exampleobject.txt"
content1 = "Hello OSS A. \n"
content2 = "Hello OSS B. \n"
content3 = "Hello OSS C. \n"
# First append.
# Set the append position to 0.
result = client.append_object(oss.AppendObjectRequest(
bucket=bucket_name,
key=object_name,
position=0,
body=content1.encode(),
))
# The 64-bit CRC value of the object.
print(f'object crc: {result.hash_crc64}')
# Second append.
# next_position specifies the position that must be provided in the next request, that is, the current length of the object.
result = client.append_object(oss.AppendObjectRequest(
bucket=bucket_name,
key=object_name,
position=result.next_position,
body=content2.encode(),
))
# Third append.
result = client.append_object(oss.AppendObjectRequest(
bucket=bucket_name,
key=object_name,
position=result.next_position,
body=content3.encode(),
))
print(f'next position: {result.next_position}')
package main
import (
"context"
"log"
"strings"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
func main() {
// Load the default configuration of the SDK and set the access credentials, region, CloudBox ID, and data domain.
// Obtain access credentials from environment variables. Before running this code example, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
// Specify the region in which the CloudBox is located, such as cn-hangzhou.
WithRegion("cn-hangzhou").
// Specify the data domain in the CloudboxId.Region.oss-cloudbox.aliyuncs.com format.
WithEndpoint("cb-f8z7yvzgwfkl9q0h****.cn-hangzhou.oss-cloudbox.aliyuncs.com").
// Specify the CloudBox ID. After you set the CloudBox ID, the SDK automatically switches the signing product to oss-cloudbox.
WithCloudBoxId("cb-f8z7yvzgwfkl9q0h****")
// Create an OSS client.
client := oss.NewClient(cfg)
// Specify the name of the bucket, such as examplebucket.
bucketName := "examplebucket"
// Specify the full path of the object, such as exampledir/exampleobject.txt. The full path of the object cannot contain the bucket name.
objectName := "exampledir/exampleobject.txt"
// First append. The append position is 0.
request := &oss.AppendObjectRequest{
Bucket: oss.Ptr(bucketName),
Key: oss.Ptr(objectName),
Position: oss.Ptr(int64(0)),
Body: strings.NewReader("Hello OSS A. \n"),
}
result, err := client.AppendObject(context.TODO(), request)
if err != nil {
log.Fatalf("failed to append object %v", err)
}
// The 64-bit CRC value of the object.
log.Printf("object crc: %v\n", oss.ToString(result.HashCRC64))
// Second append. NextPosition specifies the position that must be provided in the next request, that is, the current length of the object.
request.Position = oss.Ptr(result.NextPosition)
request.Body = strings.NewReader("Hello OSS B. \n")
result, err = client.AppendObject(context.TODO(), request)
if err != nil {
log.Fatalf("failed to append object %v", err)
}
// Third append.
request.Position = oss.Ptr(result.NextPosition)
request.Body = strings.NewReader("Hello OSS C. \n")
result, err = client.AppendObject(context.TODO(), request)
if err != nil {
log.Fatalf("failed to append object %v", err)
}
log.Printf("next position: %v\n", result.NextPosition)
}
Use ossutil
For more information about how to use ossutil to perform an append upload, see append-object.
Use the REST API
If your program requires a high degree of customization, you can make REST API requests directly. When you use the REST API, you must manually write code to calculate the signature. For more information, see AppendObject.