You can call the AppendObject operation to append content to existing appendable objects.
Note Objects uploaded by calling the AppendObject operation are appendable objects. Objects uploaded by calling the PutObject operation are normal objects.
When you upload objects by using append upload, you must configure the correct position from which the append operation starts.
- When you create an appendable object, set the position from which the append operation starts to 0.
- When you append content to an appendable object, set the position from which the append operation starts to the current length of the object.
You can obtain the length of the object from the returned message after append upload or by calling HeadObject.
Sample code
The following code provides an example on how to perform append upload:
// Specify the name of the bucket, the full path of the object, and the full path of the local file. In this example, the name of the bucket is examplebucket, the full path of the object is exampledir/exampleobject.txt, and the full path of the local file is /storage/emulated/0/oss/examplefile.txt.
// The full path of the object cannot contain the bucket name.
AppendObjectRequest append = new AppendObjectRequest("examplebucket", "exampledir/exampleobject.txt", "/storage/emulated/0/oss/examplefile.txt");
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType("text/plain");
append.setMetadata(metadata);
// Specify the position from which the append operation starts.
append.setPosition(0);
// Configure a callback function.
append.setProgressCallback(new OSSProgressCallback<AppendObjectRequest>() {
@Override
public void onProgress(AppendObjectRequest request, long currentSize, long totalSize) {
Log.d("AppendObject", "currentSize: " + currentSize + " totalSize: " + totalSize);
}
});
// Perform the append upload operation in the asynchronous mode.
OSSAsyncTask task = oss.asyncAppendObject(append, new OSSCompletedCallback<AppendObjectRequest, AppendObjectResult>() {
@Override
public void onSuccess(AppendObjectRequest request, AppendObjectResult result) {
Log.d("AppendObject", "AppendSuccess");
Log.d("NextPosition", "" + result.getNextPosition());
}
@Override
public void onFailure(AppendObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
// Handle exceptions.
}
});
For partition storage of Android 10 and later versions, you can use the Uniform Resource Identifier (URI) of an object to upload the object to Object Storage Service (OSS).
// Specify the bucket name such as examplebucket and the full path of the object such as exampledir/exampleobject.txt.
// The full path of the object cannot contain the bucket name.
AppendObjectRequest append = new AppendObjectRequest("examplebucket", "exampledir/exampleobject.txt", fileUri);
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType("text/plain");
append.setMetadata(metadata);
// Specify the position from which the append operation starts.
append.setPosition(0);
// Configure a callback function.
append.setProgressCallback(new OSSProgressCallback<AppendObjectRequest>() {
@Override
public void onProgress(AppendObjectRequest request, long currentSize, long totalSize) {
Log.d("AppendObject", "currentSize: " + currentSize + " totalSize: " + totalSize);
}
});
// Perform the append upload operation in the asynchronous mode.
OSSAsyncTask task = oss.asyncAppendObject(append, new OSSCompletedCallback<AppendObjectRequest, AppendObjectResult>() {
@Override
public void onSuccess(AppendObjectRequest request, AppendObjectResult result) {
Log.d("AppendObject", "AppendSuccess");
Log.d("NextPosition", "" + result.getNextPosition());
}
@Override
public void onFailure(AppendObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
// Handle exceptions.
}
});
References
- For more information about the complete sample code that is used to perform append upload, visit GitHub.
- For more information about the API operation that you can call to perform append upload, see AppendObject.