1. 程式人生 > >物件儲存服務OSS使用案例之騰訊COS、七牛雲端儲存、阿里OSS

物件儲存服務OSS使用案例之騰訊COS、七牛雲端儲存、阿里OSS

這裡寫圖片描述

專案目錄結構:

  • ossdemo-aliyunoss:阿里OSS使用案例(尚未完成)
  • ossdemo-qcloudcos :騰訊雲COS使用案例
  • ossdemo-qiniu :七牛雲端儲存使用案例 pom.xml

騰訊雲COS

2、免費額度

這裡寫圖片描述

相比目前市場上主流的雲端儲存平臺,這個配置對於一般的小型網際網路公司來說還是比較有人的地方。

3、專案介紹

這裡寫圖片描述

使用的是:SpringMVC、MyBatis 程式碼較為簡單,主要對CosCloud的一層簡單的封裝,載入配置資訊等操作。下邊是QcloudClient的部分程式碼:

package com.xuliugen.ossdemo.qcloudclient;

import
com.qcloud.cosapi.api.CosCloud; import com.qcloud.cosapi.sign.Sign; import com.xuliugen.ossdemo.common.constant.QcloudConst; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; import java.io.IOException; import java.util.Properties; /** * 自己定義的七牛檔案上傳Client * Created by xuliugen on 16/5/22. */
public class QcloudClient { private static Properties props = new Properties(); private static Logger log = LoggerFactory.getLogger(QcloudClient.class); static { try { props.load(QcloudClient.class.getResourceAsStream("/qcloud.properties")); } catch (IOException e) { log.error("載入配置檔案失敗"
, e); } } static int APP_ID = Integer.parseInt(props.getProperty("APP_ID")); static String SECRET_ID = props.getProperty("SECRET_ID"); static String SECRET_KEY = props.getProperty("SECRET_KEY"); static String bucketName = props.getProperty("bucketName"); /** * 自動判斷檔案的大小,已使用是否是分片的方式上傳檔案 * @param remoteFolderPath 遠端資料夾的名稱 * @param loaclPath 檔案的本地路徑 * @return */ public static String uploadFile(String remoteFolderPath, String loaclPath) { //獲取檔案輸入流 File f = new File(loaclPath); String result = null; if (f.exists() && f.isFile()) { long size = f.length(); if (size <= 8 * 1024 * 1024) { //小於等於8M result = cosUploadFile(remoteFolderPath + getFileName(loaclPath), loaclPath); } else { result = sliceUploadFileParallel(remoteFolderPath + getFileName(loaclPath), loaclPath); } } else { System.out.println("file doesn't exist or is not a file"); } // JSONObject retJson = new JSONObject(result); // int code = retJson.getInt(ResponseBodyKey.CODE); //檔案上傳返回碼 //如果錯誤 封裝資訊顯示 // return retJson.getString(ResponseBodyKey.MESSAGE); //檔案上傳返回資訊 return result; } /** * 上傳一個長度為10個位元組的檔案 * @param remotePath * @param loaclPath * @return */ public static String cosUploadFile(String remotePath, String loaclPath) { CosCloud cos = new CosCloud(APP_ID, SECRET_ID, SECRET_KEY, 60); String result = null; try { result = cos.uploadFile(bucketName, remotePath, loaclPath); } catch (Exception e) { e.printStackTrace(); } System.out.println("uploadFile result:" + result); return result; } /** * 序列分片上傳檔案, 使用預設分片大小 * @param remotePath * @param localPath * @return */ public static String sliceUploadFile(String remotePath, String localPath) { CosCloud cos = new CosCloud(APP_ID, SECRET_ID, SECRET_KEY, 60); String result = null; try { result = cos.sliceUploadFile(bucketName, remotePath, localPath); cos.shutdown(); } catch (Exception e) { e.printStackTrace(); } System.out.println("sliceUploadFile result: " + result); return result; } /** * 序列分片上傳檔案, 使用分片大小512KB * @param remotePath * @param localPath * @return */ public static String sliceUploadFileParallel(String remotePath, String localPath) { CosCloud cos = new CosCloud(APP_ID, SECRET_ID, SECRET_KEY, 60); String result = null; try { result = cos.sliceUploadFileParallel(bucketName, remotePath, localPath, 512 * 1024); cos.shutdown(); } catch (Exception e) { e.printStackTrace(); } System.out.println("para sliceUploadFile result: " + result); return result; }

該Module可以進行單獨的部署,和另外兩個Module完全獨立,便於單獨抽出進行使用。

詳細程式碼請到專案中檢視。

要修改resources下的qcloud.properties為自己賬戶下的資訊:

APP_ID=
SECRET_ID=
SECRET_KEY=
bucketName=

七牛雲端儲存

2、免費額度

這裡寫圖片描述

這裡相對於騰訊的COS空間明顯少了很多。

3、專案結構

這個和騰訊雲COS的專案結構一樣的,不多做解釋,使用到的是:

package com.xuliugen.ossdemo.qcloudclient;

import com.qiniu.storage.BucketManager;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Properties;

/**
 * 自己定義的七牛檔案上傳Client
 * Created by xuliugen on 16/5/22.
 */
public class QiniuClient {

    private static Properties props = new Properties();
    private static Logger log = LoggerFactory.getLogger(QiniuClient.class);

    static {
        try {
            props.load(QiniuClient.class.getResourceAsStream("/qiniu.properties"));
        } catch (IOException e) {
            log.error("載入配置檔案失敗", e);
        }
    }

    static String accessKey = props.getProperty("accessKey");
    static String secretKey = props.getProperty("secretKey");
    static String bucket = props.getProperty("bucket");

    static Auth auth = Auth.create(accessKey, secretKey);

    /**
     * 上傳檔案到七牛伺服器(斷點上傳)
     * @param key  檔名
     * @param data 檔案內容  byte[]
     * @throws Exception
     */
    public static void uploadFile(byte[] data, String key) throws Exception {
        //同名檔案覆蓋操作、只能上傳指定key的檔案可以可通過此方法獲取token
        String token = auth.uploadToken(bucket, key);
        UploadManager uploadManager = new UploadManager();
        //上傳資料
        uploadManager.put(data, key, token);
    }

    /**
     * 從七牛伺服器下載檔案(獲取下載地址)
     * @param key 檔名
     * @throws Exception
     */
    public static String downloadFile(String key) throws Exception {
        return auth.privateDownloadUrl(key, 3600 * 24);
    }

    /**
     * 從七牛伺服器刪除檔案
     * @param key 檔名
     */
    public static void delFile(String key) throws Exception {
        BucketManager bucketManager = new BucketManager(auth);
        bucketManager.delete(bucket, key);
    }
}

要修改resources下的qiniu.properties為自己賬戶下的資訊:

accessKey=
secretKey=
bucket=
APP_ID=
SECRET_ID=
SECRET_KEY=
bucketName=

各平臺的比較

1、免費額度的對比
這個剛才上述都已經做了對比,另外阿里雲的OSS是沒有提供免費額度的,這個免費的服務在2015年的時候據說都已經停止了。

2、API對比

(1)騰訊雲COS的上傳檔案的API有一個很糾結的地方,如下:

    /**
     * 單個檔案上傳,適用於小檔案
     * @param bucketName bucket名稱
     * @param remotePath 遠端檔案路徑
     * @param localPath 本地檔案路徑
     * @return  伺服器端返回的操作結果,成員code為0表示成功,具體參照文件手冊
     * @throws Exception
     */
    public String uploadFile(String bucketName, String remotePath, String localPath)
        throws Exception {}

這裡的remotePath遠端檔案路徑,我原本以為只是一個COS上bucket下需要將該檔案上傳到的的一個資料夾目錄,但是根據官方提供的Demo來看,這個是bucket下你要儲存的檔名的路徑。
例如:
我有一個檔案需要上傳,demo.txt bucket 名字為xuliugen4demo 下邊有一個資料夾demo,那麼remotePath必須要指定為:/demo/demo.txt ,這裡的localPath是demo.txt上傳到本地的路徑,上傳到本地伺服器之後的位置為:/upload,那麼localPath的值就是/upload/demo.txt。

因此,問題來了,我既然在localPath制定了檔案的名字,我難道還需要在remotePath中在指定一次嗎?

當然這個不是一個大問題,在個人使用的時候,如果,上傳的檔案存放到cos不需要重新命名(設定一個不重複的名稱,uuid命名的方式),那麼這個引數有一點不理性的地方,如果localPath上傳的檔案是已經設定為uuid命名的不重複的檔名,那麼remotePath的檔名完全可以在localPath的檔名中獲取。
這種方式的一個使用地方就是,上傳到本地伺服器localPath的檔名仍沒有改變為不重複的uuid命名,但是上傳到cos之後需要進行變更。總的來說,感覺倒不如直接將remotePath設定為cos要上傳的資料夾的位置。

(個人意見,可能有沒有看到這個方法更多的使用場景。)

另外,對於小檔案的上傳不支援,直接通過流的方式直接上傳,這點事處於檔案安全性的問題,但是感覺沒有這個方法,對於一些場景下的檔案上傳,總是先把檔案上傳到伺服器進行儲存,找到localPath然後再上傳到cos,上傳成功之後,然後在進行刪除本地的方法,這種方法。。。

另外,騰訊雲COS支援資料夾的建立和操作,這個功能很爽,可以根據年月進行檔案的分類管理,方便實用,這個是在下邊要將的 七牛雲中還沒有的功能。

(2)七牛雲上傳檔案的方式有一個直接支援檔案流上傳的方式,其他的都大同小異,很重要的一點是,七牛雲上沒有看到支援資料夾的API,貌似有點不方便。不過七牛三年的時間 還在慢慢成長。

3、速度對比

網上說的是阿里雲的OOS快於騰訊的COS,這點沒有自己去實驗,只是引出,後期有時間,會貼上實驗的結果。

最後,這幾個平臺正在慢慢摸索,後期會加入對錯誤的處理,容錯,錯誤重試等方法,個人水平有限,如有建議歡迎留言回覆。