Object Storage Service (OSS) does not support renaming objects directly. To rename an object, call CopyObject to copy it to a new key in the same bucket, and then call DeleteObject to delete the original.
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 (Android SDK).
Sample code
The following code renames the srcobject.txt object to destobject.txt in the examplebucket bucket.
// Specify the bucket name.
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 source 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 does not support directly renaming a folder. To rename a folder, you must rename each object and subdirectory in the folder.
References
For more information about the API operations for renaming files, see CopyObject and DeleteObject.
For OSSClient initialization, see Initialize an OSSClient instance for Android.