java采用https连接minio
时间: 2023-06-28 07:09:27 浏览: 109
java连接minio工具类
可以使用Java的HTTP客户端库,例如Apache HttpClient或OkHttp,来与MinIO服务器建立https连接。以下是使用Apache HttpClient库连接MinIO的示例代码:
```java
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLContext;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
public class MinioExample {
public static void main(String[] args) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException {
// Create SSL context with default truststore
SSLContext sslContext = SSLContexts.createDefault();
// Create SSL socket factory with the SSL context and a hostname verifier that ignores the hostname
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
// Create HTTP client with the SSL socket factory
HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslSocketFactory).build();
// Create GET request to MinIO server
HttpGet httpGet = new HttpGet("https://minio.example.com/bucket/object");
// Execute request and get response
HttpResponse response = httpClient.execute(httpGet);
// Get response entity and print it
HttpEntity entity = response.getEntity();
System.out.println(EntityUtils.toString(entity));
}
}
```
请替换示例代码中的MinIO服务器和对象的URL。此代码使用默认的信任存储库来验证服务器证书,因此需要确保服务器证书已正确安装。如果需要使用自定义信任存储库或自签名证书,请参考Java SSL文档和相关库的文档。
阅读全文