| | |
| | | package com.dji.sample.component.oss.service.impl; |
| | | |
| | | import com.aliyun.oss.OSS; |
| | | import com.aliyun.oss.OSSClientBuilder; |
| | | import com.aliyun.oss.OSSException; |
| | | import com.aliyun.oss.model.ObjectMetadata; |
| | | import com.aliyun.oss.model.PutObjectRequest; |
| | | import com.aliyun.oss.model.PutObjectResult; |
| | | import com.aliyuncs.DefaultAcsClient; |
| | | import com.aliyuncs.IAcsClient; |
| | | import com.aliyuncs.exceptions.ClientException; |
| | | import com.aliyuncs.profile.DefaultProfile; |
| | | import com.aliyuncs.sts.model.v20150401.AssumeRoleRequest; |
| | | import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse; |
| | | import com.dji.sample.component.oss.model.AliyunOSSConfiguration; |
| | | import com.dji.sample.component.oss.model.OssConfiguration; |
| | | 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; |
| | | import org.springframework.util.StringUtils; |
| | | |
| | | import java.io.InputStream; |
| | | import java.net.URL; |
| | | import java.util.Date; |
| | | import java.util.Objects; |
| | | |
| | | /** |
| | | * @author sean |
| | |
| | | @Slf4j |
| | | public class AliyunOssServiceImpl implements IOssService { |
| | | |
| | | @Autowired |
| | | private OSS ossClient; |
| | | |
| | | @Override |
| | | public String getOssType() { |
| | | return OssTypeEnum.ALIYUN.getType(); |
| | | } |
| | | |
| | | @Override |
| | | public CredentialsDTO getCredentials() { |
| | | |
| | | try { |
| | | DefaultProfile profile = DefaultProfile.getProfile( |
| | | AliyunOSSConfiguration.region, AliyunOSSConfiguration.accessKey, AliyunOSSConfiguration.secretKey); |
| | | OssConfiguration.region, OssConfiguration.accessKey, OssConfiguration.secretKey); |
| | | IAcsClient client = new DefaultAcsClient(profile); |
| | | |
| | | AssumeRoleRequest request = new AssumeRoleRequest(); |
| | | request.setDurationSeconds(AliyunOSSConfiguration.expire); |
| | | request.setRoleArn(AliyunOSSConfiguration.roleArn); |
| | | request.setRoleSessionName(AliyunOSSConfiguration.roleSessionName); |
| | | request.setDurationSeconds(OssConfiguration.expire); |
| | | request.setRoleArn(OssConfiguration.roleArn); |
| | | request.setRoleSessionName(OssConfiguration.roleSessionName); |
| | | |
| | | AssumeRoleResponse response = client.getAcsResponse(request); |
| | | return new CredentialsDTO(response.getCredentials(), AliyunOSSConfiguration.expire); |
| | | return new CredentialsDTO(response.getCredentials(), OssConfiguration.expire); |
| | | |
| | | } catch (ClientException e) { |
| | | log.debug("Failed to obtain sts."); |
| | |
| | | |
| | | @Override |
| | | public URL getObjectUrl(String bucket, String objectKey) { |
| | | if (!StringUtils.hasText(bucket) || !StringUtils.hasText(objectKey)) { |
| | | return null; |
| | | } |
| | | // First check if the object can be fetched. |
| | | ossClient.getObject(bucket, objectKey); |
| | | boolean isExist = ossClient.doesObjectExist(bucket, objectKey); |
| | | if (!isExist) { |
| | | throw new OSSException("对象不存在"); |
| | | } |
| | | |
| | | return ossClient.generatePresignedUrl(bucket, objectKey, |
| | | new Date(System.currentTimeMillis() + AliyunOSSConfiguration.expire * 1000)); |
| | | new Date(System.currentTimeMillis() + OssConfiguration.expire * 1000)); |
| | | } |
| | | |
| | | @Override |
| | | public Boolean deleteObject(String bucket, String objectKey) { |
| | | if (!ossClient.doesObjectExist(bucket, objectKey)) { |
| | | return true; |
| | | } |
| | | ossClient.deleteObject(bucket, objectKey); |
| | | return true; |
| | | } |
| | | |
| | | @Override |
| | | public InputStream getObject(String bucket, String objectKey) { |
| | | return ossClient.getObject(bucket, objectKey).getObjectContent(); |
| | | } |
| | | |
| | | @Override |
| | | public void putObject(String bucket, String objectKey, InputStream input) { |
| | | if (ossClient.doesObjectExist(bucket, objectKey)) { |
| | | throw new RuntimeException("文件已存在"); |
| | | } |
| | | PutObjectResult objectResult = ossClient.putObject(new PutObjectRequest(bucket, objectKey, input, new ObjectMetadata())); |
| | | log.info("Upload File: {}", objectResult.getETag()); |
| | | } |
| | | |
| | | public void createClient() { |
| | | if (Objects.nonNull(this.ossClient)) { |
| | | return; |
| | | } |
| | | this.ossClient = new OSSClientBuilder() |
| | | .build(OssConfiguration.endpoint, OssConfiguration.accessKey, OssConfiguration.secretKey); |
| | | } |
| | | } |