|
| 1 | +package com.qcloud.cos.demo; |
| 2 | + |
| 3 | +import com.qcloud.cos.COSClient; |
| 4 | +import com.qcloud.cos.ClientConfig; |
| 5 | +import com.qcloud.cos.auth.BasicCOSCredentials; |
| 6 | +import com.qcloud.cos.auth.COSCredentials; |
| 7 | +import com.qcloud.cos.exception.CosClientException; |
| 8 | +import com.qcloud.cos.exception.CosServiceException; |
| 9 | +import com.qcloud.cos.http.HttpProtocol; |
| 10 | + |
| 11 | +import com.qcloud.cos.model.COSVersionSummary; |
| 12 | +import com.qcloud.cos.model.CopyObjectRequest; |
| 13 | +import com.qcloud.cos.model.CopyObjectResult; |
| 14 | +import com.qcloud.cos.model.ListVersionsRequest; |
| 15 | +import com.qcloud.cos.model.VersionListing; |
| 16 | +import com.qcloud.cos.region.Region; |
| 17 | + |
| 18 | +import java.util.List; |
| 19 | +import java.util.Objects; |
| 20 | + |
| 21 | +public class RecoverObjectsDemo { |
| 22 | + private static String secretId = "AKIDXXXXXXXX"; |
| 23 | + private static String secretKey = "1A2Z3YYYYYYYYYY"; |
| 24 | + private static String bucketName = "examplebucket-12500000000"; |
| 25 | + |
| 26 | + private static String bucketRegion = "ap-guangzhou"; |
| 27 | + |
| 28 | + private static COSClient cosClient = createCli(); |
| 29 | + |
| 30 | + public static void main(String[] args) { |
| 31 | + listAndRecoverObjs(); |
| 32 | + } |
| 33 | + |
| 34 | + private static COSClient createCli() { |
| 35 | + // 1 初始化用户身份信息(secretId, secretKey) |
| 36 | + COSCredentials cred = new BasicCOSCredentials(secretId, secretKey); |
| 37 | + |
| 38 | + // 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224 |
| 39 | + ClientConfig clientConfig = new ClientConfig(new Region(bucketRegion)); |
| 40 | + |
| 41 | + clientConfig.setHttpProtocol(HttpProtocol.https); |
| 42 | + // 生成cos客户端 |
| 43 | + return new COSClient(cred, clientConfig); |
| 44 | + } |
| 45 | + |
| 46 | + private static void listAndRecoverObjs() { |
| 47 | + ListVersionsRequest listVersionsRequest = new ListVersionsRequest(); |
| 48 | + listVersionsRequest.setBucketName(bucketName); |
| 49 | + listVersionsRequest.setPrefix(""); |
| 50 | + listVersionsRequest.setMaxResults(1000); |
| 51 | + |
| 52 | + VersionListing versionListing = null; |
| 53 | + |
| 54 | + String recover_key = ""; |
| 55 | + String recover_versionid = ""; |
| 56 | + boolean has_recovered = false; |
| 57 | + |
| 58 | + do { |
| 59 | + try { |
| 60 | + versionListing = cosClient.listVersions(listVersionsRequest); |
| 61 | + } catch (CosServiceException e) { |
| 62 | + e.printStackTrace(); |
| 63 | + return; |
| 64 | + } catch (CosClientException e) { |
| 65 | + e.printStackTrace(); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + List<COSVersionSummary> cosVersionSummaries = versionListing.getVersionSummaries(); |
| 70 | + for (COSVersionSummary cosVersionSummary : cosVersionSummaries) { |
| 71 | + String key = cosVersionSummary.getKey(); |
| 72 | + String versionId = cosVersionSummary.getVersionId(); |
| 73 | + boolean isDeleteMarker = cosVersionSummary.isDeleteMarker(); |
| 74 | + boolean isLatest = cosVersionSummary.isLatest(); |
| 75 | + String msg = String.format("list obj, Key[%s], Version[%s], isDeleteMarker[%s], isLatest[%s]", key, versionId, isDeleteMarker, isLatest); |
| 76 | + System.out.println(msg); |
| 77 | + if (isDeleteMarker && isLatest) { |
| 78 | + // 只恢复最新 versionid 为删除标记的对象 |
| 79 | + recover_key = key; |
| 80 | + has_recovered = false; |
| 81 | + } else if (!isDeleteMarker && !isLatest && Objects.equals(key, recover_key) && !has_recovered) { |
| 82 | + // 既不是最新版本,也不是删除标记,如果key等于recover_key,那么说明找到了要恢复的数据版本,执行恢复逻辑 |
| 83 | + recover_versionid = versionId; |
| 84 | + recoverObj(recover_key, recover_versionid); |
| 85 | + has_recovered = true; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + String keyMarker = versionListing.getNextKeyMarker(); |
| 90 | + String versionIdMarker = versionListing.getNextVersionIdMarker(); |
| 91 | + |
| 92 | + listVersionsRequest.setKeyMarker(keyMarker); |
| 93 | + listVersionsRequest.setVersionIdMarker(versionIdMarker); |
| 94 | + |
| 95 | + } while (versionListing.isTruncated()); |
| 96 | + } |
| 97 | + |
| 98 | + private static void recoverObj(String srcKey, String srcVersionId) { |
| 99 | + String dstKey = srcKey; |
| 100 | + CopyObjectRequest copyObjectRequest = new CopyObjectRequest(new Region(bucketRegion), bucketName, srcKey, bucketName, dstKey); |
| 101 | + copyObjectRequest.setSourceVersionId(srcVersionId); |
| 102 | + try { |
| 103 | + CopyObjectResult result = cosClient.copyObject(copyObjectRequest); |
| 104 | + String msg = String.format("finish recover by copying obj, srcKey[%s], srcVersion[%s], dstKey[%s], dstVersion[%s]", srcKey, srcVersionId, dstKey, result.getVersionId()); |
| 105 | + System.out.println(msg); |
| 106 | + } catch (CosServiceException cse) { |
| 107 | + cse.printStackTrace(); |
| 108 | + } catch (CosClientException cce) { |
| 109 | + cce.printStackTrace(); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments