最近在用SpringMVC+Dubbo(阿里的,还用的fastjson,人麻了)+zookeeper写项目,但这配置文件把我人整麻了,所以在这里保留一些配置文件相关的内容,好日后查询(其实就是好复制粘贴)
七牛云官网文档
七牛云的配置类
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.BatchStatus;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.util.Auth;
import java.util.ArrayList;
import java.util.List;
public class QiNiuUtils {
private static final String ACCESS_KEY = "此处填写API的AK";
private static final String SECRET_KEY = "此处填写API的SK";
private static final String BUCKET = "此处填写API的空间名";
public static final String DOMAIN = "此处填写API的域名";
private static final Configuration cfg = new Configuration(Region.region1());
private static final Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
private static final BucketManager bucketManager = new BucketManager(auth, cfg);
// 文件名前缀
private static final String PREFIX = "";
// 每次迭代的长度限制,最大1000,推荐值 1000
private static final int LIMIT = 1000;
// 指定目录分隔符,列出所有公共前缀(模拟列出目录效果)。缺省值为空字符串
private static final String DELIMITER = "";
private static final String UP_TOKEN = auth.uploadToken(BUCKET);
private static final UploadManager uploadManager = new UploadManager(cfg);
public static List<String> listFile() {
// 列举空间文件列表, 第一个参数:图片的仓库(空间名),第二个参数,文件名前缀过滤。“”代理所有
BucketManager.FileListIterator fileListIterator =
bucketManager.createFileListIterator(BUCKET, PREFIX, LIMIT, DELIMITER);
List<String> imageFiles = new ArrayList<>();
while (fileListIterator.hasNext()) {
// 处理获取的file list结果
FileInfo[] items = fileListIterator.next();
for (FileInfo item : items) {
// item.key 文件名
imageFiles.add(item.key);
System.out.println(item.key);
}
}
return imageFiles;
}
public static List<String> removeFiles(String... filenames) {
// 删除成功的文件名列表
List<String> removeSuccessList = new ArrayList<>();
if (filenames.length > 0) {
// 创建仓库管理器
// 创建批处理器
BucketManager.BatchOperations batch = new BucketManager.BatchOperations();
// 批量删除多个文件
batch.addDeleteOp(BUCKET, filenames);
try {
// 获取服务器的响应
Response res = bucketManager.batch(batch);
// 获得批处理的状态
BatchStatus[] batchStatuses = res.jsonToObject(BatchStatus[].class);
for (int i = 0; i < filenames.length; i++) {
BatchStatus status = batchStatuses[i];
String key = filenames[i];
System.out.print(key + "\t");
if (status.code == 200) {
removeSuccessList.add(key);
System.out.println("delete success");
} else {
System.out.println("delete failure");
}
}
} catch (QiniuException e) {
e.printStackTrace();
throw new RuntimeException(MessageConstant.PIC_UPLOAD_FAIL);
}
}
return removeSuccessList;
}
public static void uploadFile(String localFilePath, String savedFilename) {
try {
Response response = uploadManager.put(localFilePath, savedFilename, UP_TOKEN);
// 解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
System.out.printf("key=%s, hash=%s%n", putRet.key, putRet.hash);
} catch (QiniuException ex) {
Response r = ex.response;
System.err.println(r.toString());
try {
System.err.println(r.bodyString());
} catch (QiniuException ex2) {
// ignore
}
throw new RuntimeException(MessageConstant.PIC_UPLOAD_FAIL);
}
}
public static void uploadViaByte(byte[] bytes, String savedFilename) {
try {
Response response = uploadManager.put(bytes, savedFilename, UP_TOKEN);
// 解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
System.out.println(putRet.key);
System.out.println(putRet.hash);
} catch (QiniuException ex) {
Response r = ex.response;
System.err.println(r.toString());
try {
System.err.println(r.bodyString());
} catch (QiniuException ex2) {
// ignore
}
throw new RuntimeException(MessageConstant.PIC_UPLOAD_FAIL);
}
}
}
Q.E.D.