use of com.aliyun.oss.model.PutObjectResult in project blade-tool by chillzhuang.
the class AliossTemplate method put.
@SneakyThrows
public BladeFile put(String bucketName, InputStream stream, String key, boolean cover) {
makeBucket(bucketName);
String originalName = key;
key = getFileName(key);
// 覆盖上传
if (cover) {
ossClient.putObject(getBucketName(bucketName), key, stream);
} else {
PutObjectResult response = ossClient.putObject(getBucketName(bucketName), key, stream);
int retry = 0;
int retryCount = 5;
while (StringUtils.isEmpty(response.getETag()) && retry < retryCount) {
response = ossClient.putObject(getBucketName(bucketName), key, stream);
retry++;
}
}
BladeFile file = new BladeFile();
file.setOriginalName(originalName);
file.setName(key);
file.setLink(fileLink(bucketName, key));
return file;
}
use of com.aliyun.oss.model.PutObjectResult in project druid by apache.
the class OssTaskLogsTest method testPushInternal.
private List<Grant> testPushInternal(boolean disableAcl, String ownerId, String ownerDisplayName) throws Exception {
EasyMock.expect(ossClient.putObject(EasyMock.anyObject())).andReturn(new PutObjectResult()).once();
AccessControlList aclExpected = new AccessControlList();
aclExpected.setOwner(new Owner(ownerId, ownerDisplayName));
EasyMock.expect(ossClient.getBucketAcl(TEST_BUCKET)).andReturn(aclExpected).once();
EasyMock.expect(ossClient.putObject(EasyMock.anyObject(PutObjectRequest.class))).andReturn(new PutObjectResult()).once();
EasyMock.replay(ossClient);
OssTaskLogsConfig config = new OssTaskLogsConfig();
config.setDisableAcl(disableAcl);
config.setBucket(TEST_BUCKET);
CurrentTimeMillisSupplier timeSupplier = new CurrentTimeMillisSupplier();
OssInputDataConfig inputDataConfig = new OssInputDataConfig();
OssTaskLogs taskLogs = new OssTaskLogs(ossClient, config, inputDataConfig, timeSupplier);
String taskId = "index_test-datasource_2019-06-18T13:30:28.887Z";
File logFile = tempFolder.newFile("test_log_file");
taskLogs.pushTaskLog(taskId, logFile);
return new ArrayList<>(aclExpected.getGrants());
}
use of com.aliyun.oss.model.PutObjectResult in project kms by mahonelau.
the class OssBootUtil method upload.
/**
* 上传文件至阿里云 OSS
* 文件上传成功,返回文件完整访问路径
* 文件上传失败,返回 null
*
* @param file 待上传文件
* @param fileDir 文件保存目录
* @return oss 中的相对文件路径
*/
public static String upload(MultipartFile file, String fileDir, String customBucket) {
String FILE_URL = null;
initOSS(endPoint, accessKeyId, accessKeySecret);
StringBuilder fileUrl = new StringBuilder();
String newBucket = bucketName;
if (oConvertUtils.isNotEmpty(customBucket)) {
newBucket = customBucket;
}
try {
// 判断桶是否存在,不存在则创建桶
if (!ossClient.doesBucketExist(newBucket)) {
ossClient.createBucket(newBucket);
}
// 获取文件名
String orgName = file.getOriginalFilename();
if ("" == orgName) {
orgName = file.getName();
}
orgName = CommonUtils.getFileName(orgName);
String fileName = orgName.indexOf(".") == -1 ? orgName + "_" + System.currentTimeMillis() : orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.lastIndexOf("."));
if (!fileDir.endsWith("/")) {
fileDir = fileDir.concat("/");
}
// update-begin-author:wangshuai date:20201012 for: 过滤上传文件夹名特殊字符,防止攻击
fileDir = StrAttackFilter.filter(fileDir);
// update-end-author:wangshuai date:20201012 for: 过滤上传文件夹名特殊字符,防止攻击
fileUrl = fileUrl.append(fileDir + fileName);
if (oConvertUtils.isNotEmpty(staticDomain) && staticDomain.toLowerCase().startsWith("http")) {
FILE_URL = staticDomain + "/" + fileUrl;
} else {
FILE_URL = "https://" + newBucket + "." + endPoint + "/" + fileUrl;
}
PutObjectResult result = ossClient.putObject(newBucket, fileUrl.toString(), file.getInputStream());
// ossClient.setBucketAcl(newBucket, CannedAccessControlList.PublicRead);
if (result != null) {
log.info("------OSS文件上传成功------" + fileUrl);
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
return FILE_URL;
}
use of com.aliyun.oss.model.PutObjectResult in project kms by mahonelau.
the class OssBootUtil method upload.
/**
* 上传文件到oss
* @param stream
* @param relativePath
* @return
*/
public static String upload(InputStream stream, String relativePath) {
String FILE_URL = null;
String fileUrl = relativePath;
initOSS(endPoint, accessKeyId, accessKeySecret);
if (oConvertUtils.isNotEmpty(staticDomain) && staticDomain.toLowerCase().startsWith("http")) {
FILE_URL = staticDomain + "/" + relativePath;
} else {
FILE_URL = "https://" + bucketName + "." + endPoint + "/" + fileUrl;
}
PutObjectResult result = ossClient.putObject(bucketName, fileUrl.toString(), stream);
// 设置权限(公开读)
ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
if (result != null) {
log.info("------OSS文件上传成功------" + fileUrl);
}
return FILE_URL;
}
use of com.aliyun.oss.model.PutObjectResult in project halo-plugin-experimental by guqing.
the class AliOssFileHandler method upload.
@Override
@NonNull
public UploadResult upload(@NonNull MultipartFile file) {
Assert.notNull(file, "Multipart file must not be null");
// Get config
String protocol = optionService.getByPropertyOfNonNull(AliOssProperties.OSS_PROTOCOL).toString();
String domain = optionService.getByPropertyOrDefault(AliOssProperties.OSS_DOMAIN, String.class, "");
String source = optionService.getByPropertyOrDefault(AliOssProperties.OSS_SOURCE, String.class, "");
String endPoint = optionService.getByPropertyOfNonNull(AliOssProperties.OSS_ENDPOINT).toString();
String accessKey = optionService.getByPropertyOfNonNull(AliOssProperties.OSS_ACCESS_KEY).toString();
String accessSecret = optionService.getByPropertyOfNonNull(AliOssProperties.OSS_ACCESS_SECRET).toString();
String bucketName = optionService.getByPropertyOfNonNull(AliOssProperties.OSS_BUCKET_NAME).toString();
String styleRule = optionService.getByPropertyOrDefault(AliOssProperties.OSS_STYLE_RULE, String.class, "");
String thumbnailStyleRule = optionService.getByPropertyOrDefault(AliOssProperties.OSS_THUMBNAIL_STYLE_RULE, String.class, "");
// Init OSS client
OSS ossClient = new OSSClientBuilder().build(endPoint, accessKey, accessSecret);
StringBuilder basePath = new StringBuilder(protocol);
if (StringUtils.isNotEmpty(domain)) {
basePath.append(domain).append(URL_SEPARATOR);
} else {
basePath.append(bucketName).append(".").append(endPoint).append(URL_SEPARATOR);
}
try {
FilePathDescriptor uploadFilePath = new FilePathDescriptor.Builder().setBasePath(basePath.toString()).setSubPath(source).setAutomaticRename(true).setRenamePredicate(relativePath -> attachmentRepository.countByFileKeyAndType(relativePath, AttachmentType.ALIOSS) > 0).setOriginalName(file.getOriginalFilename()).build();
log.info(basePath.toString());
// Upload
final PutObjectResult putObjectResult = ossClient.putObject(bucketName, uploadFilePath.getRelativePath(), file.getInputStream());
if (putObjectResult == null) {
throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到阿里云失败 ");
}
// Response result
final UploadResult uploadResult = new UploadResult();
uploadResult.setFilename(uploadFilePath.getName());
String fullPath = uploadFilePath.getFullPath();
uploadResult.setFilePath(StringUtils.isBlank(styleRule) ? fullPath : fullPath + styleRule);
uploadResult.setKey(uploadFilePath.getRelativePath());
uploadResult.setMediaType(MediaType.valueOf(Objects.requireNonNull(file.getContentType())));
uploadResult.setSuffix(uploadFilePath.getExtension());
uploadResult.setSize(file.getSize());
handleImageMetadata(file, uploadResult, () -> {
if (ImageUtils.EXTENSION_ICO.equals(uploadFilePath.getExtension())) {
return fullPath;
} else {
return StringUtils.isBlank(thumbnailStyleRule) ? fullPath : fullPath + thumbnailStyleRule;
}
});
log.info("Uploaded file: [{}] successfully", file.getOriginalFilename());
return uploadResult;
} catch (Exception e) {
throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到阿里云失败 ", e).setErrorData(file.getOriginalFilename());
} finally {
ossClient.shutdown();
}
}
Aggregations