All Products
Search
Document Center

Object Storage Service:Rename objects (iOS SDK)

Last Updated:Jun 16, 2026

OSS does not support renaming objects directly. To rename an object in the same bucket, call the CopyObject operation to copy the source object to a destination object. Then, call the DeleteObject operation to delete the source object.

Usage notes

  • Before you run the sample code, create an OSSClient instance by using a custom domain name or Security Token Service (STS). For more information, see Initialization (iOS SDK).

    Note

    The region of the bucket is determined by the endpoint region specified in the initialization configuration.

Sample code

The following sample code renames the srcobject.txt object to destobject.txt in the examplebucket bucket.

// 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;
}];
Note

OSS does not support renaming folders directly. To rename a folder, apply the same method to each subdirectory and object within the folder.

References

For more information about the API operations for renaming an object, see CopyObject and DeleteObject.