| | |
| | | import java.net.URL; |
| | | import java.security.InvalidKeyException; |
| | | import java.security.NoSuchAlgorithmException; |
| | | import java.security.ProviderException; |
| | | import java.security.SecureRandom; |
| | | import java.security.cert.X509Certificate; |
| | | import java.util.Objects; |
| | |
| | | return OssTypeEnum.MINIO.getType(); |
| | | } |
| | | |
| | | @Override |
| | | public CredentialsDTO getCredentials() { |
| | | |
| | | public CredentialsDTO getCredentials1111() { |
| | | try { |
| | | AssumeRoleProvider provider = new AssumeRoleProvider(endpoint, accessKey, |
| | | secretKey, Math.toIntExact(OssConfiguration.expire), |
| | |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | @Override |
| | | public CredentialsDTO getCredentials() { |
| | | try { |
| | | // 创建自签证书的 OkHttpClient |
| | | TrustManager[] trustAllCerts = new TrustManager[]{ |
| | | new X509TrustManager() { |
| | | @Override |
| | | public void checkClientTrusted(X509Certificate[] chain, String authType) {} |
| | | @Override |
| | | public void checkServerTrusted(X509Certificate[] chain, String authType) {} |
| | | @Override |
| | | public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[]{}; } |
| | | } |
| | | }; |
| | | SSLContext sslContext = SSLContext.getInstance("TLS"); |
| | | sslContext.init(null, trustAllCerts, new SecureRandom()); |
| | | OkHttpClient httpClient = new OkHttpClient.Builder() |
| | | .sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) trustAllCerts[0]) |
| | | .hostnameVerifier((hostname, session) -> true) |
| | | .build(); |
| | | |
| | | // 构建 AssumeRoleProvider,使用自定义 httpClient |
| | | AssumeRoleProvider provider = new AssumeRoleProvider( |
| | | endpoint, |
| | | accessKey, |
| | | secretKey, |
| | | Math.toIntExact(OssConfiguration.expire), |
| | | null, |
| | | OssConfiguration.region, |
| | | null, |
| | | null, |
| | | null, |
| | | httpClient |
| | | ); |
| | | |
| | | // 获取 STS 凭证 |
| | | var stsCredentials = provider.fetch(); |
| | | return new CredentialsDTO(stsCredentials, OssConfiguration.expire); |
| | | |
| | | } catch (ProviderException e) { |
| | | log.error("Failed to parse STS response", e); |
| | | throw new RuntimeException("STS response parse failed", e); |
| | | } catch (NoSuchAlgorithmException | java.security.KeyManagementException e) { |
| | | log.error("Failed to create custom SSL context", e); |
| | | throw new RuntimeException("SSL setup failed", e); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | |
| | | } |
| | | } |
| | | @Override |
| | | public void putObject(String bucket, String objectKey, InputStream input, String contentType) { |
| | | try { |
| | | // 检查文件是否已经存在 |
| | | client.statObject(StatObjectArgs.builder().bucket(bucket).object(objectKey).build()); |
| | | throw new RuntimeException("The file already exists: " + objectKey); |
| | | } catch (ErrorResponseException e) { |
| | | // 如果文件不存在,则继续上传 |
| | | log.info("The file does not exist, start uploading."); |
| | | try { |
| | | // 设置 partSize 为 10MB |
| | | long partSize = 10 * 1024 * 1024; // 每块 10MB |
| | | PutObjectArgs.Builder putArgsBuilder = PutObjectArgs.builder() |
| | | .bucket(bucket) |
| | | .object(objectKey) |
| | | .stream(input, -1, partSize); |
| | | |
| | | // 如果提供了 Content-Type,则设置该类型 |
| | | if (contentType != null && !contentType.isEmpty()) { |
| | | putArgsBuilder.contentType(contentType); |
| | | } |
| | | |
| | | // 上传文件 |
| | | ObjectWriteResponse response = client.putObject(putArgsBuilder.build()); |
| | | log.info("Uploaded file successfully: {}, ETag: {}", objectKey, response.etag()); |
| | | |
| | | } catch (MinioException | IOException | InvalidKeyException | NoSuchAlgorithmException ex) { |
| | | log.error("Failed to upload file: {}", objectKey, ex); |
| | | throw new RuntimeException("File upload failed: " + objectKey, ex); |
| | | } |
| | | } catch (MinioException | InvalidKeyException | IOException | NoSuchAlgorithmException e) { |
| | | // 如果 statObject 发生错误,说明服务端错误或其他问题 |
| | | log.error("Failed to check if file exists: {}", objectKey, e); |
| | | throw new RuntimeException("Failed to check if file exists: " + objectKey, e); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void createClient() { |
| | | if (Objects.nonNull(this.client)) { |
| | | return; |