| File was renamed from ard-sample/src/main/java/com/ard/sample/component/oss/service/impl/AmazonS3ServiceImpl.java |
| | |
| | | package com.ard.sample.component.oss.service.impl; |
| | | package com.ard.oss.service.impl; |
| | | |
| | | import com.amazonaws.HttpMethod; |
| | | import com.amazonaws.auth.AWSStaticCredentialsProvider; |
| | | import com.amazonaws.auth.BasicAWSCredentials; |
| | | import com.amazonaws.services.s3.AmazonS3; |
| | | import com.amazonaws.services.s3.AmazonS3ClientBuilder; |
| | | import com.amazonaws.services.s3.model.*; |
| | | import com.amazonaws.services.s3.model.ObjectMetadata; |
| | | import com.amazonaws.services.s3.model.PutObjectRequest; |
| | | import com.amazonaws.services.s3.model.PutObjectResult; |
| | | import com.amazonaws.services.securitytoken.AWSSecurityTokenService; |
| | | import com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClientBuilder; |
| | | import com.amazonaws.services.securitytoken.model.AssumeRoleRequest; |
| | | import com.amazonaws.services.securitytoken.model.AssumeRoleResult; |
| | | import com.amazonaws.services.securitytoken.model.Credentials; |
| | | import com.ard.sample.component.AuthInterceptor; |
| | | import com.ard.common.config.OssConfiguration; |
| | | import com.ard.sample.component.oss.model.enums.OssTypeEnum; |
| | | import com.ard.sample.component.oss.service.IOssService; |
| | | import com.ard.sample.media.model.CredentialsDTO; |
| | | import com.ard.oss.model.CredentialsDTO; |
| | | import com.ard.oss.model.enums.OssTypeEnum; |
| | | import com.ard.oss.service.IOssService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | import java.io.InputStream; |
| | | import java.net.URL; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | |
| | | /** |
| | |
| | | client.deleteObject(bucket, objectKey); |
| | | return true; |
| | | } |
| | | |
| | | @Override |
| | | public InputStream getObject(String bucket, String objectKey) { |
| | | return client.getObject(bucket, objectKey).getObjectContent().getDelegateStream(); |
| | |
| | | .build(); |
| | | } |
| | | |
| | | /** |
| | | * Configuring cross-origin resource sharing |
| | | */ |
| | | @PostConstruct |
| | | private void configCORS() { |
| | | if (!OssConfiguration.enable || !OssTypeEnum.AWS.getType().equals(OssConfiguration.provider)) { |
| | | return; |
| | | } |
| | | List<CORSRule.AllowedMethods> allowedMethods = new ArrayList<>(); |
| | | allowedMethods.add(CORSRule.AllowedMethods.GET); |
| | | allowedMethods.add(CORSRule.AllowedMethods.POST); |
| | | allowedMethods.add(CORSRule.AllowedMethods.DELETE); |
| | | |
| | | CORSRule rule = new CORSRule() |
| | | .withId("CORSAccessRule") |
| | | .withAllowedOrigins(List.of("*")) |
| | | .withAllowedHeaders(List.of(AuthInterceptor.PARAM_TOKEN)) |
| | | .withAllowedMethods(allowedMethods); |
| | | |
| | | client.setBucketCrossOriginConfiguration(OssConfiguration.bucket, |
| | | new BucketCrossOriginConfiguration().withRules(rule)); |
| | | |
| | | } |
| | | // /** |
| | | // * Configuring cross-origin resource sharing |
| | | // */ |
| | | // @PostConstruct |
| | | // private void configCORS() { |
| | | // if (!OssConfiguration.enable || !OssTypeEnum.AWS.getType().equals(OssConfiguration.provider)) { |
| | | // return; |
| | | // } |
| | | // List<CORSRule.AllowedMethods> allowedMethods = new ArrayList<>(); |
| | | // allowedMethods.add(CORSRule.AllowedMethods.GET); |
| | | // allowedMethods.add(CORSRule.AllowedMethods.POST); |
| | | // allowedMethods.add(CORSRule.AllowedMethods.DELETE); |
| | | // |
| | | // CORSRule rule = new CORSRule() |
| | | // .withId("CORSAccessRule") |
| | | // .withAllowedOrigins(List.of("*")) |
| | | // .withAllowedHeaders(List.of(AuthInterceptor.PARAM_TOKEN)) |
| | | // .withAllowedMethods(allowedMethods); |
| | | // |
| | | // client.setBucketCrossOriginConfiguration(OssConfiguration.bucket, |
| | | // new BucketCrossOriginConfiguration().withRules(rule)); |
| | | // |
| | | // } |
| | | } |