Puede llamar a la operación AppendObject para anexar contenido a objetos anexables existentes.
Los objetos que se cargan con la operación AppendObject son objetos anexables. Los objetos que se cargan con la operación PutObject son objetos normales.
Notas de uso
Antes de ejecutar el código de ejemplo de este tema, cree una instancia de OSSClient mediante métodos como el uso de un nombre de dominio personalizado o Security Token Service (STS). Para obtener más información, consulte Initialization (Android SDK).
Cuando carga un objeto mediante la anexión de datos, establezca correctamente el parámetro position.
Al crear un objeto anexable, establezca el parámetro position en 0.
-
Al anexar contenido a un objeto anexable, establezca el parámetro position con la longitud actual del objeto.
La longitud del objeto se obtiene de la respuesta de la carga por anexión anterior o mediante la operación HeadObject.
Permisos
De forma predeterminada, una cuenta de Alibaba Cloud tiene todos los permisos. Los usuarios o roles de RAM asociados a una cuenta de Alibaba Cloud no cuentan con ningún permiso de manera predeterminada. La cuenta de Alibaba Cloud o el administrador de la cuenta deben otorgar los permisos de operación a través de RAM policies o Bucket Policy.
|
API |
Action |
Descripción |
|
AppendObject |
|
Esta operación permite cargar un objeto y anexarlo a uno ya existente. |
|
|
Este permiso es obligatorio cuando se especifican etiquetas de objeto mediante x-oss-tagging durante una carga por anexión. |
Ejemplos de código
A continuación se muestra cómo anexar datos a un objeto.
// Specify the bucket name, the full path of the object, and the full path of the local file. Example: examplebucket, exampledir/exampleobject.txt, and /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);
// Set the append position.
append.setPosition(0);
// Set the callback.
append.setProgressCallback(new OSSProgressCallback<AppendObjectRequest>() {
@Override
public void onProgress(AppendObjectRequest request, long currentSize, long totalSize) {
Log.d("AppendObject", "currentSize: " + currentSize + " totalSize: " + totalSize);
}
});
// Asynchronously append data.
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.
}
});
Para el almacenamiento con ámbito (scoped storage) en Android 10 y versiones posteriores, puede usar el URI de un archivo para cargarlo en OSS.
// Specify the bucket name and the full path of the object. Example: examplebucket and 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);
// Set the append position.
append.setPosition(0);
// Set the callback.
append.setProgressCallback(new OSSProgressCallback<AppendObjectRequest>() {
@Override
public void onProgress(AppendObjectRequest request, long currentSize, long totalSize) {
Log.d("AppendObject", "currentSize: " + currentSize + " totalSize: " + totalSize);
}
});
// Asynchronously append data.
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.
}
});
Referencias
Para consultar el código de ejemplo completo sobre cargas por anexión, consulte el ejemplo en GitHub.
Para obtener información sobre la operación de API correspondiente a las cargas por anexión, consulte AppendObject.
Si necesita detalles sobre cómo inicializar un OSSClient, consulte Initialize an OSSClient instance for Android.