Skip to content

Commit

Permalink
Merge pull request #488 from qiniu/neiko
Browse files Browse the repository at this point in the history
Neiko - example for issue
  • Loading branch information
peteryuanpan authored Aug 13, 2020
2 parents 2e690b0 + 5658eca commit c045cd4
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 5 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ JDK 7 及以上
```Java
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.qiniu.storage.Configuration;
import com.qiniu.http.Response;
...
UploadManager uploadManager = new UploadManager()
Configuration cfg = new Configuration();
UploadManager uploadManager = new UploadManager(cfg);
Auth auth = Auth.create(accessKey, secretKey);
String token = auth.uploadToken(bucketName);
Response r = upManager.put("hello world".getBytes(), "yourkey", token);
Expand Down
88 changes: 88 additions & 0 deletions examples/QNMetaDemo.java
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");
}
}
43 changes: 39 additions & 4 deletions examples/ResourcesCensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
import java.util.HashMap;
import java.util.Map;

/**
* 内容安全审核 demo
* ImageCensor 图片内容安全审核,同步处理,不需要查询处理结果
* VideoCensor 视频内容安全审核,异步处理,需要查询处理结果,返回处理任务ID
* getVideoCensorResultByJobID 根据视频内容审核任务ID,查询审核结果
*/
public class ResourcesCensor {
//设置好账号的ACCESS_KEY和SECRET_KEY
private static final String ACCESS_KEY = "填写你们自己的ak";
Expand All @@ -20,9 +26,16 @@ public static void main(String args[]) {
ResourcesCensor resourcesCensor = new ResourcesCensor();
String result;
try {
result = resourcesCensor.ImageCensor();
//result = resourcesCensor.VideoCensor();
System.out.printf(result);
//result = resourcesCensor.ImageCensor();
result = resourcesCensor.VideoCensor();
System.out.println(result);

/* 只有视频审核才会返回jobID */
Gson gson = new Gson();
Map<String, String> jobMap = new HashMap();
String jobID = (String) gson.fromJson(result, jobMap.getClass()).get("job");
String videoCensorResult = resourcesCensor.getVideoCensorResultByJobID(jobID);
System.out.println(videoCensorResult);
} catch (QiniuException e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -88,13 +101,35 @@ public String VideoCensor() throws QiniuException {
return post(url, bodyByte);
}

/**
* 查询视频审核内容结果
* 参考
* https://developer.qiniu.com/censor/api/5620/video-censor#4
* @param ID : 视频审核返回的 job ID
*
*/
public String getVideoCensorResultByJobID(String ID){
String url = "http://ai.qiniuapi.com/v3/jobs/video/".concat(ID);
String accessToken = (String) auth.authorizationV2(url).get("Authorization");
StringMap headers = new StringMap();
headers.put("Authorization", accessToken);

try {
com.qiniu.http.Response resp = client.get(url,headers);
return resp.bodyString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

private String post(String url, byte[] body) throws QiniuException {
String accessToken = (String) auth.authorizationV2(url, "POST", body, "application/json")
.get("Authorization");

StringMap headers = new StringMap();
headers.put("Authorization", accessToken);

com.qiniu.http.Response resp = client.post(url, body, headers, Client.JsonMime);
return resp.bodyString();
}
Expand Down
69 changes: 69 additions & 0 deletions examples/UploadBySelfDefiningParam.java
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());
}

}

0 comments on commit c045cd4

Please sign in to comment.