|
|
@@ -1,10 +1,13 @@
|
|
|
package cn.tr.plugin.file.config.ali;
|
|
|
|
|
|
+import cn.hutool.core.date.DateField;
|
|
|
+import cn.hutool.core.date.DateTime;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.io.IoUtil;
|
|
|
import cn.tr.core.utils.MediaTypeUtils;
|
|
|
import cn.tr.plugin.file.config.AbstractFileClient;
|
|
|
import cn.tr.plugin.file.config.FileContent;
|
|
|
+import com.aliyun.oss.HttpMethod;
|
|
|
import com.aliyun.oss.OSS;
|
|
|
import com.aliyun.oss.OSSClientBuilder;
|
|
|
import com.aliyun.oss.model.*;
|
|
|
@@ -12,6 +15,9 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
import java.io.IOException;
|
|
|
+import java.net.URL;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
|
|
|
/**
|
|
|
* @ClassName : AliFileClient
|
|
|
@@ -71,4 +77,29 @@ public class AliFileClient extends AbstractFileClient<AliFileClientConfig> {
|
|
|
bizPath=parseBizPath(bizPath);
|
|
|
return ossClient.generatePresignedUrl(getConfig().getAliBucketName(),bizPath, DateUtil.tomorrow()).toString();
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String generationPreUploadUrl(String fileName) {
|
|
|
+ // 指定生成的预签名URL过期时间,单位为毫秒。默认24小时过期
|
|
|
+ DateTime expiration = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, 1);
|
|
|
+ // 生成预签名URL。
|
|
|
+ GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(getConfig().getAliBucketName(),
|
|
|
+ fileName,
|
|
|
+ HttpMethod.PUT);
|
|
|
+ boolean exists = ossClient.doesBucketExist(getConfig().getAliBucketName());
|
|
|
+ if (!exists) {
|
|
|
+ ossClient.createBucket(getConfig().getAliBucketName());
|
|
|
+ ossClient.setBucketAcl(getConfig().getAliBucketName(), CannedAccessControlList.PublicReadWrite);
|
|
|
+ }
|
|
|
+ HashMap<String, String> headers = new HashMap<>();
|
|
|
+ // 设置过期时间。
|
|
|
+ headers.put("Content-Type","application/octet-stream");
|
|
|
+ headers.put("X-oss-storage-class","Standard");
|
|
|
+ request.setHeaders(headers);
|
|
|
+ request.setExpiration(expiration);
|
|
|
+ // 通过HTTP PUT请求生成预签名URL。
|
|
|
+ // 打印预签名URL。
|
|
|
+ URL url= ossClient.generatePresignedUrl(request);
|
|
|
+ return url.toString();
|
|
|
+ }
|
|
|
}
|