Search in sources :

Example 1 with Configuration

use of com.qiniu.storage.Configuration in project xmall by Exrick.

the class QiniuUtil method qiniuUpload.

public static String qiniuUpload(String filePath) {
    // 构造一个带指定Zone对象的配置类 zone2华南
    Configuration cfg = new Configuration(Zone.zone2());
    UploadManager uploadManager = new UploadManager(cfg);
    String localFilePath = filePath;
    // 默认不指定key的情况下,以文件内容的hash值作为文件名
    String key = null;
    Auth auth = Auth.create(accessKey, secretKey);
    String upToken = auth.uploadToken(bucket);
    try {
        Response response = uploadManager.put(localFilePath, key, upToken);
        // 解析上传成功的结果
        DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
        return origin + putRet.key;
    } catch (QiniuException ex) {
        Response r = ex.response;
        log.warn(r.toString());
        try {
            log.warn(r.bodyString());
            return r.bodyString();
        } catch (QiniuException ex2) {
        // ignore
        }
    }
    return null;
}
Also used : Response(com.qiniu.http.Response) QiniuException(com.qiniu.common.QiniuException) Configuration(com.qiniu.storage.Configuration) Auth(com.qiniu.util.Auth) Gson(com.google.gson.Gson) DefaultPutRet(com.qiniu.storage.model.DefaultPutRet) UploadManager(com.qiniu.storage.UploadManager)

Aggregations

Gson (com.google.gson.Gson)1 QiniuException (com.qiniu.common.QiniuException)1 Response (com.qiniu.http.Response)1 Configuration (com.qiniu.storage.Configuration)1 UploadManager (com.qiniu.storage.UploadManager)1 DefaultPutRet (com.qiniu.storage.model.DefaultPutRet)1 Auth (com.qiniu.util.Auth)1