After you enable the hierarchical namespace feature for a bucket, you can rename objects in the bucket.
Usage notes
- If the hierarchical namespace feature is enabled for a bucket, you can rename an object
in the bucket by calling the Rename operation.
For more information about the hierarchical namespace feature, see Hierarchical namespace.
- If the hierarchical namespace feature is not enabled for a bucket, you cannot directly rename an object in the bucket. To rename an object in the bucket, you can call the CopyObject operation to copy the source object to the destination object and call the DeleteObject operation to delete the source object.
-
To ensure the availability of the OSS-HDFS service or prevent data loss, do not rename the
.dlsdata/
directory in which OSS-HDFS data is stored in a bucket for which hierarchical namespace and the OSS-HDFS service are enabled.
Use the OSS console
You can rename objects of all sizes in the OSS console. However, you can move only objects that do not exceed 1 GB in size in the OSS console.
Use OSS SDKs
The following code provides examples on how to rename an object by using OSS SDKs for common programming languages. For more information about how to rename an object by using OSS SDKs for other programming languages, see Overview.
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.RenameObjectRequest;
public class Demo {
public static void main(String[] args) throws Exception {
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint.
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console.
String accessKeyId = "yourAccessKeyId";
String accessKeySecret = "yourAccessKeySecret";
// Specify the name of the bucket.
String bucketName = "examplebucket";
// Specify the full path of the source object. Do not include the bucket name in the full path.
String sourceObject = "srcobject.txt";
// Specify the full path of the destination object. Do not include the bucket name in the full path.
String destinationObject = "destobject.txt";
// Create an OSSClient instance.
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
// If the hierarchical namespace feature is enabled for the bucket, use the following code to rename objects.
// Change the absolute path of the source object in the bucket to the absolute path of the destination object.
RenameObjectRequest renameObjectRequest = new RenameObjectRequest(bucketName, sourceObject, destinationObject);
ossClient.renameObject(renameObjectRequest);
// If the hierarchical namespace is not enabled for the bucket, use the following code to rename objects.
// Copy the srcobject.txt object in the examplebucket bucket to the destobject.txt object in the same bucket.
// ossClient.copyObject(bucketName, sourceObject, bucketName, destinationObject);
// Delete the srcobject.txt object.
// ossClient.deleteObject(bucketName, sourceObject);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
<?php
if (is_file(__DIR__ . '/../autoload.php')) {
require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\OssClient;
use OSS\Core\OssException;
// The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in Object Storage Service (OSS) is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console.
$accessKeyId = "yourAccessKeyId";
$accessKeySecret = "yourAccessKeySecret";
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the bucket name. Example: examplebucket.
$bucket= "examplebucket";
// Specify the full path of the source object. The full path of the object cannot contain the bucket name. Example: srcobject.txt.
$fromObject = "srcobject.txt";
// Specify the full path of the destination object. The full path of the object cannot contain the bucket name. Example: destobject.txt.
$toObject = 'destobject.txt';
try {
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
// Copy the srcobject.txt object in examplebucket to the destobject.txt object in the same bucket.
$ossClient->copyObject($bucket, $fromObject,$bucket, $toObject);
// Delete the srcobject.txt object.
$ossClient->deleteObject($bucket, $fromObject);
} catch (OssException $e) {
printf($e->getMessage() . "\n");
return;
}
print("Object ".$fromObject ." Rename complete" . PHP_EOL);
const OSS = require('ali-oss');
const client = new OSS({
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
region: 'oss-cn-hangzhou',
// The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in Object Storage Service (OSS) is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console.
accessKeyId: 'yourAccessKeyId',
accessKeySecret: 'yourAccessKeySecret',
// Specify the bucket name.
bucket: 'examplebucket',
})
// Copy the srcobject.txt object in examplebucket to the destobject.txt object in the same bucket.
client.copy('destobject.txt', 'srcobject.txt')
.then(r => {
console.log ('Copied', r);
client
// Delete the srcobject.txt object.
.delete('srcobject.txt')
.then(r => console.log(r))
.catch(e => console.log(e));
})
.catch(e => console.log(e));
# -*- coding: utf-8 -*-
import oss2
# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in Object Storage Service (OSS) is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console.
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# Specify the bucket name. Example: examplebucket.
bucket_name = 'examplebucket'
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
bucket = oss2.Bucket(auth, 'yourEndpoint', bucket_name)
# Specify the full path of the source object. The full path of the object cannot contain the bucket name. Example: srcobject.txt.
src_object_name = 'srcobject.txt'
# Specify the full path of the destination object. The full path of the object cannot contain the bucket name. Example: destobject.txt.
dest_object_name = 'destobject.txt'
# Copy the srcobject.txt object in examplebucket to the destobject.txt object in the same bucket.
result = bucket.copy_object(bucket_name, src_object_name, dest_object_name)
# Display the HTTP status code in the returned results. If HTTP status code 200 is returned, the srcobject.txt object is copied to the destobject.txt object.
print('result.status:', result.status)
# Delete the srcobject.txt object.
result_del = bucket.delete_object(src_object_name)
# Display the HTTP status code in the returned results. If HTTP status code 204 is returned, the object is deleted.
print('result.status:', result_del.status)
// Specify the bucket name.
String bucketName = "examplebucket";
// Specify the full path of the source object. The full path of the object cannot contain the bucket name. Example: srcobject.txt.
String sourceObjectKey = "srcobject.txt";
// Specify the full path of the destination object. The full path of the object cannot contain the bucket name. Example: destobject.txt.
String objectKey = "destobject.txt";
try {
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucketName, sourceObjectKey, bucketName, objectKey);
oss.copyObject(copyObjectRequest);
// Delete the srcobject.txt object.
DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(bucketName, sourceObjectKey);
oss.deleteObject(deleteObjectRequest);
} catch (ClientException e) {
// Handle client-side exceptions such as network errors.
e.printStackTrace();
} catch (ServiceException e) {
// Handle server-side exceptions.
Log.e("RequestId", e.getRequestId());
Log.e("ErrorCode", e.getErrorCode());
Log.e("HostId", e.getHostId());
Log.e("RawMessage", e.getRawMessage());
}
package main
import (
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"os"
)
func handleError(err error) {
fmt.Println("Error:", err)
os.Exit(-1)
}
func main() {
// Create an OSSClient instance.
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
// The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in Object Storage Service (OSS) is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console.
client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
if err != nil {
handleError(err)
}
// Specify the bucket name.
bucket, err := client.Bucket("examplebucket")
if err != nil {
handleError(err)
}
// Specify the full path of the source object. The full path of the object cannot contain the bucket name.
srcObject := "srcobject.txt"
// Specify the full path of the destination object. The full path of the object cannot contain the bucket name.
destObject := "destobject.txt"
// Copy the srcobject.txt object in examplebucket to the destobject.txt object in the same bucket.
_,err = bucket.CopyObject(srcObject,destObject)
if err != nil {
handleError(err)
}
// Delete the srcobject.txt object.
err = bucket.DeleteObject(srcObject)
if err != nil {
handleError(err)
}
fmt.Print(srcObject+" has renamed "+destObject)
}
// Specify the bucket name.
NSString *bucketName = @"examplebucket";
// Specify the full path of the source object. The full path of the object cannot contain the bucket name. Example: srcobject.txt.
NSString *sourceObjectKey = @"sourceObjectKey";
// Specify the full path of the destination object. The full path of the object cannot contain the bucket name. Example: destobject.txt.
NSString *objectKey = @"destobject.txt";
[[[OSSTask taskWithResult:nil] continueWithBlock:^id _Nullable(OSSTask * _Nonnull task) {
// Copy the srcobject.txt object in examplebucket to the destobject.txt object 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;
}];
Use ossutil
For more information about how to use ossutil to rename objects, see Copy objects.
Use RESTful APIs
If your business requires a high level of customization, you can directly call RESTful APIs. To directly call an API, you must include the signature calculation in your code. For more information, see Rename.