场景描述
用户使用对象存储(原生版)I型(简称ZOS)服务时,客户端所在的网络环境受防火墙策略或其他因素影响需要通过代理访问外网的对象存储服务。不同环境的操作系统设置代理的方式不同,且容器访问时仍然需要单独配置代理。
本文重点介绍如何在使用SDK请求时,配置代理进行对象存储的访问。
使用介绍
以java 语言 SDK 为例。 通过代理服务器连接到外网的对象存储服务,需要通过 ClientConfiguration 对象配置代理服务器设置。并且在构造客户端对象时,传入修改后的 ClientConfiguration 来自定义客户端的配置。 常用的设置配置如下:
setProxyHost(String proxyHost) 设置代理主机
setProxyPort(int proxyPort) 设置代理主机的端口
setProxyUsername(String proxyUsername) 设置代理验证的用户名
setProxyPassword(String proxyPassword) 设置代理验证的密码
代码示例
public AmazonS3 getZosClient() {
if (zosClient == null) {
synchronized (DefaultS3Client.class) {
if (zosClient == null) {
BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
ClientConfiguration clientConfiguration = new ClientConfiguration();
clientConfiguration.withSignerOverride("S3SignerType");
clientConfiguration.setMaxErrorRetry(2);
//设置代理主机
clientConfiguration.setProxyHost(proxyHost);
//设置代理主机的端口
clientConfiguration.setProxyPort(proxyPort);
//设置代理验证的用户名
clientConfiguration.setProxyUsername(proxyUsername);
//设置代理验证的密码
clientConfiguration.setProxyPassword(proxyPassword);
AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(
endpoint, Regions.DEFAULT_REGION.getName());
zosClient = AmazonS3ClientBuilder.standard()
//客户端设置
.withClientConfiguration(clientConfiguration)
//凭证设置
.withCredentials(new AWSStaticCredentialsProvider(credentials))
//endpoint设置
.withEndpointConfiguration(endpointConfiguration)
//path style
.enablePathStyleAccess()
.build();
}
}
}
return zosClient;
}