Skip to content

Commit

Permalink
Merge pull request qiniu#318 from MistyL/master
Browse files Browse the repository at this point in the history
add changeHeaders method to allow user to self define the response headers of files in bucket
  • Loading branch information
jemygraw authored Jan 9, 2018
2 parents ac95e13 + 45fe6fb commit 0bfd905
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
21 changes: 21 additions & 0 deletions src/main/java/com/qiniu/storage/BucketManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

/**
* 主要涉及了空间资源管理及批量操作接口的实现,具体的接口规格可以参考
Expand Down Expand Up @@ -194,6 +195,26 @@ public Response changeMime(String bucket, String key, String mime)
return rsPost(bucket, path, null);
}

/**
* 修改文件的元数据
*
* @param bucket 空间名称
* @param key 文件名称
* @param headers 需要修改的文件元数据
* @throws QiniuException
* @link https://developer.qiniu.com/kodo/api/1252/chgm
*/
public Response changeHeaders(String bucket, String key, Map<String, String> headers)
throws QiniuException {
String resource = encodedEntry(bucket, key);
String path = String.format("/chgm/%s", resource);
for (String k : headers.keySet()) {
String encodedMetaValue = UrlSafeBase64.encodeToString(headers.get(k));
path = String.format("%s/%s/%s", path, k, encodedMetaValue);
}
return rsPost(bucket, path, null);
}


//存储类型
public enum StorageType {
Expand Down
30 changes: 26 additions & 4 deletions src/test/java/test/com/qiniu/storage/BucketTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.text.SimpleDateFormat;
import java.util.*;

@SuppressWarnings("ConstantConditions")
public class BucketTest extends TestCase {
Expand Down Expand Up @@ -267,6 +265,30 @@ public void testChangeMime() {
}
}

@Test
public void testChangeHeaders() {
List<String[]> cases = new ArrayList<String[]>();
cases.add(new String[]{TestConfig.testBucket_z0, TestConfig.testKey_z0});
cases.add(new String[]{TestConfig.testBucket_na0, TestConfig.testKey_na0});

for (String[] icase : cases) {
String bucket = icase[0];
String key = icase[1];
try {
Map<String, String> headers = new HashMap<>();
Date d = new Date();
SimpleDateFormat dateFm = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.ENGLISH);
System.out.println(dateFm.format(d));
headers.put("x-qn-meta-!Content-Type", "image/png");
headers.put("x-qn-meta-!Last-Modifie", dateFm.format(d));
bucketManager.changeHeaders(bucket, key, headers);
} catch (QiniuException e) {
fail(bucket + ":" + key + "==> " + e.response.toString());
}
}
}


@Test
public void testPrefetch() {
String[] buckets = new String[]{TestConfig.testBucket_z0, TestConfig.testBucket_na0};
Expand Down

0 comments on commit 0bfd905

Please sign in to comment.