Welcome to Yumao′s Blog.
阿裏云OSS分享系統設計(一)
, 2013年03月17日 , Java Language , 评论 在〈阿裏云OSS分享系統設計(一)〉中留言功能已關閉 ,

研究了下阿裏云提供的oss java sdk
發現有點不靠譜
還好阿裏云有給出api接口文檔
那就照著api接口文檔做好了
成果地址: http://teemo.name/

幾個需要解決的問題:
1.文檔定位問題
使用prefix和delimiter兩個參數就可以定位資料夾地址
以及顯示幾層目錄文檔
2.關於Sign加密問題
文章内容會給出一個加密工具
用來HMAC_SHA1加密使用
3.返回XML解析問題

直接採用JOX進行分層解析轉換即可
4.訪問方式
採用httpclient

先給出樹結構

aliyun_oss

今天先給出四個Utils的源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//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(
             + contentsBean.getKey() + "&Scale=2.0')");
        }
        if(contentsBean.getKey().endsWith(".mp3")){
            ossFileBean.setToDo(
            + 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;
    }
}
关键字:, , ,

评论已关闭