Java环境下AmazonS3客户端如何配置自定义endpoint端点
AWS S3 Java SDK 配置私有云S3存储Endpoint方法
你可以通过withEndpointConfiguration方法指定自定义私有云存储地址,同时需要开启路径风格访问配置,适配MinIO等私有S3存储的访问规则,完整代码如下:
import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.client.builder.AwsClientBuilder; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; // 初始化访问凭证 BasicAWSCredentials credentials = new BasicAWSCredentials("1234567", "7654321"); // 构造自定义Endpoint配置,第二个参数为区域标识,私有存储场景可直接沿用原有值即可 AwsClientBuilder.EndpointConfiguration endpointConfig = new AwsClientBuilder.EndpointConfiguration("https://bla-bla-bla.company.com:9000", "us-west-2"); AmazonS3 s3Client = AmazonS3ClientBuilder .standard() .withCredentials(new AWSStaticCredentialsProvider(credentials)) // 绑定自定义私有云存储地址 .withEndpointConfiguration(endpointConfig) // 必须开启路径风格访问,否则SDK会默认将bucket名作为子域名解析,导致访问私有存储失败 .withPathStyleAccessEnabled(true) .build();
注意事项
- 配置了
withEndpointConfiguration后不需要再单独调用withRegion方法,重复配置区域参数会抛出冲突异常 - 路径风格访问配置为必填项,除非你司运维团队搭建的私有存储明确告知已开启虚拟主机模式的bucket访问
内容的提问来源于stack exchange,提问作者user2611714
相关产品推荐
相关产品推荐

