七牛(7.0.3)工具类,java上传下载demo


依赖包


	org.apache.commons
	commons-lang3
	3.5


	com.qiniu
	qiniu-java-sdk
	7.0.3

代码

package xxx.xxx;

import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.File;

/**
 * Created by HGG on 17/5/31.
 */
public class QiNiuUtils {

    private static Log logger= LogFactory.getLog(QiNiuUtils.class);

    private static UploadManager uploadManager = new UploadManager();

    private static String ACCESS_KEY = "xxxxxxxxx";

    private static String SECRET_KEY = "xxxxxxxxx";

    private static Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);

    private static BucketManager bucketManager = new BucketManager(auth);

    private static String HTTP_DOMAIN="http://xxxx.qiniucdn.com";

    public static final String BUCKET = "bucket-name";


    /**
     * 有效时长,单位秒。默认900s
     */
    public static final long EXPIRES_TIME = 900l;



    public static String getUploadTokenDefault(String bucket) {
        return auth.uploadToken(bucket);
    }

    public static String getUploadTokenByKey(String bucket, String key) {
        return auth.uploadToken(bucket, key);
    }

    public static void uploadOneObject(byte[] byteOrFile, String key, String token) throws Exception {
        Response res = uploadManager.put(byteOrFile, key, token);
        if(!res.isOK()){
            throw new Exception("upload response not ok.key:"+key+",token:"+token);
        }else{
            logger.info("[UploadOK]-- key:"+key);
        }
    }

    public static String getPrivateUrl(String key){
        return auth.privateDownloadUrl(HTTP_DOMAIN+key);
    }


    /**
     * 生成下载url
     * @param key
     * @return
     */
    public static String getDownloadUrl(String key){
        //第二个参数可以设置Token的过期时间
        return auth.privateDownloadUrl(HTTP_DOMAIN +"/"+ key,EXPIRES_TIME);
    }

    /**
     * 删除文件
     * @param key 文件key,多个以逗号间隔
     */
    public static void deleteByKeys(String key){
        if (StringUtils.isNotBlank(key)){
            String[] keys = key.split(",");
            for (String fileKey: keys) {
                try{
                    bucketManager.delete(BUCKET, fileKey);
                }catch (QiniuException e){
                    logger.error("七牛文件不存在,删除失败,key:"+fileKey);
                }
            }
        }
    }


    /**
     * 上传文件
     * @param key
     * @return
     * @throws QiniuException
     */
    public static String uploadFile(String key) throws QiniuException {
        Response res = uploadManager.put(new File("D://data.txt"),"2017731data",getUploadTokenDefault(BUCKET));
        //打印返回的信息
        return res.bodyString();
    }

    public static void main(String[] args) throws Exception {
        Response res = uploadManager.put(new File("D://data.txt"),"2017731data",getUploadTokenDefault(BUCKET));
        //打印返回的信息
        System.out.println(res.bodyString());
    }

}






你可能感兴趣的:(java,云存储(七牛))