1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| package com.example.bootminio.upload;
import io.minio.BucketExistsArgs; import io.minio.MakeBucketArgs; import io.minio.MinioClient; import io.minio.PutObjectArgs; import io.minio.errors.MinioException; import okhttp3.OkHttpClient;
import javax.net.ssl.*; import java.io.FileInputStream; import java.io.IOException; import java.security.InvalidKeyException; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.cert.CertificateException; import java.security.cert.X509Certificate;
public class FileUploader { public static void main(String[] args) throws NoSuchAlgorithmException, IOException, InvalidKeyException { try { MinioClient minioClient = MinioClient.builder() .httpClient(getUnsafeOkHttpsClient()) .credentials("K8sgt80o0TzhWRGg", "t7l2L6l3pVptLEWsfdZa4iaxadptD2aB") .endpoint("192.168.66.31", 9000, false) .build();
boolean isExist = minioClient.bucketExists(BucketExistsArgs.builder().bucket("test").build()); if (isExist) { System.out.println("Bucket already exists."); } else { minioClient.makeBucket(MakeBucketArgs.builder().bucket("test").build()); } FileInputStream fis = new FileInputStream("./pom.xml"); minioClient.putObject(PutObjectArgs.builder() .bucket("test") .object("pom.xml") .stream(fis, fis.available(), -1) .build()); System.out.println("pom.xml is successfully uploaded as pom.xml to `test` bucket."); } catch (MinioException e) { System.out.println("Error occurred: " + e); } catch (KeyManagementException e) { throw new RuntimeException(e); } }
public static OkHttpClient getUnsafeOkHttpsClient() throws KeyManagementException { try { final TrustManager[] trustAllCerts = new TrustManager[]{ new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[]{}; } } };
final SSLContext sslContext = SSLContext.getInstance("SSL"); sslContext.init(null, trustAllCerts, new SecureRandom()); final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); OkHttpClient.Builder builder = new OkHttpClient.Builder(); builder.sslSocketFactory(sslSocketFactory,new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[]{}; } });
builder.hostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String s, SSLSession sslSession) { return true; } }); return builder.build();
} catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return null; } }
|