Welcome to Yumao′s Blog.
研究了下阿裏云提供的oss java sdk
發現有點不靠譜
還好阿裏云有給出api接口文檔
那就照著api接口文檔做好了
成果地址: http://teemo.name/
幾個需要解決的問題:
1.文檔定位問題
使用prefix和delimiter兩個參數就可以定位資料夾地址
以及顯示幾層目錄文檔
2.關於Sign加密問題
文章内容會給出一個加密工具
用來HMAC_SHA1加密使用
3.返回XML解析問題
直接採用JOX進行分層解析轉換即可
4.訪問方式
採用httpclient
先給出樹結構
今天先給出四個Utils的源
//GMTUtils
package name.yumao.oss.utils;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.SimpleTimeZone;
public class GMTUtils {
public static String getGMTDateStr() {
Date d = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
dateFormat.setTimeZone(new SimpleTimeZone(0, "GMT"));
return dateFormat.format(d);
}
}
//JOXUtils
package name.yumao.oss.utils;
import java.io.ByteArrayInputStream;
import com.wutka.jox.JOXBeanInputStream;
import name.yumao.oss.entity.ListBucketResultBean;
public class JOXUtils {
public static ListBucketResultBean XML2Bean(String resData){
ListBucketResultBean listBucketResultBean = new ListBucketResultBean();
try{
ByteArrayInputStream stream = new ByteArrayInputStream(resData.getBytes("UTF-8"));
JOXBeanInputStream joxIn = new JOXBeanInputStream(stream);
listBucketResultBean = (ListBucketResultBean) joxIn.readObject(ListBucketResultBean.class);
//关闭流
stream.close();
joxIn.close();
}catch(Exception e){
//logger.info("XML转Bean失败!");
}
return listBucketResultBean;
}
}
//OSSUtils
package name.yumao.oss.utils;
import name.yumao.oss.entity.CommonPrefixesBean;
import name.yumao.oss.entity.ContentsBean;
import name.yumao.oss.entity.OssFileBean;
public class OSSUtils {
//设置特定文件打开方式
public static OssFileBean FilesTODO(OssFileBean ossFileBean,ContentsBean contentsBean){
if(contentsBean.getKey().startsWith("Tarena/")
||contentsBean.getKey().startsWith("Docs/")){
ossFileBean.setToDo(
"downFile('http://reader.oss.aliyuncs.com/Utils/FlexPaperViewer.swf?SwfFile=http://reader.oss.aliyuncs.com/"
+ contentsBean.getKey() + "&Scale=2.0')");
}
if(contentsBean.getKey().endsWith(".mp3")){
ossFileBean.setToDo(
"downFile('http://reader.oss.aliyuncs.com/Utils/player.swf?soundfile=http://reader.oss.aliyuncs.com/"
+ contentsBean.getKey() + "&autostart=yes')");
}
return ossFileBean;
}
//设置特定文件夹打开方式
public static OssFileBean FoldersTODO(OssFileBean ossFileBean,CommonPrefixesBean commonPrefixesBean){
if(commonPrefixesBean.getPrefix().equals("Java_1.6_API/")
||commonPrefixesBean.getPrefix().equals("JQuery/")
||commonPrefixesBean.getPrefix().equals("PHP_API/")){
ossFileBean.setToDo("downFile('http://reader.oss.aliyuncs.com/" + commonPrefixesBean.getPrefix() + "index.html')");
}
return ossFileBean;
}
}
//SignUtils
package name.yumao.oss.utils;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
public class SignUtils {
private static final String HMAC_SHA1 = "HmacSHA1";
public static byte[] getSignature(byte[] data, byte[] key) throws InvalidKeyException, NoSuchAlgorithmException {
SecretKeySpec signingKey = new SecretKeySpec(key, HMAC_SHA1);
Mac mac = Mac.getInstance(HMAC_SHA1);
mac.init(signingKey);
byte[] rawHmac = mac.doFinal(data);
return rawHmac;
}
}
