You can call the AppendObject operation to append content to existing appendable objects.
Objects uploaded by calling the AppendObject operation are appendable objects. Objects uploaded by calling the PutObject operation are normal objects.
Usage notes
Before you run the sample code in this topic, you must create an OSSClient instance by using methods such as using a custom domain name or Security Token Service (STS). For more information, see Initialization.
When you upload objects by performing 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.
Examples
The following sample code provides an example on how to perform append upload:
b/ 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.
// Do not include the bucket name in the full path of the object.
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 callbacks.
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.
}
});
The following sample code provides an example on how to use the URI of a file to upload the file to OSS for partition storage on Android 10 or later:
// Specify the name of the bucket and the full path of the object. In this example, the name of the bucket is examplebucket and the full path of the object is exampledir/exampleobject.txt.
// Do not include the bucket name in the full path of the object.
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 callbacks.
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 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.
For more information about how to initialize an OSSClient instance, see Initialization.