package com.stylefeng.guns.modular.system.util.qiniuyun;
|
|
import com.google.gson.Gson;
|
import com.qiniu.common.QiniuException;
|
import com.qiniu.common.Zone;
|
import com.qiniu.http.Response;
|
import com.qiniu.storage.Configuration;
|
import com.qiniu.storage.Region;
|
import com.qiniu.storage.UploadManager;
|
import com.qiniu.storage.model.DefaultPutRet;
|
import com.qiniu.util.Auth;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import java.io.ByteArrayInputStream;
|
import java.io.IOException;
|
import java.io.UnsupportedEncodingException;
|
import java.util.UUID;
|
|
/**
|
* 对象存储
|
* @author zhibing.pu
|
* @Date 2023/3/31 11:08
|
*/
|
public class KodoUtil {
|
private static String accessKey = "6DqM0CEdAVON3mL3C6E23Vuj--635Q1hdjOwnpXw";
|
private static String secretKey = "q35tZkQjvBCRkDsfWksY3PCr3wqUuJlodE7EF5qg";
|
private static String bucketName = "rbkj-zycx";
|
|
|
/**
|
* 普通上传
|
* @param file
|
* @return
|
* @throws IOException
|
*/
|
public static String upload(MultipartFile file) throws IOException {
|
String originalFilename = file.getOriginalFilename();
|
String fileName = UUID.randomUUID().toString().replaceAll("-","") + originalFilename.subSequence(originalFilename.lastIndexOf("."), originalFilename.length());
|
|
try {
|
//构造一个带指定 Region 对象的配置类
|
Configuration cfg = new Configuration(Region.autoRegion());
|
cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;// 指定分片上传版本
|
//...其他参数参考类注释
|
UploadManager uploadManager = new UploadManager(cfg);
|
|
byte[] uploadBytes = file.getBytes();
|
ByteArrayInputStream byteInputStream=new ByteArrayInputStream(uploadBytes);
|
Auth auth = Auth.create(accessKey, secretKey);
|
String upToken = auth.uploadToken(bucketName);
|
try {
|
Response response = uploadManager.put(byteInputStream,fileName,upToken,null, null);
|
//解析上传成功的结果
|
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
|
// System.out.println(putRet.key);
|
// System.out.println(putRet.hash);
|
return "https://qncdn.zhaoyangchuxing.com/" + putRet.key;
|
} catch (QiniuException ex) {
|
Response r = ex.response;
|
System.err.println(r.toString());
|
try {
|
System.err.println(r.bodyString());
|
} catch (QiniuException ex2) {
|
ex2.printStackTrace();
|
}
|
}
|
} catch (UnsupportedEncodingException ex) {
|
ex.printStackTrace();
|
}
|
return null;
|
}
|
|
|
|
// 分片上传 v1
|
public static String uploadv1(MultipartFile file){
|
String originalFilename = file.getOriginalFilename();
|
String fileName = UUID.randomUUID().toString().replaceAll("-","") + originalFilename.subSequence(originalFilename.lastIndexOf("."), originalFilename.length());
|
Configuration cfg = new Configuration();
|
UploadManager uploadManager = new UploadManager(cfg);
|
Auth auth = Auth.create(accessKey, secretKey);
|
String token = auth.uploadToken(bucketName);
|
Response r = null;
|
try {
|
r = uploadManager.put(file.getBytes(), fileName, token);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return r.address;
|
}
|
|
// 分片上传 v2
|
public static String uploadv2(MultipartFile file){
|
String originalFilename = file.getOriginalFilename();
|
String fileName = UUID.randomUUID().toString().replaceAll("-","") + originalFilename.subSequence(originalFilename.lastIndexOf("."), originalFilename.length());
|
Configuration cfg = new Configuration();
|
cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;
|
UploadManager uploadManager = new UploadManager(cfg);
|
Auth auth = Auth.create(accessKey, secretKey);
|
String token = auth.uploadToken(bucketName);
|
Response r = null;
|
try {
|
r = uploadManager.put(file.getBytes(), fileName, token);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return r.address;
|
}
|
}
|