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
| public class FileUploader { public static void main(String[] args) throws NoSuchAlgorithmException, IOException, InvalidKeyException { try { MinioClient minioClient = MinioClient.builder() .httpClient(getUnsafeOkHttpsClient()) .credentials("jGtLBvw6HhnW9Nsc", "BzDmbL3ZCPERFHPxB5jOnf0w7l48DqOu") .endpoint("192.168.100.100", 31582, 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; } }
|