| | |
| | | import com.amazonaws.auth.BasicAWSCredentials; |
| | | import com.amazonaws.services.s3.AmazonS3; |
| | | import com.amazonaws.services.s3.AmazonS3ClientBuilder; |
| | | import com.amazonaws.services.s3.model.BucketCrossOriginConfiguration; |
| | | import com.amazonaws.services.s3.model.CORSRule; |
| | | import com.amazonaws.services.s3.model.S3Object; |
| | | import com.amazonaws.services.s3.model.*; |
| | | import com.amazonaws.services.securitytoken.AWSSecurityTokenService; |
| | | import com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClientBuilder; |
| | | import com.amazonaws.services.securitytoken.model.AssumeRoleRequest; |
| | |
| | | import com.dji.sample.component.oss.model.enums.OssTypeEnum; |
| | | import com.dji.sample.component.oss.service.IOssService; |
| | | import com.dji.sample.media.model.CredentialsDTO; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.net.URL; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | |
| | | /** |
| | | * @author sean |
| | | * @version 1.0 |
| | | * @date 2022/4/27 |
| | | */ |
| | | @Slf4j |
| | | @Service |
| | | public class AmazonS3ServiceImpl implements IOssService { |
| | | |
| | | @Autowired |
| | | private OssConfiguration configuration; |
| | | |
| | | private AmazonS3 client; |
| | | |
| | | @Override |
| | | public String getOssType() { |
| | | return OssTypeEnum.AWS.getType(); |
| | |
| | | public CredentialsDTO getCredentials() { |
| | | AWSSecurityTokenService stsClient = AWSSecurityTokenServiceClientBuilder.standard() |
| | | .withCredentials(new AWSStaticCredentialsProvider( |
| | | new BasicAWSCredentials(configuration.getAccessKey(), configuration.getSecretKey()))) |
| | | .withRegion(configuration.getRegion()).build(); |
| | | new BasicAWSCredentials(OssConfiguration.accessKey, OssConfiguration.secretKey))) |
| | | .withRegion(OssConfiguration.region).build(); |
| | | |
| | | AssumeRoleRequest request = new AssumeRoleRequest() |
| | | .withRoleArn(configuration.getRoleArn()) |
| | | .withRoleSessionName(configuration.getRoleSessionName()) |
| | | .withDurationSeconds(Math.toIntExact(configuration.getExpire())); |
| | | .withRoleArn(OssConfiguration.roleArn) |
| | | .withRoleSessionName(OssConfiguration.roleSessionName) |
| | | .withDurationSeconds(Math.toIntExact(OssConfiguration.expire)); |
| | | AssumeRoleResult result = stsClient.assumeRole(request); |
| | | Credentials credentials = result.getCredentials(); |
| | | stsClient.shutdown(); |
| | | return new CredentialsDTO(credentials); |
| | | } |
| | | |
| | | @Override |
| | | public URL getObjectUrl(String bucket, String objectKey) { |
| | | AmazonS3 client = this.createClient(); |
| | | URL url = client.generatePresignedUrl(bucket, objectKey, |
| | | new Date(System.currentTimeMillis() + configuration.getExpire() * 1000), HttpMethod.GET); |
| | | client.shutdown(); |
| | | return url; |
| | | return client.generatePresignedUrl(bucket, objectKey, |
| | | new Date(System.currentTimeMillis() + OssConfiguration.expire * 1000), HttpMethod.GET); |
| | | } |
| | | |
| | | @Override |
| | | public Boolean deleteObject(String bucket, String objectKey) { |
| | | AmazonS3 client = this.createClient(); |
| | | if (!client.doesObjectExist(bucket, objectKey)) { |
| | | client.shutdown(); |
| | | return true; |
| | | } |
| | | client.deleteObject(bucket, objectKey); |
| | | client.shutdown(); |
| | | return true; |
| | | } |
| | | |
| | | public byte[] getObject(String bucket, String objectKey) { |
| | | AmazonS3 client = this.createClient(); |
| | | S3Object object = client.getObject(bucket, objectKey); |
| | | |
| | | try (InputStream stream = object.getObjectContent().getDelegateStream()) { |
| | | return stream.readAllBytes(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | | client.shutdown(); |
| | | } |
| | | return new byte[0]; |
| | | public InputStream getObject(String bucket, String objectKey) { |
| | | return client.getObject(bucket, objectKey).getObjectContent().getDelegateStream(); |
| | | } |
| | | |
| | | private AmazonS3 createClient() { |
| | | return AmazonS3ClientBuilder.standard() |
| | | @Override |
| | | public void putObject(String bucket, String objectKey, InputStream input) { |
| | | if (client.doesObjectExist(bucket, objectKey)) { |
| | | throw new RuntimeException("The filename already exists."); |
| | | } |
| | | PutObjectResult objectResult = client.putObject(new PutObjectRequest(bucket, objectKey, input, new ObjectMetadata())); |
| | | log.info("Upload File: {}", objectResult.toString()); |
| | | } |
| | | |
| | | public void createClient() { |
| | | if (Objects.nonNull(this.client)) { |
| | | return; |
| | | } |
| | | this.client = AmazonS3ClientBuilder.standard() |
| | | .withCredentials( |
| | | new AWSStaticCredentialsProvider( |
| | | new BasicAWSCredentials(configuration.getAccessKey(), configuration.getSecretKey()))) |
| | | .withRegion(configuration.getRegion()) |
| | | new BasicAWSCredentials(OssConfiguration.accessKey, OssConfiguration.secretKey))) |
| | | .withRegion(OssConfiguration.region) |
| | | .build(); |
| | | } |
| | | |
| | |
| | | */ |
| | | @PostConstruct |
| | | private void configCORS() { |
| | | if (!configuration.isEnable() || !OssTypeEnum.AWS.getType().equals(configuration.getProvider())) { |
| | | if (!OssConfiguration.enable || !OssTypeEnum.AWS.getType().equals(OssConfiguration.provider)) { |
| | | return; |
| | | } |
| | | List<CORSRule.AllowedMethods> allowedMethods = new ArrayList<>(); |
| | |
| | | .withAllowedHeaders(List.of(AuthInterceptor.PARAM_TOKEN)) |
| | | .withAllowedMethods(allowedMethods); |
| | | |
| | | AmazonS3 client = this.createClient(); |
| | | |
| | | client.setBucketCrossOriginConfiguration(this.configuration.getBucket(), |
| | | client.setBucketCrossOriginConfiguration(OssConfiguration.bucket, |
| | | new BucketCrossOriginConfiguration().withRules(rule)); |
| | | client.shutdown(); |
| | | |
| | | } |
| | | } |