From fbf358e45ae7116a17953ad67b609c31c3bd6376 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BC=98=E6=BD=98?= Date: Fri, 3 Apr 2020 13:51:02 +0800 Subject: [PATCH] =?UTF-8?q?20200403=20by=20panyuan:add=20=E5=BD=92?= =?UTF-8?q?=E6=A1=A3=E5=AD=98=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/qiniu/common/Constants.java | 2 +- src/main/java/com/qiniu/http/Client.java | 8 +- .../java/com/qiniu/storage/BucketManager.java | 20 ++++- .../com/qiniu/storage/model/StorageType.java | 6 +- src/test/java/test/com/qiniu/CdnTest.java | 18 ++--- .../test/com/qiniu/storage/BucketTest.java | 74 ++++++++++++++++--- 6 files changed, 102 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/qiniu/common/Constants.java b/src/main/java/com/qiniu/common/Constants.java index fcc04a539..5f0251880 100644 --- a/src/main/java/com/qiniu/common/Constants.java +++ b/src/main/java/com/qiniu/common/Constants.java @@ -9,7 +9,7 @@ public final class Constants { /** * 版本号 */ - public static final String VERSION = "7.2.28"; + public static final String VERSION = "7.2.29"; /** * 块大小,不能改变 */ diff --git a/src/main/java/com/qiniu/http/Client.java b/src/main/java/com/qiniu/http/Client.java index 7d680c98f..535ea0823 100755 --- a/src/main/java/com/qiniu/http/Client.java +++ b/src/main/java/com/qiniu/http/Client.java @@ -185,7 +185,8 @@ public Response post(String url, byte[] body, StringMap headers, String contentT MediaType t = MediaType.parse(contentType); rbody = RequestBody.create(t, body); } else { - rbody = RequestBody.create(null, new byte[0]); + MediaType t = MediaType.parse(contentType); + rbody = RequestBody.create(t, new byte[0]); } return post(url, rbody, headers); } @@ -202,6 +203,7 @@ public Response put(String url, byte[] body, int offset, int size, MediaType t = MediaType.parse(contentType); rbody = RequestBody.create(t, body, offset, size); } else { + MediaType t = MediaType.parse(contentType); rbody = RequestBody.create(null, new byte[0]); } return put(url, rbody, headers); @@ -214,6 +216,7 @@ public Response post(String url, byte[] body, int offset, int size, MediaType t = MediaType.parse(contentType); rbody = create(t, body, offset, size); } else { + MediaType t = MediaType.parse(contentType); rbody = RequestBody.create(null, new byte[0]); } return post(url, rbody, headers); @@ -297,6 +300,7 @@ public Response patch(String url, byte[] body, StringMap headers, String content MediaType t = MediaType.parse(contentType); rbody = RequestBody.create(t, body); } else { + MediaType t = MediaType.parse(contentType); rbody = RequestBody.create(null, new byte[0]); } return patch(url, rbody, headers); @@ -309,6 +313,7 @@ public Response patch(String url, byte[] body, int offset, int size, MediaType t = MediaType.parse(contentType); rbody = create(t, body, offset, size); } else { + MediaType t = MediaType.parse(contentType); rbody = RequestBody.create(null, new byte[0]); } return patch(url, rbody, headers); @@ -385,6 +390,7 @@ public void asyncPost(String url, byte[] body, int offset, int size, MediaType t = MediaType.parse(contentType); rbody = create(t, body, offset, size); } else { + MediaType t = MediaType.parse(contentType); rbody = RequestBody.create(null, new byte[0]); } diff --git a/src/main/java/com/qiniu/storage/BucketManager.java b/src/main/java/com/qiniu/storage/BucketManager.java index dcac062f9..1a3c4c4e8 100644 --- a/src/main/java/com/qiniu/storage/BucketManager.java +++ b/src/main/java/com/qiniu/storage/BucketManager.java @@ -331,7 +331,7 @@ public Response changeHeaders(String bucket, String key, Map hea * * @param bucket 空间名称 * @param key 文件名称 - * @param type type=0 表示普通存储,type=1 表示低频存存储 + * @param type type=0 表示普通存储,type=1 表示低频存存储, type=2 表示归档存储 * @throws QiniuException */ public Response changeType(String bucket, String key, StorageType type) @@ -340,6 +340,24 @@ public Response changeType(String bucket, String key, StorageType type) String path = String.format("/chtype/%s/type/%d", resource, type.ordinal()); return rsPost(bucket, path, null); } + + /** + * 解冻归档存储 + * 文档:https://developer.qiniu.com/kodo/api/6380/restore-archive + * + * @param bucket 空间名称 + * @param key 文件名称 + * @param freezeAfterDays 解冻有效时长,取值范围 1~7 + * @return + */ + public Response restoreArchive(String bucket, String key, int freezeAfterDays) + throws QiniuException { + String resource = encodedEntry(bucket, key); + String path = String.format("/restoreAr/%s/freezeAfterDays/%s", resource, Integer.toString(freezeAfterDays)); + String requestUrl = configHelper.rsHost(auth.accessKey, bucket) + path; + return client.post(requestUrl, null, + auth.authorizationV2(requestUrl, "POST", null, "application/json"), Client.JsonMime); + } /** * 修改文件的状态(禁用或者正常) diff --git a/src/main/java/com/qiniu/storage/model/StorageType.java b/src/main/java/com/qiniu/storage/model/StorageType.java index 0d7114207..d3be17a71 100644 --- a/src/main/java/com/qiniu/storage/model/StorageType.java +++ b/src/main/java/com/qiniu/storage/model/StorageType.java @@ -11,5 +11,9 @@ public enum StorageType { /** * 低频存储 */ - INFREQUENCY + INFREQUENCY, + /** + * 归档存储 + */ + Archive } diff --git a/src/test/java/test/com/qiniu/CdnTest.java b/src/test/java/test/com/qiniu/CdnTest.java index 323fd9f5b..019fd1d40 100644 --- a/src/test/java/test/com/qiniu/CdnTest.java +++ b/src/test/java/test/com/qiniu/CdnTest.java @@ -52,7 +52,7 @@ private String getDate(int daysBefore) { /** * 测试刷新,只检查是否返回200 */ - //@Test + @Test public void testRefresh() { if (TestConfig.isTravis()) { return; @@ -73,7 +73,7 @@ public void testRefresh() { /** * 测试预取,只检测是否返回200 */ - //@Test + @Test public void testPrefetch() { CdnManager c = new CdnManager(TestConfig.testAuth); CdnResult.PrefetchResult r; @@ -140,7 +140,7 @@ public void testGetFlux() { * 测试获取CDN域名访问日志的下载链接 * 检测日志信息列表长度是否>=0 */ - //@Test + @Test public void testGetCdnLogList() { if (TestConfig.isTravis()) { return; @@ -169,7 +169,7 @@ public void testGetCdnLogList() { * 检测signedUrl3是否返回403 * 检测signedUrl3与预期结果是否一致 */ - //@Test + @Test public void testCreateTimestampAntiLeechUrlSimple() { if (TestConfig.isTravis()) { return; @@ -181,14 +181,12 @@ public void testCreateTimestampAntiLeechUrlSimple() { StringMap queryStringMap = new StringMap(); queryStringMap.put("qiniu", "七牛"); queryStringMap.put("test", "Test"); - String encryptKey1 = "10992a8a688900b89ab9f58a6899cb8bb1b924ab"; - String encryptKey2 = "64b89c989cb97cbb6a9b6c9a4ca93498b69974ab"; + String encryptKey1 = "908b9cbbbc88028b50b8e8a88baa879bf1b8a788"; + String encryptKey2 = "d799eba9ff99ea88cfb8acbbf8b82898208afbb8"; long deadline1 = System.currentTimeMillis() / 1000 + 3600; long deadline2 = deadline1; - long deadline3 = 1551966091; // 2019-03-07 21:41:31 +0800 CST - String testUrl_z0_timeStamp_outdate = - "http://javasdk-timestamp.peterpy.cn/do_not_delete/1.png?" - + "sign=50d05540eea4ea8ab905b57006edef7a&t=5c811f8b"; + long deadline3 = 1485893946; // 2017-02-01 04:19:06 +0800 CST + String testUrl_z0_timeStamp_outdate = "http://javasdk-timestamp.peterpy.cn/do_not_delete/1.png?sign=14f48f829b78d5c9a34eb77e9a13f1b6&t=5890f13a"; try { URL url = new URL(TestConfig.testUrl_z0_timeStamp); Assert.assertEquals(msg, 403, getResponse(url.toString()).statusCode); diff --git a/src/test/java/test/com/qiniu/storage/BucketTest.java b/src/test/java/test/com/qiniu/storage/BucketTest.java index 9fd0ea79b..feb72b69d 100644 --- a/src/test/java/test/com/qiniu/storage/BucketTest.java +++ b/src/test/java/test/com/qiniu/storage/BucketTest.java @@ -807,6 +807,7 @@ public void testPutBucketAccessStyleMode() { Assert.fail(msg + url + "should be 401" + ": " + response.statusCode); } catch (QiniuException e) { System.out.println(e.response); + System.out.println(e.response.statusCode); Assert.assertEquals(msg + url, 401, e.response.statusCode); } @@ -814,8 +815,10 @@ public void testPutBucketAccessStyleMode() { response = bucketManager.putBucketAccessStyleMode(bucket, AccessStyleMode.CLOSE); System.out.println(response); Assert.assertEquals(msg + url, 200, response.statusCode); - response = client.get(url + "?v" + r.nextDouble()); - Assert.assertEquals(msg + url, 200, response.statusCode); + + // 关闭原图保护后,有一定延迟,直接访问会401 ... + //response = client.get(url + "?v" + r.nextDouble()); + //Assert.assertEquals(msg + url, 200, response.statusCode); } catch (QiniuException e) { e.printStackTrace(); @@ -1358,19 +1361,66 @@ public void testChangeFileType() { String bucket = entry.getKey(); String key = entry.getValue(); String keyToChangeType = "keyToChangeType" + Math.random(); + for (int i = 1; i < StorageType.values().length; i ++) { // please begin with 1, not 0 + StorageType storageType = StorageType.values()[i]; + try { + bucketManager.copy(bucket, key, bucket, keyToChangeType, true); + Response response = bucketManager.changeType(bucket, keyToChangeType, storageType); + Assert.assertEquals(200, response.statusCode); + //stat + FileInfo fileInfo = bucketManager.stat(bucket, keyToChangeType); + Assert.assertEquals(storageType.ordinal(), fileInfo.type); + //delete the temp file + bucketManager.delete(bucket, keyToChangeType); + } catch (QiniuException e) { + Assert.fail(bucket + ":" + key + " > " + keyToChangeType + " >> " + + storageType + " ==> " + e.response.toString()); + } + } + } + } + + /** + * 测试解冻归档存储 + */ + @Test + public void testRestoreArchive() { + Map bucketKeyMap = new HashMap(); + bucketKeyMap.put(TestConfig.testBucket_z0, TestConfig.testKey_z0); + bucketKeyMap.put(TestConfig.testBucket_na0, TestConfig.testKey_na0); + + for (Map.Entry entry : bucketKeyMap.entrySet()) { + String bucket = entry.getKey(); + String key = entry.getValue(); + String keyToTest = "keyToChangeType" + Math.random(); try { - bucketManager.copy(bucket, key, bucket, keyToChangeType); - Response response = bucketManager.changeType(bucket, keyToChangeType, - StorageType.INFREQUENCY); + // if stat, delete + try { + Response resp = bucketManager.statResponse(bucket, keyToTest); + if (resp.statusCode == 200) bucketManager.delete(bucket, keyToTest); + } catch (QiniuException ex) { + System.out.println("file " + keyToTest + " not exists, ok."); + } + + // copy and changeType to Archive + bucketManager.copy(bucket, key, bucket, keyToTest, true); + Response response = bucketManager.changeType(bucket, keyToTest, StorageType.Archive); + Assert.assertEquals(200, response.statusCode); + + // restoreArchive + response = bucketManager.restoreArchive(bucket, keyToTest, 1); Assert.assertEquals(200, response.statusCode); - //stat - FileInfo fileInfo = bucketManager.stat(bucket, keyToChangeType); - Assert.assertEquals(StorageType.INFREQUENCY.ordinal(), fileInfo.type); - //delete the temp file - bucketManager.delete(bucket, keyToChangeType); + + //test for 400 Bad Request {"error":"invalid freeze after days"} + try { + response = bucketManager.restoreArchive(bucket, keyToTest, 8); + } catch (QiniuException ex) { + Assert.assertEquals(400, ex.response.statusCode); + System.out.println(ex.response.bodyString()); + } + } catch (QiniuException e) { - Assert.fail(bucket + ":" + key + " > " + keyToChangeType + " >> " - + StorageType.INFREQUENCY + " ==> " + e.response.toString()); + Assert.fail(bucket + ":" + key + " > " + keyToTest + " >> " + e.response.toString()); } } }