關於七牛雲存儲使用的點滴(QinNiu)

上次本想寫七牛雲相關的,結果太晚沒熬住.抽點時間整理一下吧.

和其他的sdk一樣申請,accessKey ,accessSecret,Appid.下載對應的sdk,引入到工程中,我引的是qiniu-java-sdk.

我們一般用到這個文件或者圖片上傳的都會封裝成一個工具類,指定參數完成初始化,提取常用的靜態方法.

pipeline 這個是在控制檯創建的音視頻 的一個私有隊列,做視頻幀數縮略圖存儲需要指定的.

zone2 是指華南服務器, 還有zone0 zone1 好像對應的是華北 華東,還有一個autoZone 自動判斷鏈接的服務器

public  class QinNiuUtils {
   public static String accessKey = "***";
   public static String secretKey = "*************************";
   public static String bucket = "*******";
   public static String domainName = "http://**********.clouddn.com";
   public static String pipeline = "***************";

   public static Auth auth = Auth.create(accessKey, secretKey);
   public static Configuration cfg = new Configuration(Zone.zone2());
   public static UploadManager uploadManager = new UploadManager(cfg);
// public static String upToken = auth.uploadToken(bucket);
   public static String userinfo_qrcode_filePath = "userinfo\\qrcode\\filePath";

獲取token方法,這個前端他們做上傳下載時候需要從後臺拿的

public static String  getToken(){
    String upToken = auth.uploadToken(bucket);
    return upToken;
}

文件上傳

public static String uploadFile(FileInputStream file) throws FileNotFoundException{
// StringMap putPolicy = new StringMap();
// putPolicy.put("returnBody", "{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"bucket\":\"$(bucket)\",\"fsize\":$(fsize)}");
// 
   try {
//      FileInputStream fileInputStream =new FileInputStream("D:\\work\\soft\\aaa.png");
       Response response = uploadManager.put(file,null,getToken(),null, null);
       DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
//     FileInfo info = uploadManager.stat("", "");
        return domainName+File.separatorChar+putRet.key;
   } catch (QiniuException ex) {
       Response r = ex.response;
       System.err.println(r.toString());
       try {
           System.err.println(r.bodyString());
       } catch (QiniuException ex2) {
          ex2.printStackTrace();
       }
   
}
   return null;
}

還有很多其他的,關於,增刪改的,操作和其他的,見官網

https://developer.qiniu.com/kodo/sdk/1239/java#1

帶數據處理的憑證 這個來自官網文檔

七牛支持在文件上傳到七牛之後,立即對其進行多種指令的數據處理,這個只需要在生成的上傳憑證中指定相關的處理參數即可。

<button title="" class="btn copybtn" data-toggle="tooltip" data-placement="top" container="body" data-original-title="拷貝代碼" data-clipboard-text="" string="" accesskey=""access" key";"="" style="margin: 0px; font-style: inherit; font-variant: inherit; font-stretch: inherit; font-size: 12px; line-height: 1.42857; font-family: inherit; color: rgb(118, 118, 118); overflow: visible; cursor: pointer; padding: 5px 8px; white-space: nowrap; vertical-align: middle; touch-action: manipulation; user-select: none; background-image: none; border-width: 1px; border-style: solid; border-color: rgb(225, 225, 232); border-radius: 0px 4px; position: absolute; background-color: rgb(255, 255, 255); right: -2px; top: -1px;">拷貝
  1. Auth auth = Auth.create(accessKey, secretKey);
  2. StringMap putPolicy = new StringMap();
  3. //數據處理指令,支持多個指令
  4. String saveMp4Entry = String.format("%s:avthumb_test_target.mp4", bucket);
  5. String saveJpgEntry = String.format("%s:vframe_test_target.jpg", bucket);
  6. String avthumbMp4Fop = String.format("avthumb/mp4|saveas/%s", UrlSafeBase64.encodeToString(saveMp4Entry));
  7. String vframeJpgFop = String.format("vframe/jpg/offset/1|saveas/%s", UrlSafeBase64.encodeToString(saveJpgEntry));
  8. //將多個數據處理指令拼接起來
  9. String persistentOpfs = StringUtils.join(new String[]{
  10. avthumbMp4Fop, vframeJpgFop
  11. }, ";");
  12. putPolicy.put("persistentOps", persistentOpfs);
  13. //數據處理隊列名稱,必填
  14. putPolicy.put("persistentPipeline", "mps-pipe1");
  15. //數據處理完成結果通知地址
  16. putPolicy.put("persistentNotifyUrl", "http://api.example.com/qiniu/pfop/notify");
  17. long expireSeconds = 3600;
  18. String upToken = auth.uploadToken(bucket, null, expireSeconds, putPolicy);
  19. System.out.println(upToken);
    /**
    * 獲取文件的信息
    * @param fileKey
    */
   public static FileInfo  getFileMsg(String fileKey){
        //構造一個帶指定Zone對象的配置類
        Configuration cfg = new Configuration(Zone.zone2());
        Auth auth = Auth.create(accessKey, secretKey);
        BucketManager bucketManager = new BucketManager(auth, cfg);
      FileInfo fileInfo = null;
      try {
         fileInfo = bucketManager.stat(bucket, fileKey);
           System.out.println(fileInfo.hash);
           System.out.println(fileInfo.fsize);
           System.out.println(fileInfo.mimeType);
           System.out.println(fileInfo.putTime);
        } catch (QiniuException ex) {
           System.err.println(ex.response.toString());
        }
        return fileInfo;
}
/**
 * 獲取空間文件列表
 */
 public static  FileInfo[]  getFileList() {
    //構造一個帶指定Zone對象的配置類 (zone2 代表華南)
    Configuration cfg = new Configuration(Zone.zone2());
//...其他參數參考類註釋
    Auth auth = Auth.create(accessKey, secretKey);
    BucketManager bucketManager = new BucketManager(auth, cfg);
//文件名前綴
    String prefix = "";
//每次迭代的長度限制,最大1000,推薦值 1000
    int limit = 1000;
//指定目錄分隔符,列出所有公共前綴(模擬列出目錄效果)。缺省值爲空字符串
    String delimiter = "";
//列舉空間文件列表
    BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(bucket, prefix, limit, delimiter);
    while (fileListIterator.hasNext()) {
       //處理獲取的file list結果
       FileInfo[] items = fileListIterator.next();
       for (FileInfo item : items) {
          System.out.println(item.key);
          System.out.println(item.hash);
          System.out.println(item.fsize);
          System.out.println(item.mimeType);
          System.out.println(item.putTime);
          System.out.println(item.endUser);
       }
       return items;
    }
    return null;
 }
/**
 * 獲取到視頻的縮略圖
 */
public static String getVframe(String videoKey) {
   // HttpUrl client = new HttpUrl(domainName+"/video_20180528164559?vframe/png/offset/5");
   Auth auth = Auth.create(accessKey, secretKey);
   Configuration cfg = new Configuration(Zone.zone2());

   // 新建一個OperationManager對象
   OperationManager operater = new OperationManager(auth, cfg);
   // 設置要轉碼的空間和key,並且這個key在你空間中存在
   String key = videoKey;
   // 設置轉碼操作參數
   String fops = "vframe/jpg/offset/0.025/w/480/h/360";
   //設置轉碼的隊列
   String pipeline2 = pipeline;
   //可以對轉碼後的文件進行使用saveas參數自定義命名,當然也可以不指定文件會默認命名並保存在當前空間。
   //設置pipeline參數
   StringMap params = new StringMap().putWhen("force", 1, true).putNotEmpty("pipeline", pipeline2);
   try {
      //persistentId  z2.5b0bd78be3d00468087bc950
      String pfop = operater.pfop(bucket, key,fops, params);
      //持久化的 執行進度
      OperationStatus operationStatus = operater.prefop(pfop);
      // http://p***********/uMAtcafSVjwXomg-y6DfpirkmPk=/FiR-CWCKlrWiQg561v0NjByFNXSh
      System.out.println("persistentId"+ pfop);
      System.out.println("執行狀態"+ operationStatus.code);

      if( 0 == operationStatus.code && operationStatus.items.length > 0){
         return domainName+"/"+operationStatus.items[0].key;
      }
   } catch (QiniuException e) {
      // 捕獲異常信息
      Response r = e.response;
      // 請求失敗時簡單狀態信息
      System.out.println(r.toString());
      try {
         // 響應的文本信息
         System.out.println(r.bodyString());
      } catch (QiniuException e1) {
         e1.printStackTrace();
      }
   }
   return null;
}
傳入視頻保存時候的key 值,會根據
vframe/jpg/offset/0.025/w/480/h/360

截取0.025秒幀的圖,而且指定寬度是480 高度是360的圖片,通過我們指定的Pipeline,保存一張截圖在七牛雲上.返回的參數就是operationStatus 在這個裏面 有我們持久化的id ,操作狀態,以及返回的那張圖片的key值.雖然在這裏返回的這個key值並不是存儲空間裏面標註的目標位置名,但是加上我們的外鏈域名和item裏面的key就可以訪問到那張圖片,

//還有一張方式就是實時獲取視頻秒幀截圖,只要在視頻url後面拼接上這個 一樣是可以的只是這個就不會走隊列存儲在七牛雲上.我們做視頻第一幀時候也不需要存儲了

?vframe/jpg/offset/0.025/w/480/h/360


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章