ghy-all/ghy-common/src/main/java/com/ghy/common/utils/QiniuUtils.java

38 lines
1.2 KiB
Java
Raw Normal View History

2022-03-14 16:56:11 +08:00
package com.ghy.common.utils;
import com.google.gson.Gson;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.ghy.common.config.QiniuConfig;
import com.ghy.common.utils.uuid.UUID;
import com.qiniu.common.QiniuException;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
public class QiniuUtils {
public static String getUpToken() {
Auth auth = Auth.create(QiniuConfig.getAccessKey(), QiniuConfig.getSecretKey());
return auth.uploadToken(QiniuConfig.getBucketName());
}
public static String upload(byte[] uploadBytes) throws QiniuException {
String key = UUID.randomUUID().toString().replaceAll("-", "");
Configuration cfg = new Configuration(Region.region0());//设置华南的服务器
UploadManager uploadManager = new UploadManager(cfg);
String upToken = getUpToken();
Response response = uploadManager.put(uploadBytes, key, upToken);
//解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
return QiniuConfig.getMediaUrl() + putRet.key;
}
}