|
@@ -0,0 +1,108 @@
|
|
|
|
+package com.chelvc.framework.upload.support;
|
|
|
|
+
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.io.InputStream;
|
|
|
|
+import java.util.Arrays;
|
|
|
|
+
|
|
|
|
+import com.aliyun.oss.ClientBuilderConfiguration;
|
|
|
|
+import com.aliyun.oss.OSS;
|
|
|
|
+import com.aliyun.oss.OSSClientBuilder;
|
|
|
|
+import com.aliyun.oss.common.auth.DefaultCredentialProvider;
|
|
|
|
+import com.aliyun.oss.common.comm.Protocol;
|
|
|
|
+import com.aliyun.oss.common.comm.SignVersion;
|
|
|
|
+import com.aliyun.oss.model.CannedAccessControlList;
|
|
|
|
+import com.aliyun.oss.model.ObjectMetadata;
|
|
|
|
+import com.aliyuncs.DefaultAcsClient;
|
|
|
|
+import com.aliyuncs.IAcsClient;
|
|
|
|
+import com.aliyuncs.cdn.model.v20180510.RefreshObjectCachesRequest;
|
|
|
|
+import com.aliyuncs.exceptions.ClientException;
|
|
|
|
+import com.aliyuncs.profile.DefaultProfile;
|
|
|
|
+import com.chelvc.framework.base.util.HttpUtils;
|
|
|
|
+import com.chelvc.framework.common.util.AssertUtils;
|
|
|
|
+import com.chelvc.framework.common.util.ObjectUtils;
|
|
|
|
+import com.chelvc.framework.upload.UploadHandler;
|
|
|
|
+import com.chelvc.framework.upload.config.UploadProperties;
|
|
|
|
+import lombok.NonNull;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 基于阿里云OSS文件上传实现
|
|
|
|
+ *
|
|
|
|
+ * @author Woody
|
|
|
|
+ * @date 2024/4/3
|
|
|
|
+ */
|
|
|
|
+public class AliyunUploadHandler implements UploadHandler {
|
|
|
|
+ private final OSS oss;
|
|
|
|
+ private final IAcsClient acs;
|
|
|
|
+ private final String domain;
|
|
|
|
+ private final String directory;
|
|
|
|
+
|
|
|
|
+ public AliyunUploadHandler(@NonNull UploadProperties properties) {
|
|
|
|
+ String appid = AssertUtils.nonempty(properties.getAppid(), () -> "Aliyun upload appid is missing");
|
|
|
|
+ String secret = AssertUtils.nonempty(properties.getSecret(), () -> "Aliyun upload secret is missing");
|
|
|
|
+ String region = AssertUtils.nonempty(properties.getRegion(), () -> "Aliyun upload region is missing");
|
|
|
|
+ String endpoint = AssertUtils.nonempty(properties.getEndpoint(), () -> "Aliyun upload endpoint is missing");
|
|
|
|
+ this.domain = AssertUtils.nonempty(properties.getDomain(), () -> "Aliyun upload domain is missing");
|
|
|
|
+ this.directory = AssertUtils.nonempty(properties.getDirectory(), () -> "Aliyun upload directory is missing");
|
|
|
|
+
|
|
|
|
+ // 初始化OSS客户端
|
|
|
|
+ ClientBuilderConfiguration configuration = new ClientBuilderConfiguration();
|
|
|
|
+ configuration.setSignatureVersion(SignVersion.V4);
|
|
|
|
+ configuration.setProtocol(Protocol.HTTPS);
|
|
|
|
+ configuration.setMaxConnections(properties.getHttpclient().getMaxTotal());
|
|
|
|
+ configuration.setConnectionTimeout(properties.getHttpclient().getConnectTimeout());
|
|
|
|
+ configuration.setSocketTimeout(properties.getHttpclient().getSocketTimeout());
|
|
|
|
+ configuration.setConnectionRequestTimeout(properties.getHttpclient().getConnectionRequestTimeout());
|
|
|
|
+ this.oss = OSSClientBuilder.create().region(region).endpoint(endpoint).clientConfiguration(configuration)
|
|
|
|
+ .credentialsProvider(new DefaultCredentialProvider(appid, secret)).build();
|
|
|
|
+
|
|
|
|
+ //初始化ACS客户端
|
|
|
|
+ this.acs = new DefaultAcsClient(DefaultProfile.getProfile(region, appid, secret));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void refreshContentDeliveryNetwork(@NonNull String... urls) {
|
|
|
|
+ if (ObjectUtils.isEmpty(urls)) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //单次提交多个 URL 或多个目录时,使用换行符(\n)或(\r\n)分隔。单次提交的任务中所有 URL 的域名个数总和需要在 10 个以内
|
|
|
|
+ ObjectUtils.split(Arrays.asList(urls), 10, (n, sublist) -> {
|
|
|
|
+ RefreshObjectCachesRequest request = new RefreshObjectCachesRequest();
|
|
|
|
+ request.setObjectPath(String.join("\\n", sublist));
|
|
|
|
+ request.setObjectType("File");
|
|
|
|
+ try {
|
|
|
|
+ this.acs.getAcsResponse(request);
|
|
|
|
+ } catch (ClientException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public String upload(@NonNull File file, @NonNull String filename) throws IOException {
|
|
|
|
+ ObjectMetadata metadata = new ObjectMetadata();
|
|
|
|
+ metadata.setObjectAcl(CannedAccessControlList.PublicRead);
|
|
|
|
+ this.oss.putObject(this.directory, filename, file, metadata);
|
|
|
|
+ return HttpUtils.url(this.domain, filename);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public String upload(@NonNull InputStream stream, @NonNull String filename) throws IOException {
|
|
|
|
+ return this.upload(stream, filename, stream.available());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public String upload(@NonNull InputStream stream, @NonNull String filename, long length) throws IOException {
|
|
|
|
+ ObjectMetadata metadata = new ObjectMetadata();
|
|
|
|
+ metadata.setContentLength(length);
|
|
|
|
+ metadata.setObjectAcl(CannedAccessControlList.PublicRead);
|
|
|
|
+ this.oss.putObject(this.directory, filename, stream, metadata);
|
|
|
|
+ return HttpUtils.url(this.domain, filename);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void destroy() {
|
|
|
|
+ this.oss.shutdown();
|
|
|
|
+ }
|
|
|
|
+}
|