| | |
| | | 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 lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | |
| | | * @version 1.0 |
| | | * @date 2022/4/27 |
| | | */ |
| | | @Slf4j |
| | | @Service |
| | | public class AmazonS3ServiceImpl implements IOssService { |
| | | |
| | |
| | | @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) { |
| | | public InputStream getObject(String bucket, String objectKey) { |
| | | AmazonS3 client = this.createClient(); |
| | | S3Object object = client.getObject(bucket, objectKey); |
| | | InputStream stream = object.getObjectContent().getDelegateStream(); |
| | | try { |
| | | return stream.readAllBytes(); |
| | | try (InputStream input = object.getObjectContent().getDelegateStream()) { |
| | | return input; |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | | try { |
| | | stream.close(); |
| | | client.shutdown(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | client.shutdown(); |
| | | } |
| | | return new byte[0]; |
| | | return InputStream.nullInputStream(); |
| | | } |
| | | |
| | | @Override |
| | | public void putObject(String bucket, String objectKey, InputStream input) { |
| | | AmazonS3 client = this.createClient(); |
| | | if (client.doesObjectExist(bucket, objectKey)) { |
| | | client.shutdown(); |
| | | throw new RuntimeException("The filename already exists."); |
| | | } |
| | | PutObjectResult objectResult = client.putObject(new PutObjectRequest(bucket, objectKey, input, new ObjectMetadata())); |
| | | client.shutdown(); |
| | | log.info("Upload File: {}", objectResult.toString()); |
| | | } |
| | | |
| | | private AmazonS3 createClient() { |