package com.wj.other; import com.volcengine.tos.*; import com.volcengine.tos.auth.StaticCredentials; import com.volcengine.tos.model.object.DeleteObjectInput; import com.volcengine.tos.model.object.ListObjectsType2Input; import com.volcengine.tos.model.object.ListObjectsType2Output; public class DeleteObjectWithPrefixExample { public static void main(String[] args) { String endpoint = "yuyi-realme-storage.tos-cn-shanghai.volces.com"; String region = "cn-shanghai"; String accessKey = "AKLTYWMzMWNlMDY2ZTRiNDIxMzhlMWJjNTJiOTg0YzAxNjM"; String secretKey = "TWpsa1lqUmxNekkyTkRjNU5EZGhNbUUwTUdKak1qaGxPV05rT1dVeE56WQ=="; String bucketName = "yuyi-realme-storage"; String prefix = null; TOSClientConfiguration configuration = TOSClientConfiguration.builder() .region(region) .endpoint(endpoint) .isCustomDomain(true) // 标识当前域名为自定义域名 .credentials(new StaticCredentials(accessKey, secretKey)) .build(); TOSV2 tos = new TOSV2ClientBuilder().build(configuration); try{ boolean isTruncated = true; String continuationToken = null; while (isTruncated) { ListObjectsType2Input input = new ListObjectsType2Input().setBucket(bucketName) .setPrefix(prefix).setContinuationToken(continuationToken); ListObjectsType2Output output = tos.listObjectsType2(input); if (output.getContents() != null){ for (int i = 0; i < output.getContents().size(); i++) { String objectKey = output.getContents().get(i).getKey(); DeleteObjectInput deleteInput = new DeleteObjectInput().setBucket(bucketName) .setKey(output.getContents().get(i).getKey()); tos.deleteObject(deleteInput); System.out.println("deleteObject succeed, deleted key is " + objectKey); } } isTruncated = output.isTruncated(); continuationToken = output.getNextContinuationToken(); } } catch (TosClientException e) { // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送 System.out.println("deleteObject failed"); System.out.println("Message: " + e.getMessage()); if (e.getCause() != null) { e.getCause().printStackTrace(); } } catch (TosServerException e) { // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息 System.out.println("deleteObject 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("deleteObject failed"); System.out.println("unexpected exception, message: " + t.getMessage()); } } }