You can call AppendObject to append content to append objects that are uploaded.

Note
  • For the complete code of append upload, visit GitHub.
  • Objects that are uploaded by using append upload are append objects. You can append content only to append objects. For more information about append upload, see Append upload in Object Storage Service (OSS) Developer Guide and AppendObject in OSS API Reference.

Usage notes

When you call AppendObject to append content to an object, take note of the following items:

  • If the object to which you want to append content does not exist, an append object is created when you call AppendObject.
  • If the object to which you want to append content exists, the following scenarios may occur when you call AppendObject: 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.

Examples

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 OSS because the account has permissions on all API operations. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M. 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. 
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. 
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 you want to upload. 
meta.setContentType("text/plain");

// 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. The object name indicates the full path of the object and does not contain the bucket name. Example: example/test.txt. 
//appendObjectRequest.setKey(objectName);
// Specify the content you want to append. Two types of content are available: InputStream and File. The following code provides an example on how to specify the content of the InputStream type. 
//appendObjectRequest.setInputStream(new ByteArrayInputStream(content1.getBytes()));
// Specify the content you want to append. Two types of content are available: InputStream and File. The following code provides an example on 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 append operation on the object for the first time. 
//appendObjectRequest.setMetadata(meta);

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

// Start the second append. 
// NextPosition indicates 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 your OSSClient. 
ossClient.shutdown();      

For more information about the naming conventions for buckets, see bucket. For more information about the naming conventions for objects, see object.