| | |
| | | package com.dji.sample.component.oss.service.impl; |
| | | |
| | | import com.dji.sample.component.oss.model.MinIOConfiguration; |
| | | 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 io.minio.GetPresignedObjectUrlArgs; |
| | | import io.minio.MinioClient; |
| | | import io.minio.*; |
| | | import io.minio.credentials.AssumeRoleProvider; |
| | | import io.minio.errors.*; |
| | | import io.minio.http.Method; |
| | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.net.URL; |
| | | import java.security.InvalidKeyException; |
| | | import java.security.NoSuchAlgorithmException; |
| | | import java.util.Objects; |
| | | |
| | | /** |
| | | * @author sean |
| | |
| | | @Slf4j |
| | | public class MinIOServiceImpl implements IOssService { |
| | | |
| | | @Autowired(required = false) |
| | | private MinioClient client; |
| | | |
| | | @Autowired |
| | | private OssConfiguration configuration; |
| | | |
| | | @Override |
| | | public String getOssType() { |
| | | return OssTypeEnum.MINIO.getType(); |
| | | } |
| | | |
| | | @Override |
| | | public CredentialsDTO getCredentials() { |
| | | try { |
| | | AssumeRoleProvider provider = new AssumeRoleProvider(MinIOConfiguration.endpoint, MinIOConfiguration.accessKey, |
| | | MinIOConfiguration.secretKey, MinIOConfiguration.expire, |
| | | null, null, null, null, null, null); |
| | | return new CredentialsDTO(provider.fetch(), MinIOConfiguration.expire); |
| | | AssumeRoleProvider provider = new AssumeRoleProvider(configuration.getEndpoint(), configuration.getAccessKey(), |
| | | configuration.getSecretKey(), Math.toIntExact(configuration.getExpire()), |
| | | null, configuration.getRegion(), null, null, null, null); |
| | | return new CredentialsDTO(provider.fetch(), configuration.getExpire()); |
| | | } catch (NoSuchAlgorithmException e) { |
| | | log.debug("Failed to obtain sts."); |
| | | e.printStackTrace(); |
| | |
| | | public URL getObjectUrl(String bucket, String objectKey) { |
| | | try { |
| | | return new URL( |
| | | client.getPresignedObjectUrl( |
| | | GetPresignedObjectUrlArgs.builder() |
| | | .method(Method.GET) |
| | | .bucket(bucket) |
| | | .object(objectKey) |
| | | .expiry(MinIOConfiguration.expire) |
| | | .build())); |
| | | this.createClient() |
| | | .getPresignedObjectUrl( |
| | | GetPresignedObjectUrlArgs.builder() |
| | | .method(Method.GET) |
| | | .bucket(bucket) |
| | | .object(objectKey) |
| | | .expiry(Math.toIntExact(configuration.getExpire())) |
| | | .build())); |
| | | } catch (ErrorResponseException | InsufficientDataException | InternalException | |
| | | InvalidKeyException | InvalidResponseException | IOException | |
| | | NoSuchAlgorithmException | XmlParserException | ServerException e) { |
| | | log.error("The file does not exist on the oss."); |
| | | log.error("The file does not exist on the OssConfiguration."); |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | @Override |
| | | public Boolean deleteObject(String bucket, String objectKey) { |
| | | MinioClient client = this.createClient(); |
| | | try { |
| | | client.removeObject(RemoveObjectArgs.builder().bucket(bucket).object(objectKey).build()); |
| | | } catch (MinioException | NoSuchAlgorithmException | IOException | InvalidKeyException e) { |
| | | log.error("Failed to delete file."); |
| | | e.printStackTrace(); |
| | | return false; |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | @Override |
| | | public InputStream getObject(String bucket, String objectKey) { |
| | | try { |
| | | GetObjectResponse object = this.createClient().getObject(GetObjectArgs.builder().bucket(bucket).object(objectKey).build()); |
| | | return new ByteArrayInputStream(object.readAllBytes()); |
| | | } catch (ErrorResponseException | InsufficientDataException | InternalException | InvalidKeyException | InvalidResponseException | IOException | NoSuchAlgorithmException | ServerException | XmlParserException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return InputStream.nullInputStream(); |
| | | } |
| | | |
| | | @Override |
| | | public void putObject(String bucket, String objectKey, InputStream input) { |
| | | try { |
| | | MinioClient client = this.createClient(); |
| | | client.statObject(StatObjectArgs.builder().bucket(bucket).object(objectKey).build()); |
| | | throw new RuntimeException("The filename already exists."); |
| | | } catch (MinioException | InvalidKeyException | IOException | NoSuchAlgorithmException e) { |
| | | log.info("The file does not exist, start uploading."); |
| | | try { |
| | | ObjectWriteResponse response = client.putObject( |
| | | PutObjectArgs.builder().bucket(bucket).object(objectKey).stream(input, input.available(), 0).build()); |
| | | log.info("Upload File: {}", response.etag()); |
| | | } catch (MinioException | IOException | InvalidKeyException | NoSuchAlgorithmException ex) { |
| | | log.error("Failed to upload File {}.", objectKey); |
| | | ex.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | private MinioClient createClient() { |
| | | if (Objects.nonNull(this.client)) { |
| | | return this.client; |
| | | } |
| | | this.client = MinioClient.builder() |
| | | .endpoint(configuration.getEndpoint()) |
| | | .credentials(configuration.getAccessKey(), configuration.getSecretKey()) |
| | | .region(configuration.getRegion()) |
| | | .build(); |
| | | return this.client; |
| | | } |
| | | } |