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

Usage notes

  • 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.

Sample code

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

// Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
String endpoint = "yourEndpoint";
// 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. 
String accessKeyId = "yourAccessKeyId";
String accessKeySecret = "yourAccessKeySecret";
// Specify the name of the bucket. Example: examplebucket. For more information about the naming conventions for buckets, see bucket. 
String bucketName = "examplebucket";
// Specify the full path of the object. Example: exampledir/exampleobject.txt. The full path of the object cannot contain the bucket name. For more information about the naming conventions for objects, see object. 
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. 
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

ObjectMetadata meta = new ObjectMetadata();
// Specify the type of content that you want to upload. 
meta.setContentType("text/plain");
// Specify the caching behavior of the web page for the object. 
//meta.setCacheControl("no-cache");
// Specify the name of the object when the object is downloaded. 
//meta.setContentDisposition("attachment;filename=oss_download.txt");
// Specify the encoding format for the content of the object. 
//meta.setContentEncoding(OSSConstants.DEFAULT_CHARSET_NAME);
// 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. 
//meta.setContentMD5("ohhnqLBJFiKkPSBO1eNaUA==");
// Specify the expiration time. 
//try {
//    meta.setExpirationTime(DateUtil.parseRfc822Date("Wed, 08 Jul 2022 16:57:01 GMT"));
//} catch (ParseException e) {
//    e.printStackTrace();
//}
// Specify the server-side encryption method. The following sample code shows how to set the encryption method to server-side encryption by using OSS-managed keys (SSE-OSS): 
//meta.setServerSideEncryption(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
// Specify the access control list (ACL) of the object. The following sample code shows how to set the ACL of the object to private: 
//meta.setObjectAcl(CannedAccessControlList.Private);
// Specify the storage class of the object. 
//meta.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.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 an existing append object. Parameters whose names are prefixed with x-oss-meta- are considered the metadata of the object. 
//meta.setHeader("x-oss-meta-author", "Alice");

// Configure multiple parameters by using AppendObjectRequest. 
AppendObjectRequest appendObjectRequest = new AppendObjectRequest(bucketName, objectName, new ByteArrayInputStream(content1.getBytes()),meta);

// Configure a single parameter by using AppendObjectRequest. 
// Specify the bucket name. 
//appendObjectRequest.setBucketName(bucketName);
// Specify the object name. 
//appendObjectRequest.setKey(objectName);
// Specify the content that you want to append. Two types of content are available: InputStream and File. The following sample code shows how to specify the content of the InputStream type: 
//appendObjectRequest.setInputStream(new ByteArrayInputStream(content1.getBytes()));
// Specify the content that you want to append. Two types of content are available: InputStream and File. The following sample code shows how to specify the content of the File type: 
//appendObjectRequest.setFile(new File("D:\\localpath\\examplefile.txt"));
// Specify the object metadata. You can specify the metadata of an object only when you perform the first append operation on the object. 
//appendObjectRequest.setMetadata(meta);

// Perform the first append operation. 
// Specify the position from which the append operation starts. 
appendObjectRequest.setPosition(0L);
AppendObjectResult appendObjectResult = ossClient.appendObject(appendObjectRequest);
// Calculate the CRC-64 value of the object. The value is calculated based on the ECMA-182 standard. 
System.out.println(appendObjectResult.getObjectCRC());

// Perform the second append operation. 
// NextPosition specifies the position from which the next append operation starts, which is the current length of the object. 
appendObjectRequest.setPosition(appendObjectResult.getNextPosition());
appendObjectRequest.setInputStream(new ByteArrayInputStream(content2.getBytes()));
appendObjectResult = ossClient.appendObject(appendObjectRequest);

// Perform the third append operation. 
appendObjectRequest.setPosition(appendObjectResult.getNextPosition());
appendObjectRequest.setInputStream(new ByteArrayInputStream(content3.getBytes()));
appendObjectResult = ossClient.appendObject(appendObjectRequest);

// Shut down the OSSClient instance. 
ossClient.shutdown();      

References

  • For the complete sample code of append upload, visit GitHub.
  • For the API operation that you can call to perform append upload, see AppendObject.