use of com.qiniu.http.Response 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.http.Response in project paascloud-master by paascloud.
the class OptQiniuOssServiceImpl method deleteFile.
@Override
@Retryable(value = Exception.class, backoff = @Backoff(delay = 5000, multiplier = 2))
public void deleteFile(String fileName, String bucketName) throws QiniuException {
log.info("deleteFile - 删除OSS文件. fileName={}, bucketName={}", fileName, bucketName);
Preconditions.checkArgument(StringUtils.isNotEmpty(fileName), ErrorCodeEnum.OPC10040010.msg());
Preconditions.checkArgument(StringUtils.isNotEmpty(bucketName), "存储空间不能为空");
Response response = bucketManager.delete(bucketName, fileName);
log.info("deleteFile - 删除OSS文件. [OK] response={}", response);
}
use of com.qiniu.http.Response in project paascloud-master by paascloud.
the class OptQiniuOssServiceImpl method batchDeleteFile.
@Override
public Set<String> batchDeleteFile(String[] fileNameList, String bucketName) throws QiniuException {
log.info("batchDeleteFile - 删除OSS文件. fileNameList={}, bucketName={}", fileNameList, bucketName);
BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations();
batchOperations.addDeleteOp(bucketName, fileNameList);
Response response = bucketManager.batch(batchOperations);
BatchStatus[] batchStatusList = response.jsonToObject(BatchStatus[].class);
Set<String> failSet = Sets.newHashSet();
for (int i = 0; i < fileNameList.length; i++) {
BatchStatus status = batchStatusList[i];
String fileName = fileNameList[i];
if (status.code != 200) {
failSet.add(fileName);
log.error("batchDeleteFile - 删除OSS文件. [FAIL] fileName={}, error={}", fileName, status.data.error);
} else {
log.info("batchDeleteFile - 删除OSS文件. [OK] fileName={}, bucketName={}", fileName, bucketName);
}
}
return failSet;
}
use of com.qiniu.http.Response 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