本文介绍如何通过 TOS Java SDK 重命名对象。
objectA
重命名为 objectA
。objectA
和 objectB
两个对象,将 objectA
重命名为 objectB
,则 objectB
数据会被覆盖。更多信息,请参见使用 RenameObject。import com.volcengine.tos.TOSV2; import com.volcengine.tos.TOSV2ClientBuilder; import com.volcengine.tos.TosClientException; import com.volcengine.tos.TosServerException; import com.volcengine.tos.model.object.RenameObjectInput; import com.volcengine.tos.model.object.RenameObjectOutput; public class RenameObjectExample { public static void main(String[] args) { String endpoint = "your endpoint"; String region = "your region"; String accessKey = System.getenv("TOS_ACCESS_KEY"); String secretKey = System.getenv("TOS_SECRET_KEY"); String bucketName = "bucket-example"; String objectKey = "example_dir/example_object.txt"; String renamedObjectKey = "example_dir/example_object_rename.txt"; TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey); try{ RenameObjectInput input = new RenameObjectInput().setBucket(bucketName).setKey(objectKey) .setNewKey(renamedObjectKey); RenameObjectOutput output = tos.renameObject(input); System.out.println(output.getRequestInfo().getRequestId()); System.out.println("renameObject succeed."); } catch (TosClientException e) { // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送 System.out.println("renameObject failed"); System.out.println("Message: " + e.getMessage()); if (e.getCause() != null) { e.getCause().printStackTrace(); } } catch (TosServerException e) { // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息 System.out.println("renameObject failed"); System.out.println("StatusCode: " + e.getStatusCode()); System.out.println("RequestID: " + e.getRequestID()); } catch (Throwable t) { // 作为兜底捕获其他异常,一般不会执行到这里 System.out.println("renameObject failed"); System.out.println("unexpected exception, message: " + t.getMessage()); } } }
关于 RenameObject 的 API 文档,请参见 RenameObject。