use of com.qiniu.storage.model.DefaultPutRet 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;
}
use of com.qiniu.storage.model.DefaultPutRet in project paascloud-master by paascloud.
the class OptQiniuOssServiceImpl method uploadFile.
@Override
public OptUploadFileRespDto uploadFile(byte[] uploadBytes, String fileName, String filePath, String bucketName) throws IOException {
log.info("uploadFile - 上传文件. fileName={}, bucketName={}", fileName, bucketName);
Preconditions.checkArgument(uploadBytes != null, "读取文件失败");
Preconditions.checkArgument(StringUtils.isNotEmpty(fileName), ErrorCodeEnum.OPC10040010.msg());
Preconditions.checkArgument(StringUtils.isNotEmpty(filePath), "文件路径不能为空");
Preconditions.checkArgument(StringUtils.isNotEmpty(bucketName), "存储节点不能为空");
InputStream is = new ByteArrayInputStream(uploadBytes);
String inputStreamFileType = FileTypeUtil.getType(is);
String newFileName = UniqueIdGenerator.generateId() + "." + inputStreamFileType;
// 检查数据大小
this.checkFileSize(uploadBytes);
Response response = uploadManager.put(uploadBytes, filePath + newFileName, getUpToken(bucketName));
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
log.info("uploadFile - 上传文件. [OK] putRet={}", putRet);
if (PublicUtil.isEmpty(putRet) || StringUtils.isEmpty(putRet.key)) {
throw new OpcBizException(ErrorCodeEnum.OPC10040009);
}
String fileUrl;
// 获取图片路径
if (StringUtils.equals(OPEN_IMG_BUCKET, bucketName)) {
fileUrl = paascloudProperties.getQiniu().getOss().getPublicHost() + "/" + filePath + newFileName;
} else {
String domainUrl = paascloudProperties.getQiniu().getOss().getPrivateHost();
fileUrl = this.getFileUrl(domainUrl, fileName);
}
OptUploadFileRespDto result = new OptUploadFileRespDto();
result.setAttachmentUrl(fileUrl);
result.setAttachmentName(newFileName);
result.setAttachmentPath(filePath);
return result;
}
Aggregations