关于七牛云存储使用的点滴(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


猜你喜欢

转载自blog.csdn.net/fzq_javaee/article/details/80494850
今日推荐