桶(Bucket)是 TOS 的全局唯一的命名空间,相当于数据的容器,用来储存对象(Object)数据。当您不再需要保留某个桶(Bucket)时,可将其删除,以免产生额外费用。本文介绍如何通过 TOS Java SDK 的 deleteBucket
接口删除一个指定的桶。
tos:DeleteBucket
权限。具体操作,请参见权限配置概述。删除桶之前,请确保您已经删除桶中所有数据。
如下代码展示如何删除一个空桶。
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.DeleteBucketInput; import com.volcengine.tos.model.bucket.DeleteBucketOutput; public class DeleteBucketExample { 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 = "your bucket name"; TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey); try{ DeleteBucketInput input = new DeleteBucketInput().setBucket(bucketName); DeleteBucketOutput output = tos.deleteBucket(input); System.out.println("deleteBucket succeed, " + output); } catch (TosClientException e) { // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送 System.out.println("deleteBucket failed"); System.out.println("Message: " + e.getMessage()); if (e.getCause() != null) { e.getCause().printStackTrace(); } } catch (TosServerException e) { // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息 System.out.println("deleteBucket 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("deleteBucket failed"); System.out.println("unexpected exception, message: " + t.getMessage()); } } }
关于删除桶的 API 文档,请参见 DeleteBucket。