七牛是个好东东。然则其东西qrsync只能往上sync不能往下sync。相称的不方便么。官方只提供了一个list的接口和例程。http://developer.qiniu.com/docs/v6/sdk/java-sdk.html#rsf-listPrefix
如何用最偷懒的方法把七牛上的数据拖下来呢?轻微改下例程.
/**
* Created by fangjian on 14-12-1.
* 把天生的index.html文件放到microcard.qiniudn.com下
* 运用wget敕令镜像站点
* wget -c -m http://<你的bucket>.qiniudn.com/index.html
*/
import com.qiniu.api.auth.digest.Mac;
import com.qiniu.api.config.Config;
import com.qiniu.api.rsf.ListItem;
import com.qiniu.api.rsf.ListPrefixRet;
import com.qiniu.api.rsf.RSFClient;
import com.qiniu.api.rsf.RSFEofException;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
public class ListPrefix {
public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
// List<ListItem> all=fakeList();//for test
List<ListItem> all=getList();
printList(all);
buildHtml(all);
}
public static List<ListItem> getList(){
final String bucketName="<你的bucket>";
Config.ACCESS_KEY = "<你的ACCESS_KEY>";
Config.SECRET_KEY = "<你的SECRET_KEY>";
Mac mac = new Mac(Config.ACCESS_KEY, Config.SECRET_KEY);
RSFClient client = new RSFClient(mac);
String marker = "";
List<ListItem> all = new ArrayList<ListItem>();
ListPrefixRet ret = null;
while (true) {
ret = client.listPrifix(bucketName, "", marker, 1000);
marker = ret.marker;
all.addAll(ret.results);
if (!ret.ok()) {
// no more items or error occurs
break;
}
}
if (ret.exception.getClass() != RSFEofException.class) {
// error handler
}
return all;
}
public static void printList(List<ListItem> all){
for(ListItem item:all){
System.out.println(item.key);
}
}
public static List<ListItem> fakeList(){
ListItem a=new ListItem();
a.key="abc";
ListItem b=new ListItem();
b.key="def";
ArrayList<ListItem> listItems=new ArrayList<ListItem>();
listItems.add(a);
listItems.add(b);
return listItems;
}
public static void buildHtml(List<ListItem> all) throws FileNotFoundException, UnsupportedEncodingException {
PrintWriter writer = new PrintWriter("index.html", "UTF-8");
writer.println("<HTML><BODY>");
for(ListItem item:all){
writer.println(makeALink(item.key));
}
writer.println("</BODY><HTML>");
writer.close();
}
public static String makeALink(String href){
String link="<a href='"+href+"'>"+href+"</a><br/>";
return link;
}
}
运转顺序会获得一个index.html,把这个index.html上传到七牛。
然后用wget -c -m http://<你的bucket>.qiniudn.com/index.html
功德圆满!