package com.hyx.ssl;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.model.BatchStatus;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.storage.model.FileListing;
import com.qiniu.storage.model.StorageType;
import com.qiniu.util.Auth;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
public class Qiniu {
public static void main(String[] args) throws QiniuException {
//构造一个带指定 Region 对象的配置类
Configuration cfg = new Configuration(Region.region0());
//...其他参数参考类注释
String accessKey = "你的accessKey";
String secretKey = "你的secretKey";
String bucket = "对象存储空间名称";
Auth auth = Auth.create(accessKey, secretKey);
BucketManager bucketManager = new BucketManager(auth, cfg);
//文件名前缀
String prefix = "";
//每次迭代的长度限制,最大1000,推荐值 1000
int limit = 1000;
//指定目录分隔符,列出所有公共前缀(模拟列出目录效果)。缺省值为空字符串
String delimiter = "";
String marker = "";
while (true) {
System.out.println("当前marker:" + marker);
//文件列表
FileListing fileListing = bucketManager.listFiles(bucket, prefix, marker, limit, delimiter);
if (fileListing.isEOF() || fileListing.items == null || fileListing.items.length == 0) {
break;
}
//处理获取的file list结果
marker = fileListing.marker;
FileInfo[] items = fileListing.items;
List<String> keys = new ArrayList<>();
for (FileInfo item : items) {
//一年之内上传的文件不处理
if ((item.putTime / 10000) > ZonedDateTime.now().minusYears(1).toInstant().toEpochMilli()) {
continue;
}
//只处理普通存储文件
if (item.type != 0) {
continue;
}
keys.add(item.key);
}
if (!keys.isEmpty()) {
//批量改为归档直读存储
BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations();
batchOperations.addChangeTypeOps(bucket, StorageType.ArchiveIR, keys.toArray(new String[0]));
Response response = bucketManager.batch(batchOperations);
BatchStatus[] batchStatusList = response.jsonToObject(BatchStatus[].class);
for (int i = 0; i < keys.size(); i++) {
BatchStatus status = batchStatusList[i];
if (status.code == 200) {
System.out.println(keys.get(i) + ":成功");
} else {
System.out.println(keys.get(i) + ":失败," + status.data.error);
}
}
}
}
}
}