//构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(Zone.zone0());
//...其他参数参考类注释
String accessKey = "your access key";
String secretKey = "your secret key";
String bucket = "your bucket name";
Auth auth = Auth.create(accessKey, secretKey);
BucketManager bucketManager = new BucketManager(auth, cfg);
try {
//单次批量请求的文件数量不得超过1000
String[] keyList = new String[]{
"qiniu.jpg",
"qiniu.mp4",
"qiniu.png",
};
BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations();
for (String key : keyList) {
batchOperations.addCopyOp(bucket, key, bucket, key + "_copy");
}
Response response = bucketManager.batch(batchOperations);
BatchStatus[] batchStatusList = response.jsonToObject(BatchStatus[].class);
for (int i = 0; i < keyList.length; i++) {
BatchStatus status = batchStatusList[i];
String key = keyList[i];
System.out.print(key + "\t");
if (status.code == 200) {
System.out.println("copy success");
} else {
System.out.println(status.data.error);
}
}
} catch (QiniuException ex) {
System.err.println(ex.response.toString());
}
当然batch接口支持混合命令的处理,但是为了方便解析请求的回复,一般不推荐这样做。
//构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(Zone.zone0());
//...其他参数参考类注释
String accessKey = "your access key";
String secretKey = "your secret key";
String bucket = "your bucket name";
Auth auth = Auth.create(accessKey, secretKey);
BucketManager bucketManager = new BucketManager(auth, cfg);
try {
//单次批量请求的文件数量不得超过1000
BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations();
//添加混合指令
batchOperations.addStatOps(bucket, "qiniu.png", "qiniu.jpg");
batchOperations.addCopyOp(bucket, "qiniu.png", bucket, "qiniu_copy1.png");
batchOperations.addMoveOp(bucket, "qiniu2.png", bucket, "qiniu3.png");
batchOperations.addDeleteOp(bucket, "qiniu4.png");
Response response = bucketManager.batch(batchOperations);
BatchStatus[] batchStatusList = response.jsonToObject(BatchStatus[].class);
for (BatchStatus status : batchStatusList) {
if (status.code == 200) {
System.out.println("operation success");
} else {
System.out.println(status.data.error);
}
}
} catch (QiniuException ex) {
System.err.println(ex.response.toString());
}
对于配置了镜像存储的空间,所以镜像源站更新了文档内容,则默认状况下,不会再主动从顾客镜像源站同步新的副本,这个时候就需要借助这个prefetch接口来主动地将空间中的文档和更新后的源站副本进行同步。调用方法很简单,指定空间和文件名即可。
//构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(Zone.zone0());
//...其他参数参考类注释
String accessKey = "your access key";
String secretKey = "your secret key";
String bucket = "your bucket name";
String key = "your file key";
Auth auth = Auth.create(accessKey, secretKey);
BucketManager bucketManager = new BucketManager(auth, cfg);
try {
bucketManager.prefetch(bucket, key);
} catch (QiniuException ex) {
//如果遇到异常,说明更新失败
System.err.println(ex.code());
System.err.println(ex.response.toString());
}
对于早已保留到空间的文档,可以通过发送持久化的数据处理指令来进行处理,这种命令支持官方提供的命令,也包含顾客自己开发的自定义数据处理的命令。数据处理的结果还可以通过主动通知的方法询问业务服务器。
String accessKey = "access key";
String secretKey = "secret key";
//待处理文件所在空间
String bucket = "bucket name";
//待处理文件名
String key = "file key";
Auth auth = Auth.create(accessKey, secretKey);
//数据处理指令,支持多个指令
String saveMp4Entry = String.format("%s:avthumb_test_target.mp4", bucket);
String saveJpgEntry = String.format("%s:vframe_test_target.jpg", bucket);
String avthumbMp4Fop = String.format("avthumb/mp4|saveas/%s", UrlSafeBase64.encodeToString(saveMp4Entry));
String vframeJpgFop = String.format("vframe/jpg/offset/1|saveas/%s", UrlSafeBase64.encodeToString(saveJpgEntry));
//将多个数据处理指令拼接起来
String persistentOpfs = StringUtils.join(new String[]{
avthumbMp4Fop, vframeJpgFop
}, ";");
//数据处理队列名称,必须
String persistentPipeline = "mps-pipe1";
//数据处理完成结果通知地址
String persistentNotifyUrl = "http://api.example.com/qiniu/pfop/notify";
//构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(Zone.zone0());
//...其他参数参考类注释
//构建持久化数据处理对象
OperationManager operationManager = new OperationManager(auth, cfg);
try {
String persistentId = operationManager.pfop(bucket, key, persistentOpfs, persistentPipeline, persistentNotifyUrl, true);
//可以根据该 persistentId 查询任务处理进度
System.out.println(persistentId);
OperationStatus operationStatus = operationManager.prefop(persistentId);
//解析 operationStatus 的结果
} catch (QiniuException e) {
System.err.println(e.response.toString());
}
由于数据处理是异步处理,可以按照发送处理请求时前往的 persistentId 去查询任务的处理进度,所以在增设了persistentNotifyUrl 的现象下,直接业务服务器等待处理结果通告即可,所以需要主动查询,可以采用如上代码中的:
OperationStatus operationStatus = operationManager.prefop(persistentId);
//解析 operationStatus 的结果
String accessKey = "your access key";
String secretKey = "your secret key";
Auth auth = Auth.create(accessKey, secretKey);
CdnManager c = new CdnManager(auth);
//待刷新的链接列表
String[] urls = new String[]{
"http://javasdk.qiniudn.com/gopher1.jpg",
"http://javasdk.qiniudn.com/gopher2.jpg",
//....
};
try {
//单次方法调用刷新的链接不可以超过100个
CdnResult.RefreshResult result = c.refreshUrls(urls);
System.out.println(result.code);
//获取其他的回复内容
} catch (QiniuException e) {
System.err.println(e.response.toString());
}
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-118827-5.html