陈长荣 2 сар өмнө
parent
commit
1630a82e0b

+ 30 - 61
src/main/java/com/github/jfcloud/excel/editor/docdeal/oss/service/OssTemplate.java

@@ -11,7 +11,7 @@ import com.amazonaws.services.s3.AmazonS3Client;
 import com.amazonaws.services.s3.model.*;
 import com.amazonaws.util.IOUtils;
 import com.github.jfcloud.excel.editor.docdeal.oss.OssProperties;
-import org.springframework.beans.factory.InitializingBean;
+import org.springframework.stereotype.Component;
 
 import java.io.ByteArrayInputStream;
 import java.io.InputStream;
@@ -22,71 +22,55 @@ import java.util.*;
  * @author zj
  * @date 2023-4-25
  */
-public class OssTemplate implements InitializingBean {
-    private final OssProperties ossProperties;
-    private AmazonS3 amazonS3;
+@Component
+public class OssTemplate {
+    private final AmazonS3 amazonS3;
 
     public OssTemplate(OssProperties ossProperties) {
-        this.ossProperties = ossProperties;
+        ClientConfiguration clientConfiguration = new ClientConfiguration();
+        clientConfiguration.setMaxConnections(ossProperties.getMaxConnections());
+        AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(ossProperties.getEndpoint(), ossProperties.getRegion());
+        AWSCredentials awsCredentials = new BasicAWSCredentials(ossProperties.getAccessKey(), ossProperties.getSecretKey());
+        AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(awsCredentials);
+        amazonS3 = AmazonS3Client.builder()
+                .withEndpointConfiguration(endpointConfiguration)
+                .withClientConfiguration(clientConfiguration)
+                .withCredentials(awsCredentialsProvider)
+                .disableChunkedEncoding()
+                .withPathStyleAccessEnabled(ossProperties.getPathStyleAccess())
+                .build();
     }
 
     public void createBucket(String bucketName) {
-        try {
-            if (!this.amazonS3.doesBucketExistV2(bucketName)) {
-                this.amazonS3.createBucket(bucketName);
-            }
-
-        } catch (Throwable var3) {
-            throw var3;
+        if (!this.amazonS3.doesBucketExistV2(bucketName)) {
+            this.amazonS3.createBucket(bucketName);
         }
     }
 
     public List<Bucket> getAllBuckets() {
-        try {
-            return this.amazonS3.listBuckets();
-        } catch (Throwable var2) {
-            throw var2;
-        }
+        return this.amazonS3.listBuckets();
     }
 
     public Optional<Bucket> getBucket(String bucketName) {
-        try {
-            return this.amazonS3.listBuckets().stream().filter((b) -> {
-                return b.getName().equals(bucketName);
-            }).findFirst();
-        } catch (Throwable var3) {
-            throw var3;
-        }
+        return this.amazonS3.listBuckets().stream().filter(b -> b.getName().equals(bucketName)).findFirst();
     }
 
     public void removeBucket(String bucketName) {
-        try {
-            this.amazonS3.deleteBucket(bucketName);
-        } catch (Throwable var3) {
-            throw var3;
-        }
+        this.amazonS3.deleteBucket(bucketName);
     }
 
     public List<S3ObjectSummary> getAllObjectsByPrefix(String bucketName, String prefix, boolean recursive) {
-        try {
-            ObjectListing objectListing = this.amazonS3.listObjects(bucketName, prefix);
-            return new ArrayList(objectListing.getObjectSummaries());
-        } catch (Throwable var5) {
-            throw var5;
-        }
+        ObjectListing objectListing = this.amazonS3.listObjects(bucketName, prefix);
+        return new ArrayList<>(objectListing.getObjectSummaries());
     }
 
     public String getObjectURL(String bucketName, String objectName, Integer expires) {
-        try {
-            Date date = new Date();
-            Calendar calendar = new GregorianCalendar();
-            calendar.setTime(date);
-            calendar.add(5, expires);
-            URL url = this.amazonS3.generatePresignedUrl(bucketName, objectName, calendar.getTime());
-            return url.toString();
-        } catch (Throwable var7) {
-            throw var7;
-        }
+        Date date = new Date();
+        Calendar calendar = new GregorianCalendar();
+        calendar.setTime(date);
+        calendar.add(Calendar.DATE, expires);
+        URL url = this.amazonS3.generatePresignedUrl(bucketName, objectName, calendar.getTime());
+        return url.toString();
     }
 
     public boolean exist(String bucketName, String objectName) {
@@ -121,7 +105,7 @@ public class OssTemplate implements InitializingBean {
     }
 
     public void putObject(String bucketName, String objectName, InputStream stream) throws Exception {
-        this.putObject(bucketName, objectName, stream, (long) stream.available(), "application/octet-stream");
+        this.putObject(bucketName, objectName, stream, stream.available(), "application/octet-stream");
     }
 
     public PutObjectResult putObject(String bucketName, String objectName, InputStream stream, long size, String contextType) throws Exception {
@@ -156,19 +140,4 @@ public class OssTemplate implements InitializingBean {
     public void removeObject(String bucketName, String objectName) {
         this.amazonS3.deleteObject(bucketName, objectName);
     }
-
-    public void afterPropertiesSet() {
-        ClientConfiguration clientConfiguration = new ClientConfiguration();
-        clientConfiguration.setMaxConnections(this.ossProperties.getMaxConnections());
-        AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(this.ossProperties.getEndpoint(), this.ossProperties.getRegion());
-        AWSCredentials awsCredentials = new BasicAWSCredentials(this.ossProperties.getAccessKey(), this.ossProperties.getSecretKey());
-        AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(awsCredentials);
-        this.amazonS3 = AmazonS3Client.builder()
-                .withEndpointConfiguration(endpointConfiguration)
-                .withClientConfiguration(clientConfiguration)
-                .withCredentials(awsCredentialsProvider)
-                .disableChunkedEncoding()
-                .withPathStyleAccessEnabled(ossProperties.getPathStyleAccess())
-                .build();
-    }
 }