-
Notifications
You must be signed in to change notification settings - Fork 477
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #488 from qiniu/neiko
Neiko - example for issue
- Loading branch information
Showing
4 changed files
with
199 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import com.qiniu.common.QiniuException; | ||
import com.qiniu.http.Client; | ||
import com.qiniu.http.Response; | ||
import com.qiniu.util.Auth; | ||
import com.qiniu.util.StringMap; | ||
import com.qiniu.util.UrlSafeBase64; | ||
|
||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
|
||
/** | ||
* 自定义文件元信息demo(x-qn-meta-*) | ||
* | ||
* 接口 | ||
* POST /setmeta/<EncodedEntryURI>[/<x-qn-meta-MetaKey>/<EncodedMetaValue>][/cond/Encoded(condKey1=condVal1&condKey2=condVal2)] | ||
* Host: rs-<region>.qiniu.com | ||
* Content-Type: application/x-www-form-urlencoded | ||
* Authorization: Qbox 鉴权 | ||
* | ||
* 注意: | ||
* meta-key,key不能设置为中文,不允许为空; | ||
* 新的metas会完全替换掉以前的metas,注意, 是完全覆盖; | ||
* 如果请求url中没有 [<x-qn-meta-MetaKey>/<EncodedMetaValue>],则表示要删除所有metas; | ||
* | ||
*/ | ||
public class QNMetaDemo { | ||
//设置好账号的ACCESS_KEY和SECRET_KEY | ||
private static final String ACCESS_KEY = "填写你们自己的AK"; | ||
private static final String SECRET_KEY = "填写你们自己的SK"; | ||
private static final Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY); | ||
|
||
private Client client = new Client(); | ||
private String bucketName; | ||
private String rsHost; | ||
|
||
{ | ||
bucketName = "填写你们自己的存储空间"; | ||
//设置存储区域rs域名,华东z0 华北z1 华南z2 | ||
rsHost = "rs-z2.qiniu.com"; | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
QNMetaDemo qnMetaDemo = new QNMetaDemo(); | ||
//需要设置自定义meta的空间文件名称 | ||
String key = "1.mp4"; | ||
//设置自定义的meta头部,注意,每次调用该接口,是直接覆盖meta,不是追加meta | ||
HashMap<String, String> metaKeyVal = new HashMap<>(); | ||
metaKeyVal.put("eng1", "qiniu"); | ||
metaKeyVal.put("eng2", "七牛"); | ||
boolean result = qnMetaDemo.setMeta(qnMetaDemo.bucketName, key, metaKeyVal); | ||
if(result){ | ||
System.out.println("done"); | ||
} | ||
} | ||
|
||
/** | ||
* @param bucket 存储空间名称 | ||
* @param key 存储空间的文件名称 | ||
* @param headers 自定义的请求头 key - val map | ||
* @return true or false | ||
* @throws QiniuException | ||
*/ | ||
public boolean setMeta(String bucket, String key, Map<String, String> headers) throws QiniuException { | ||
String resource = UrlSafeBase64.encodeToString(bucket.concat(":").concat(key)); | ||
String path = String.format("/setmeta/%s", resource); | ||
String k; | ||
String encodedMetaValue; | ||
for(Iterator var6 = headers.keySet().iterator(); var6.hasNext(); path = String.format("%s/x-qn-meta-%s/%s", path, k, encodedMetaValue)) { | ||
k = (String)var6.next(); | ||
encodedMetaValue = UrlSafeBase64.encodeToString((String)headers.get(k)); | ||
} | ||
//接口请求地址 | ||
String url = String.format("https://%s%s", rsHost, path); | ||
System.out.println(url); | ||
Response res = this.post(url); | ||
if (res.statusCode != 200) { | ||
return false; | ||
} else { | ||
return true; | ||
} | ||
} | ||
|
||
private Response post(String url) throws QiniuException { | ||
StringMap headers = this.auth.authorization(url); | ||
return this.client.post(url, null, headers, "application/x-www-form-urlencoded"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import com.qiniu.common.QiniuException; | ||
import com.qiniu.http.Response; | ||
import com.qiniu.storage.Configuration; | ||
import com.qiniu.storage.Region; | ||
import com.qiniu.storage.UploadManager; | ||
import com.qiniu.util.Auth; | ||
import com.qiniu.util.StringMap; | ||
|
||
/** | ||
* 设置自定义变量上传并接收自定义变量 demo | ||
* | ||
* 自定义变量需要以 x: 开头, 携带自定义变量上传参考文档 | ||
* https://developer.qiniu.com/kodo/manual/1235/vars#2 | ||
* | ||
* 接收自定义变量参考上传策略文档 -- returnBody | ||
* https://developer.qiniu.com/kodo/manual/1206/put-policy | ||
* | ||
* 服务端具体用法实例参考 UploadBySelfDefiningParam.upload() | ||
*/ | ||
public class UploadBySelfDefiningParam { | ||
|
||
private static final String ACCESS_KEY = "设置好你们自己的AK"; | ||
private static final String SECRET_KEY = "设置好你们自己的SK"; | ||
private static final Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY); | ||
|
||
private Configuration cfg; | ||
private UploadManager uploadManager; | ||
private Region region; | ||
private String bucket; | ||
|
||
{ | ||
bucket = "设置你们自己的上传空间名称"; | ||
//指定存储空间所在区域,华北region1,华南region2 ,华东 region0 | ||
region = Region.region1(); | ||
//初始化cfg实例,可以指定上传区域,也可以创建无参实例 , cfg = new Configuration(); | ||
cfg = new Configuration(region); | ||
//是否指定https上传,默认true | ||
//cfg.useHttpsDomains=false; | ||
//构建 uploadManager 实例 | ||
uploadManager = new UploadManager(cfg); | ||
} | ||
|
||
public static void main(String args[]) throws Exception { | ||
UploadBySelfDefiningParam up = new UploadBySelfDefiningParam(); | ||
up.upload(); | ||
} | ||
|
||
public void upload() throws QiniuException { | ||
//设置上传后的文件名称 | ||
String key = "qiniu_test.jpg"; | ||
//上传策略 | ||
StringMap policy = new StringMap(); | ||
//自定义上传后返回内容,返回自定义参数,需要设置 x:参数名称 | ||
policy.put("returnBody", "{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"fname\":\"$(x:fname)\",\"age\",$(x:age)}"); | ||
//生成上传token | ||
String upToken = auth.uploadToken(bucket, key, 3600, policy); | ||
|
||
//上传自定义参数,自定义参数名称需要以 x:开头 | ||
StringMap params = new StringMap(); | ||
params.put("x:fname","123.jpg"); | ||
params.put("x:age",20); | ||
String localFilePath = "/Users/mini/Downloads/qiniu_test.jpg"; | ||
|
||
Response response = uploadManager.put(localFilePath, key, upToken,params,null,false); | ||
//输出返回结果 | ||
System.out.println(response.bodyString()); | ||
} | ||
|
||
} |