天天看點

圖檔存儲方案

介紹

在實際開發中,我們會有很多處理不同功能的伺服器。例如:

應用伺服器:負責部署我們的應用

資料庫伺服器:運作我們的資料庫

檔案伺服器:負責存儲使用者上傳檔案的伺服器

圖檔存儲方案

分伺服器處理的目的是讓伺服器各司其職,進而提高我們項目的運作效率。

常見的圖檔存儲方案:

方案一:使用nginx搭建圖檔伺服器

方案二:使用開源的分布式檔案存儲系統,例如Fastdfs、HDFS等

方案三:使用雲存儲,例如阿裡雲、七牛雲等

七牛雲存儲

七牛雲(隸屬于上海七牛資訊技術有限公司)是國内領先的以視覺智能和資料智能為核心的企業級雲計算服務商,同時也是國内知名智能視訊雲服務商,累計為 70 多萬家企業提供服務,覆寫了國内80%網民。圍繞富媒體場景推出了對象存儲、融合 CDN 加速、容器雲、大資料平台、深度學習平台等産品、并提供一站式智能視訊雲解決方案。為各行業及應用提供可持續發展的智能視訊雲生态,幫助企業快速上雲,創造更廣闊的商業價值。

官網:https://www.qiniu.com/

通過七牛雲官網介紹我們可以知道其提供了多種服務,我們主要使用的是七牛雲提供的對象存儲服務來存儲圖檔。

注冊、登入

要使用七牛雲的服務,首先需要注冊成為會員。位址:https://portal.qiniu.com/signup

圖檔存儲方案

注冊完成後就可以使用剛剛注冊的郵箱和密碼登入到七牛雲:

圖檔存儲方案

登入成功後點選頁面右上角管理控制台:

圖檔存儲方案

注意:登入成功後還需要進行實名認證才能進行相關操作。

建立存儲空間

要進行圖檔存儲,我們需要在七牛雲管理控制台建立存儲空間。點選管理控制台首頁對象存儲下的立即添加按鈕,頁面跳轉到建立存儲空間頁面:

圖檔存儲方案

可以建立多個存儲空間,各個存儲空間是互相獨立的。

檢視存儲空間資訊

存儲空間建立後,會在左側的存儲空間清單菜單中展示建立的存儲空間名稱,點選存儲空間名稱可以檢視目前存儲空間的相關資訊

圖檔存儲方案

開發者中心

可以通過七牛雲提供的開發者中心學習如何操作七牛雲服務,位址:https://developer.qiniu.com/

圖檔存儲方案

點選對象存儲,跳轉到對象存儲開發頁面,位址:https://developer.qiniu.com/kodo

圖檔存儲方案

七牛雲提供了多種方式操作對象存儲服務,本項目采用Java SDK方式,位址:https://developer.qiniu.com/kodo/sdk/1239/java

圖檔存儲方案

使用Java SDK操作七牛雲需要導入如下maven坐标:

<dependency>
  <groupId>com.qiniu</groupId>
  <artifactId>qiniu-java-sdk</artifactId>
  <version>7.2.0</version>
</dependency>      

鑒權

Java SDK的所有的功能,都需要合法的授權。授權憑證的簽算需要七牛賬号下的一對有效的Access Key和Secret Key,這對密鑰可以在七牛雲管理控制台的個人中心(https://portal.qiniu.com/user/key)獲得,如下圖:

圖檔存儲方案

Java SDK操作七牛雲

本章節我們就需要使用七牛雲提供的Java SDK完成圖檔上傳和删除,我們可以參考官方提供的例子。

//構造一個帶指定Zone對象的配置類
Configuration cfg = new Configuration(Zone.zone0());
//...其他參數參考類注釋
UploadManager uploadManager = new UploadManager(cfg);
//...生成上傳憑證,然後準備上傳
String accessKey = "your access key";
String secretKey = "your secret key";
String bucket = "your bucket name";
//如果是Windows情況下,格式是 D:\\qiniu\\test.png
String localFilePath = "/home/qiniu/test.png";
//預設不指定key的情況下,以檔案内容的hash值作為檔案名
String key = null;
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
try {
    Response response = uploadManager.put(localFilePath, key, upToken);
    //解析上傳成功的結果
    DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
    System.out.println(putRet.key);
    System.out.println(putRet.hash);
} catch (QiniuException ex) {
    Response r = ex.response;
    System.err.println(r.toString());
    try {
        System.err.println(r.bodyString());
    } catch (QiniuException ex2) {
        //ignore
    }
}      

View Code

//構造一個帶指定Zone對象的配置類
Configuration cfg = new Configuration(Zone.zone0());
//...其他參數參考類注釋
​
String accessKey = "your access key";
String secretKey = "your secret key";
​
String bucket = "your bucket name";
String key = "your file key";
​
Auth auth = Auth.create(accessKey, secretKey);
BucketManager bucketManager = new BucketManager(auth, cfg);
try {
    bucketManager.delete(bucket, key);
} catch (QiniuException ex) {
    //如果遇到異常,說明删除失敗
    System.err.println(ex.code());
    System.err.println(ex.response.toString());
}      

View Code

封裝工具類

為了友善操作七牛雲存儲服務,我們可以将官方提供的案例簡單改造成一個工具類,在我們的項目中直接使用此工具類來操作就可以:

package com.itheima.utils;
​
import com.google.gson.Gson;
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.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
​
/**
 * 七牛雲工具類
 */
public class QiniuUtils {
    public  static String accessKey = "dulF9Wze9bxujtuRvu3yyYb9JX1Sp23jzd3tO708";
    public  static String secretKey = "vZkhW7iot3uWwcWz9vXfbaP4JepdWADFDHVLMZOe";
    public  static String bucket = "qiniutest";
​
    public static void upload2Qiniu(String filePath,String fileName){
        //構造一個帶指定Zone對象的配置類
        Configuration cfg = new Configuration(Zone.zone0());
        UploadManager uploadManager = new UploadManager(cfg);
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        try {
            Response response = uploadManager.put(filePath, fileName, upToken);
            //解析上傳成功的結果
            DefaultPutRet putRet = 
              new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
        } catch (QiniuException ex) {
            Response r = ex.response;
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        }
    }
​
    //上傳檔案
    public static void upload2Qiniu(byte[] bytes, String fileName){
        //構造一個帶指定Zone對象的配置類
        Configuration cfg = new Configuration(Zone.zone0());
        //...其他參數參考類注釋
        UploadManager uploadManager = new UploadManager(cfg);
        //預設不指定key的情況下,以檔案内容的hash值作為檔案名
        String key = fileName;
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        try {
            Response response = uploadManager.put(bytes, key, upToken);
            //解析上傳成功的結果
            DefaultPutRet putRet = 
              new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        }
    }
​
    //删除檔案
    public static void deleteFileFromQiniu(String fileName){
        //構造一個帶指定Zone對象的配置類
        Configuration cfg = new Configuration(Zone.zone0());
        String key = fileName;
        Auth auth = Auth.create(accessKey, secretKey);
        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            bucketManager.delete(bucket, key);
        } catch (QiniuException ex) {
            //如果遇到異常,說明删除失敗
            System.err.println(ex.code());
            System.err.println(ex.response.toString());
        }
    }
}      

View Code

将此工具類放在health_common工程中,後續會使用到。