今天打开博客一看,图片全挂了- -!,打开七牛一看才发现是七牛的测试域名过期被删除了。。恶心到了O__O "…
想着将文件备份下来换别的服务,但是无法通过外链访问,点了点空间内的下载按钮,发现点不了。WTF..
gg看到 qiniu/qshell#188 提的方法,在官方也翻到了SDK,顺手写了下,已经解决了。
官网有Qshell工具可以弄,但是还得安装一下,所以贴下代码给大家偷下懒。简单测试可以下载文件到本地,没做太多处理,有问题的话不要打我。。。
溜了O__O "…
需要注意的是,新建的七牛云的bucket访问设置需要为公开访问。
- 新建maven工程,添加依赖:
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>[7.2.0, 7.2.99]</version>
</dependency>
- class Main
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.model.BatchStatus;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.util.Auth;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
/**
*
* first:将旧bucket文件复制到新bucket中
* second:用新的bucket提供的自定义域名拼接文件名进行下载。
*
* 配置AK、SK,以及两个Bukcet的名称,以及新建的bucket给的自定义域名即可。
*
*
* @author bennie
*/
public class Main {
private static final String ACCESS_KEY = "";
private static final String SECRET_KEY = "";
/**
* 旧bucket名称,拷贝的源bucket。
*/
private static final String SRC_BUCKET = "";
/**
* 新bucket的名称,拷贝的目标bucket。
*/
private static final String DEST_BUCKET = "";
/**
* 新bucket的自定义域名(需要以/结尾)
*/
private static final String DOMAIN = "";
private static BucketManager bucketManager;
static {
// 构造一个带指定Zone对象的配置类,请按照空间所属区域进行修改。
// Zone的区域名称:z0 华东 z1 华北 z2 华南 na0 北美 as0 东南亚
Configuration cfg = new Configuration(Zone.zone0());
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
bucketManager = new BucketManager(auth, cfg);
}
private static void downFile(String savePath) {
List<String> list = getFileList(DEST_BUCKET);
System.out.println("文件总数: " + list.size());
for (String fileName : list) {
File file = saveFile(DOMAIN, fileName, savePath);
if (file.exists()) {
System.out.println("文件: " + DOMAIN + fileName + ", 保存完毕!");
}
}
}
private static File saveFile(String domain, String fileName, String dir) {
File file = new File(dir, fileName);
try {
URL url = new URL(domain + fileName);
URLConnection connection = url.openConnection();
InputStream inputStream = connection.getInputStream();
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));
byte[] readBuf = new byte[4096];
for (int len; (len = inputStream.read(readBuf)) != -1; ) {
outputStream.write(readBuf, 0, len);
}
inputStream.close();
outputStream.flush();
outputStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
/**
* 以批量复制方式,将一个bucket的文件复制到另一个bucket!
*/
public static void cpBucketFiles() {
try {
List<String> keyList = getFileList(SRC_BUCKET);
BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations();
for (String key : keyList) {
batchOperations.addCopyOp(SRC_BUCKET, key, DEST_BUCKET, key);
}
Response response = bucketManager.batch(batchOperations);
BatchStatus[] batchStatusList = response.jsonToObject(BatchStatus[].class);
for (int i = 0; i < keyList.size(); i++) {
BatchStatus status = batchStatusList[i];
String key = keyList.get(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());
}
}
public static List<String> getFileList(String bucket) {
BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(bucket, "", 1000, "");
// 单次批量请求的文件数量不得超过1000
List<String> keyList = new ArrayList<String>();
while (fileListIterator.hasNext()) {
// 处理获取的file list结果
FileInfo[] items = fileListIterator.next();
for (FileInfo item : items) {
keyList.add(item.key);
}
}
return keyList;
}
public static void main(String[] args) throws Exception {
cpBucketFiles();
downFile("/Users/xxx/Desktop");
}
}