|
@@ -36,13 +36,11 @@ public class AliyunOSS {
|
|
|
private static class Builder{
|
|
|
private static OSSClientBuilder ossClientBuilder = new OSSClientBuilder();
|
|
|
}
|
|
|
-
|
|
|
/**
|
|
|
* 无参构造,初始化oss
|
|
|
*/
|
|
|
private AliyunOSS() {
|
|
|
- hint("Default");
|
|
|
- init();
|
|
|
+ init("Default");
|
|
|
}
|
|
|
/**
|
|
|
* 全参构造方法
|
|
@@ -57,18 +55,14 @@ public class AliyunOSS {
|
|
|
this.accessKeyId = accessKeyId;
|
|
|
this.accessKeySecret = accessKeySecret;
|
|
|
this.bucketName = bucketName;
|
|
|
- hint("Custom");
|
|
|
- init();
|
|
|
- }
|
|
|
-
|
|
|
- private void hint(String type){
|
|
|
- log.info("Hint: Using the {} Configuration To Create Aliyun OSS Client.",type);
|
|
|
+ init("Custom");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 初始化,创建OSSClient实例
|
|
|
*/
|
|
|
- private void init() {
|
|
|
+ private void init(String type) {
|
|
|
+ log.info("Hint: Using the {} Configuration To Create Aliyun OSS Client.",type);
|
|
|
if (null == ossClient) {
|
|
|
ossClient = Builder.ossClientBuilder.build(endpoint, accessKeyId, accessKeySecret);
|
|
|
}
|
|
@@ -155,38 +149,6 @@ public class AliyunOSS {
|
|
|
destroy();
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- /**
|
|
|
- * 批量上传文件到oss,返回 key:objectName ,value:url
|
|
|
- *
|
|
|
- * @param bucketName
|
|
|
- * @param ossEntities
|
|
|
- * @return
|
|
|
- */
|
|
|
- public Map<String, String> putObjects(String bucketName, List<OssEntity> ossEntities) {
|
|
|
- try {
|
|
|
- if (IterUtil.isEmpty(ossEntities)) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- Map<String, String> urls = new HashMap<>();
|
|
|
- ossEntities.forEach(entity -> {
|
|
|
- // 上传内容到指定的存储空间(bucketName)并保存为指定的文件名称(objectName)。
|
|
|
- String objectName = entity.getObjectName();
|
|
|
- ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(entity.getContent()));
|
|
|
- String url = getObjectUrl(bucketName, objectName);
|
|
|
- log.info("objectName: {}, url: {}", objectName, url);
|
|
|
- urls.put(objectName, url);
|
|
|
- });
|
|
|
- return urls;
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("上传文件异常: {}", e);
|
|
|
- return null;
|
|
|
- } finally {
|
|
|
- destroy();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
public String putObject(String objectName, byte[] content) {
|
|
|
if (StrUtil.isEmpty(objectName)) {
|
|
|
return null;
|