Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

20200403 by panyuan:add 归档存储 #473

Merged
merged 1 commit into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/com/qiniu/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public final class Constants {
/**
* 版本号
*/
public static final String VERSION = "7.2.28";
public static final String VERSION = "7.2.29";
/**
* 块大小,不能改变
*/
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/qiniu/http/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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]);
}

Expand Down
20 changes: 19 additions & 1 deletion src/main/java/com/qiniu/storage/BucketManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public Response changeHeaders(String bucket, String key, Map<String, String> 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)
Expand All @@ -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);
}

/**
* 修改文件的状态(禁用或者正常)
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/qiniu/storage/model/StorageType.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@ public enum StorageType {
/**
* 低频存储
*/
INFREQUENCY
INFREQUENCY,
/**
* 归档存储
*/
Archive
}
18 changes: 8 additions & 10 deletions src/test/java/test/com/qiniu/CdnTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private String getDate(int daysBefore) {
/**
* 测试刷新,只检查是否返回200
*/
//@Test
@Test
public void testRefresh() {
if (TestConfig.isTravis()) {
return;
Expand All @@ -73,7 +73,7 @@ public void testRefresh() {
/**
* 测试预取,只检测是否返回200
*/
//@Test
@Test
public void testPrefetch() {
CdnManager c = new CdnManager(TestConfig.testAuth);
CdnResult.PrefetchResult r;
Expand Down Expand Up @@ -140,7 +140,7 @@ public void testGetFlux() {
* 测试获取CDN域名访问日志的下载链接
* 检测日志信息列表长度是否>=0
*/
//@Test
@Test
public void testGetCdnLogList() {
if (TestConfig.isTravis()) {
return;
Expand Down Expand Up @@ -169,7 +169,7 @@ public void testGetCdnLogList() {
* 检测signedUrl3是否返回403
* 检测signedUrl3与预期结果是否一致
*/
//@Test
@Test
public void testCreateTimestampAntiLeechUrlSimple() {
if (TestConfig.isTravis()) {
return;
Expand All @@ -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);
Expand Down
74 changes: 62 additions & 12 deletions src/test/java/test/com/qiniu/storage/BucketTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -807,15 +807,18 @@ 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);
}

// 测试关闭原图保护
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();
Expand Down Expand Up @@ -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<String, String> bucketKeyMap = new HashMap<String, String>();
bucketKeyMap.put(TestConfig.testBucket_z0, TestConfig.testKey_z0);
bucketKeyMap.put(TestConfig.testBucket_na0, TestConfig.testKey_na0);

for (Map.Entry<String, String> 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());
}
}
}
Expand Down