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 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.
示例代码
以下代码用于将examplebucket下的srcobject.txt重命名为destobject.txt。
// 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. Example: srcobject.txt.
String sourceObjectKey = "srcobject.txt";
// Specify the full path of the destination object. Do not include the bucket name in the full path. 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());
}OSS也不支持直接对目录进行重命名。如果需要重命名目录,您可以参考以上示例对该目录下的子目录和Object逐个进行重命名操作。
References
For more information about the API operations that you can call to rename an object, see CopyObject and DeleteObject.
For more information about how to initialize an OSSClient instance, see Initialize an OSSClient instance for Android.