博客图片上传到七牛云QiniuUtil

package com.server.util;



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.util.Auth;



public class QiniuUtil {

   String BUCKET_NAME = "youdao-img";

   String ACCESS_KEY = "o9JN3myOL1STxxxxxxxxxxxxxxxxxxx";

   String SECRET_KEY = "o1OlbxVaS2wRdxxxxxxxxxxxxxxxx";

   String WAI_LINK = "http://pa58oxxxxx.bkt.clouddn.com/";

   String token = null;

   Auth auth = null;

   Configuration config = null;

   private static QiniuUtil instance = new QiniuUtil();



   private QiniuUtil() {

      auth = Auth.create(ACCESS_KEY, SECRET_KEY);

      token = auth.uploadToken(BUCKET_NAME);

      config = new Configuration(Zone.zone2());

   }



   public static QiniuUtil getInstance() {

      return instance;

   }



   public String put(String filePath, String key) {

      try {

         UploadManager uploadManager = new UploadManager(config);

         Response res = uploadManager.put(filePath, key, token);

         if (res.isOK()) {

            return WAI_LINK + key;

         }

      } catch (Exception e) {

         e.printStackTrace();

      }

      return null;

   }



   public String fetch(String url, String key) {

      try {

         BucketManager bucketManager = new BucketManager(auth, config);

         bucketManager.fetch(url, BUCKET_NAME, key);

         return WAI_LINK + key;

      } catch (Exception e) {

         e.printStackTrace();

      }

      return null;

   }

}











Author:

发表评论

电子邮件地址不会被公开。 必填项已用*标注