Todos los productos
Search
Centro de documentos

Object Storage Service:Cambiar el nombre de objetos (iOS SDK)

Última actualización: Sep 22, 2026

OSS no admite cambiar el nombre de los objetos directamente. Para hacerlo, copie el objeto y luego elimine el original: llame a CopyObject para crear un objeto nuevo con el nombre deseado y después llame a DeleteObject para quitar el objeto de origen.

Notas

  • Antes de ejecutar el código de ejemplo, cree una instancia de OSSClient mediante un dominio personalizado o Security Token Service (STS). Para obtener más información, consulte Initialization (iOS SDK).

    Nota

    La región del bucket la determina la región del endpoint que se especifique en la configuración de inicialización.

Código de ejemplo

A continuación se muestra un código de ejemplo que cambia el nombre del objeto srcobject.txt a destobject.txt dentro del bucket examplebucket.

// Specify the bucket name.
NSString *bucketName = @"examplebucket";
// Specify the full path of the source object. Do not include the bucket name. For example, srcobject.txt.
NSString *sourceObjectKey = @"sourceObjectKey";
// Specify the full path of the destination object. Do not include the bucket name. For example, destobject.txt.
NSString *objectKey = @"destobject.txt";
[[[OSSTask taskWithResult:nil] continueWithBlock:^id _Nullable(OSSTask * _Nonnull task) {
    // Copy the srcobject.txt object to destobject.txt in the same bucket.
    OSSCopyObjectRequest *copyRequest = [OSSCopyObjectRequest new];
    copyRequest.bucketName = bucketName;
    copyRequest.sourceBucketName = bucketName;
    copyRequest.sourceObjectKey = sourceObjectKey;
    copyRequest.objectKey = objectKey;
    OSSTask *copyTask = [client copyObject:copyRequest];
    [copyTask waitUntilFinished];
    if (copyTask.error) {
        return copyTask;
    }
    // Delete the srcobject.txt object.
    OSSDeleteObjectRequest *deleteObject = [OSSDeleteObjectRequest new];
    deleteObject.bucketName = bucketName;
    deleteObject.objectKey = sourceObjectKey;
    OSSTask *deleteTask = [client deleteObject:deleteObject];
    [deleteTask waitUntilFinished];
    if (deleteTask.error) {
        return deleteTask;
    }
    return nil;
}] continueWithBlock:^id _Nullable(OSSTask * _Nonnull task) {
    if (task.error) {
        NSLog(@"rename fail! error: %@", task.error);
    } else {
        NSLog(@"rename success!");
    }
    return nil;
}];
Nota

OSS no admite cambiar el nombre de las carpetas directamente. Para lograrlo, aplique este mismo método a cada subdirectorio y objeto dentro de la carpeta.

Referencias

Para obtener más información sobre las operaciones de API que permiten cambiar el nombre de un objeto, consulte CopyObject y DeleteObject.