import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import com.volcengine.tos.TOSV2;
import com.volcengine.tos.TOSV2ClientBuilder;
import com.volcengine.tos.TosException;
import com.volcengine.tos.comm.common.ACLType;
import com.volcengine.tos.comm.common.StorageClassType;
import com.volcengine.tos.model.object.AppendObjectInput;
import com.volcengine.tos.model.object.AppendObjectOutput;
import com.volcengine.tos.model.object.ObjectMetaRequestOptions;
import java.io.ByteArrayInputStream;
import java.util.Arrays;
import java.util.HashMap;
public class AppendObjectExample extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
String endpoint = "your endpoint";
String region = "your region";
String accessKey = "your access key";
String secretKey = "your secret key";
String securityToken = "your security token";
String bucketName = "your bucket name";
String objectKey = "your object key";
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey, securityToken);
Thread tosThread = new Thread(new Runnable() {
byte[] data1 = new byte[128 * 1024];
Arrays.fill(data1, (byte) 'A');
ByteArrayInputStream stream = new ByteArrayInputStream(data1);
ObjectMetaRequestOptions options = new ObjectMetaRequestOptions();
options.setAclType(ACLType.ACL_PRIVATE);
options.setStorageClass(StorageClassType.STORAGE_CLASS_STANDARD);
options.setContentType("application/json");
Map<String, String> custom = new HashMap<>();
custom.put("name", "volc_user");
options.setCustomMetadata(custom);
long contentLength = data1.length;
AppendObjectInput input = new AppendObjectInput().setBucket(bucketName).setKey(objectKey)
.setContent(stream).setContentLength(contentLength).setOffset(offset).setOptions(options);
AppendObjectOutput output = tos.appendObject(input);
Log.i("appendObject", "appendObject first time succeed, object's nextAppendOffset is " + output.getNextAppendOffset());
Log.i("appendObject", "appendObject first time succeed, object's crc64 is " + output.getHashCrc64ecma());
byte[] data2 = new byte[128 * 1024 + 1024];
Arrays.fill(data2, (byte) 'B');
contentLength = data2.length;
String preHashCrc64 = output.getHashCrc64ecma();
input = new AppendObjectInput().setBucket(bucketName).setKey(objectKey).setContent(new ByteArrayInputStream(data2))
.setOffset(offset).setContentLength(contentLength).setPreHashCrc64ecma(preHashCrc64);
output = tos.appendObject(input);
Log.i("appendObject", "appendObject second time succeed, object's nextAppendOffset is " + output.getNextAppendOffset());
Log.i("appendObject", "appendObject second time succeed, object's crc64 is " + output.getHashCrc64ecma());
byte[] data3 = new byte[256 * 1024 + 1024];
Arrays.fill(data3, (byte) 'C');
contentLength = data3.length;
offset = data1.length + data2.length;
preHashCrc64 = output.getHashCrc64ecma();
input = new AppendObjectInput().setBucket(bucketName).setKey(objectKey).setContent(new ByteArrayInputStream(data3))
.setOffset(offset).setContentLength(contentLength).setPreHashCrc64ecma(preHashCrc64);
output = tos.appendObject(input);
Log.i("appendObject", "appendObject third time succeed, object's nextAppendOffset is " + output.getNextAppendOffset());
Log.i("appendObject", "appendObject third time succeed, object's crc64 is " + output.getHashCrc64ecma());
} catch (TosException e) {
Log.e("TosException", "appendObject failed");