package com.dji.sample.component.oss.service.impl; 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.*; import io.minio.credentials.AssumeRoleProvider; import io.minio.errors.*; import io.minio.http.Method; import lombok.extern.slf4j.Slf4j; 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 * @version 0.3 * @date 2021/12/23 */ @Service @Slf4j public class MinIOServiceImpl implements IOssService { private MinioClient client; @Override public String getOssType() { return OssTypeEnum.MINIO.getType(); } @Override public CredentialsDTO getCredentials() { try { AssumeRoleProvider provider = new AssumeRoleProvider(OssConfiguration.externalEndpoint, OssConfiguration.accessKey, OssConfiguration.secretKey, Math.toIntExact(OssConfiguration.expire), null, OssConfiguration.region, null, null, null, null); return new CredentialsDTO(provider.fetch(), OssConfiguration.expire); } catch (NoSuchAlgorithmException e) { log.debug("Failed to obtain sts."); e.printStackTrace(); } return null; } @Override public URL getObjectUrl(String bucket, String objectKey) { try { return new URL( client.getPresignedObjectUrl( GetPresignedObjectUrlArgs.builder() .method(Method.GET) .bucket(bucket) .object(objectKey) .expiry(Math.toIntExact(OssConfiguration.expire)) .build())); } catch (ErrorResponseException | InsufficientDataException | InternalException | InvalidKeyException | InvalidResponseException | IOException | NoSuchAlgorithmException | XmlParserException | ServerException e) { throw new RuntimeException("OssConfiguration上不存在该文件."); } } @Override public Boolean deleteObject(String bucket, String objectKey) { 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 = client.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 { client.statObject(StatObjectArgs.builder().bucket(bucket).object(objectKey).build()); throw new RuntimeException("文件名已存在"); } catch (MinioException | InvalidKeyException | IOException | NoSuchAlgorithmException e) { log.info("文件不存在,开始上传。"); 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(); } } } public void createClient() { if (Objects.nonNull(this.client)) { return; } this.client = MinioClient.builder() .endpoint(OssConfiguration.endpoint) .credentials(OssConfiguration.accessKey, OssConfiguration.secretKey) .region(OssConfiguration.region) .build(); } }