当前位置 : 主页 > 编程语言 > c++ >

七牛云文件储存(java)

来源:互联网 收集:自由互联 发布时间:2021-07-03
七牛云文件工具类 package com.xincd.cms.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.stor
七牛云文件工具类
package com.xincd.cms.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 com.qiniu.util.StringMap;
import com.xincd.cms.constants.SysConstants;

/**
 * @Author by Yong.Yang on 2017-08-23 下午 2:26.
 */

public class QiniuOSSUtil {
    //授权验证
    public static Auth auth = Auth.create(SysConstants.QINIU_ACCESS_KEY_ID, SysConstants.QINIU_SECRET_KEY);
    //定义变量
    public static UploadManager uploadManager = null;
    public static BucketManager bucketManager = null;
    public static String accessKey = null;
    public static String secretKey = null;
    public static String bucketName = null;
    // 令牌验证
    public static String token = null;
    //初始化配置
    static {
        Configuration cfg = new Configuration(Zone.zone0()); //空间区域
        accessKey = SysConstants.QINIU_ACCESS_KEY_ID;
        secretKey = SysConstants.QINIU_SECRET_KEY;
        bucketName = SysConstants.QINIU_BUCKET;
        uploadManager =new UploadManager(cfg);
        bucketManager =new BucketManager(auth,cfg);
    }

    /**
     * 根据空间名获取token 获取空间的token凭证
     * @param bucket
     * @return
     */
    public static String getToken(String bucket) {
        //获取到 Access Key 和 Secret Key 之后,您可以按照如下方式进行密钥配置
        return token = auth.uploadToken(bucket);

    }

    /**
     * 七牛上传的方法
     * 对同名的文件覆盖
     * @return
     */
    public static void uploadFile(String bucket, String filePath, String key) {
        try {
            //token = getToken(bucket); //同名跳过
            token = auth.uploadToken(bucket, key,10, new StringMap().put("insertOnly", 0)); //同名覆盖
            Response response = uploadManager.put(filePath, key, token);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            if(response.statusCode==200){
                System.out.println("图片上传成功...HASH:"+putRet.hash+"文件Key:"+putRet.key);
            }
        } catch (QiniuException e) {
            Response r = e.response;
            System.out.println(r.toString());
            e.printStackTrace();
        }
    }

    /**
     * 根据空间名和文件key 删除某空间的文件
     * @param bucket
     * @param key
     */
    public static void delBucketKey(String bucket, String key) {
        try {
            bucketManager.delete(bucket, key);
        } catch (QiniuException e) {
            Response r = e.response;
            System.out.println(r.toString());
        }
    }

    public static void main(String[] args) {
        //确定自己的某个磁盘下有个图片
        String file="E:\\aaa.jpg";
        uploadFile(bucketName, file, "20170823161344gHWre.jpg");
    }
}
网友评论