gpt4 book ai didi

java - 无法使用 HTTPS 使用 AWS S3 Java API 访问 S3 上的存储桶

转载 作者:行者123 更新时间:2023-12-02 03:32:44 25 4
gpt4 key购买 nike

尝试访问我们的本地 S3 存储时,我首先遇到了证书丢失的问题:

Exception in thread "main" com.amazonaws.SdkClientException: Unable to execute HTTP request: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

关闭证书检查:

System.setProperty(SDKGlobalConfiguration.DISABLE_CERT_CHECKING_SYSTEM_PROPERTY, "true");

我成功“连接”到服务器。但是使用下面的代码不会产生任何结果(也称为空列表)。到目前为止非常简单。

端点和存储桶名称都应该正确,因为我从我的(工作)S3 浏览器中剪切并粘贴了它们。

凭据似乎也是正确的,因为当我弄乱这些凭据时,我正在运行 404。再次 - 从 y S3 兄弟复制那些。

使用 S3 浏览器,我可以访问存储桶、添加文件等。

运行时 List<Bucket> buckets = s3Client.listBuckets();它只是返回一个空列表 - 没有任何异常(exception)。

控制台输出为:

Juli 03, 2019 8:48:07 NACHM. com.amazonaws.http.AmazonHttpClient createSocketFactoryRegistry WARNUNG: SSL Certificate checking for endpoints has been explicitly disabled. Juli 03, 2019 8:48:08 NACHM. com.amazonaws.auth.profile.internal.BasicProfileConfigLoader loadProfiles WARNUNG: Your profile name includes a 'profile ' prefix. This is considered part of the profile name in the Java SDK, so you will need to include this prefix in your profile name when you reference this profile from your Java code. Juli 03, 2019 8:48:08 NACHM. com.amazonaws.auth.profile.internal.BasicProfileConfigLoader loadProfiles WARNUNG: Your profile name includes a 'profile ' prefix. This is considered part of the profile name in the Java SDK, so you will need to include this prefix in your profile name when you reference this profile from your Java code. Juli 03, 2019 8:48:23 NACHM. com.amazonaws.http.AmazonHttpClient createSocketFactoryRegistry WARNUNG: SSL Certificate checking for endpoints has been explicitly disabled.

有什么建议吗?

System.setProperty(SDKGlobalConfiguration.DISABLE_CERT_CHECKING_SYSTEM_PROPERTY, "true");
String accessKey = "myaccess";
String secretKey = "mysecret";

AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
ClientConfiguration clientConfig = new ClientConfiguration();
clientConfig.setProtocol(Protocol.HTTPS);

EndpointConfiguration endpointConfig = new EndpointConfiguration("endpoint.com", Regions.EU_CENTRAL_1.getName());
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentials))
//.withRegion(defaultRegion)
.withEndpointConfiguration(endpointConfig)
.build();

List<Bucket> buckets = s3Client.listBuckets();
for (Bucket bucket : buckets) {
System.out.println(
bucket.getName() +
"\t" +
StringUtils.fromDate(bucket.getCreationDate())
);
}

运行时 List<Bucket> buckets = s3Client.listBuckets();它只是返回一个空列表 - 没有任何异常(exception)。

此存储桶的根目录中有(根据 S3 浏览器)2 个文件。

控制台输出为:

Juli 03, 2019 8:48:07 NACHM. com.amazonaws.http.AmazonHttpClient createSocketFactoryRegistry WARNUNG: SSL Certificate checking for endpoints has been explicitly disabled. Juli 03, 2019 8:48:08 NACHM. com.amazonaws.auth.profile.internal.BasicProfileConfigLoader loadProfiles WARNUNG: Your profile name includes a 'profile ' prefix. This is considered part of the profile name in the Java SDK, so you will need to include this prefix in your profile name when you reference this profile from your Java code. Juli 03, 2019 8:48:08 NACHM. com.amazonaws.auth.profile.internal.BasicProfileConfigLoader loadProfiles WARNUNG: Your profile name includes a 'profile ' prefix. This is considered part of the profile name in the Java SDK, so you will need to include this prefix in your profile name when you reference this profile from your Java code. Juli 03, 2019 8:48:23 NACHM. com.amazonaws.http.AmazonHttpClient createSocketFactoryRegistry WARNUNG: SSL Certificate checking for endpoints has been explicitly disabled.

有什么建议吗?

最佳答案

所以我终于回答自己了。

使用

禁用端点验证
System.setProperty(SDKGlobalConfiguration.DISABLE_CERT_CHECKING_SYSTEM_PROPERTY, "true");

就像一个魅力。该问题有两个原因:

  1. 我的账户没有列出存储桶的权限(不知道为什么,访问 AWS S3 时不会发生这种情况)。
  2. 我没有正确提供存储桶名称,因为我必须在存储桶名称中添加前导“/”。

第 1 点可以通过访问对象列表来解决:

ObjectListing objects = conn.listObjects("/my_bucket_name");

第 2 点显然可以通过在存储桶名称中添加前导“/”来解决。

干杯,

奥利

关于java - 无法使用 HTTPS 使用 AWS S3 Java API 访问 S3 上的存储桶,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56876379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com