TOS 支持桶内单个对象的重命名功能,当您想要使用重命名对象功能时需要先开启桶的重命名功能。
renameEnable
参数只能为 True
。以下代码用于设置桶 examplebucket 开启重命名功能。
#include "TosClientV2.h" using namespace VolcengineTos; int main(void){ // 初始化 TOS 账号信息 // Your Region 填写 Bucket 所在 Region std::string region = "Your Region"; std::string accessKey = std::getenv("TOS_ACCESS_KEY"); std::string secretKey = std::getenv("TOS_SECRET_KEY"); // 填写 Bucket 名称,例如 examplebucket std::string bucketName = "examplebucket"; // 初始化网络等资源 InitializeClient(); // 创建交互的 client TosClientV2 client(region, accessKey, secretKey); PutBucketRenameInput input(bucketName, true); auto output = client.putBucketRename(input); if (!output.isSuccess()) { // 异常处理 std::cout << "PutBucketRename failed." << output.error().String() << std::endl; // 释放网络等资源 CloseClient(); return -1; } std::cout << "PutBucketRename success." << std::endl; // 释放网络等资源 CloseClient(); return 0; }
获取桶的重命名规则,默认您必须为桶所有者。
以下代码用于获取桶 examplebucket 重命名规则。
#include "TosClientV2.h" using namespace VolcengineTos; int main(void){ // 初始化 TOS 账号信息 // Your Region 填写 Bucket 所在 Region std::string region = "Your Region"; std::string accessKey = std::getenv("TOS_ACCESS_KEY"); std::string secretKey = std::getenv("TOS_SECRET_KEY"); // 填写 Bucket 名称,例如 examplebucket std::string bucketName = "examplebucket"; // 初始化网络等资源 InitializeClient(); // 创建交互的 client TosClientV2 client(region, accessKey, secretKey); GetBucketRenameInput input(bucketName); auto output = client.getBucketRename(input); if (!output.isSuccess()) { // 异常处理 std::cout << "GetBucketRename failed." << output.error().String() << std::endl; // 释放网络等资源 CloseClient(); return -1; } std::cout << "GetBucketRename success" << std::endl; // 释放网络等资源 CloseClient(); return 0; }
删除桶的重命名规则,默认您必须为桶所有者。
以下代码用于删除桶 examplebucket 重命名规则。
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.bucket.DeleteBucketRenameInput; import com.volcengine.tos.model.bucket.DeleteBucketRenameOutput; public class DeleteBucketRenameExample { 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"; TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey); try{ DeleteBucketRenameInput input = new DeleteBucketRenameInput().setBucket(bucketName); DeleteBucketRenameOutput output = tos.deleteBucketRename(input); System.out.println("deleteBucketRename succeed, " + output); } catch (TosClientException e) { // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送 System.out.println("deleteBucketRename failed"); System.out.println("Message: " + e.getMessage()); if (e.getCause() != null) { e.getCause().printStackTrace(); } } catch (TosServerException e) { // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息 System.out.println("deleteBucketRename failed"); System.out.println("StatusCode: " + e.getStatusCode()); System.out.println("Code: " + e.getCode()); System.out.println("Message: " + e.getMessage()); System.out.println("RequestID: " + e.getRequestID()); } catch (Throwable t) { // 作为兜底捕获其他异常,一般不会执行到这里 System.out.println("deleteBucketPolicy failed"); System.out.println("unexpected exception, message: " + t.getMessage()); } } }