【问题标题】:How to upload file to AWS bucket with AWS Java SDK 2如何使用 AWS Java SDK 2 将文件上传到 AWS 存储桶
【发布时间】:2021-05-05 02:07:57
【问题描述】:

AWS Java SDK 2.15.73(当前最新版本)。 Java 1.8.222 操作系统 IBM AIX 7.1

开发将文件上传到 S3 存储桶的应用程序。

在 Maven pom 文件中:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>bom</artifactId>
            <version>2.15.73</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>software.amazon.awssdk</groupId>
        <artifactId>s3</artifactId>
    </dependency>
    <dependency>
      <groupId>software.amazon.awssdk</groupId>
      <artifactId>apache-client</artifactId>
      <version>2.0.0-preview-10</version>
    </dependency>           
    <dependency>
      <groupId>software.amazon.awssdk</groupId>
      <artifactId>http-client-spi</artifactId>
      <version>2.0.0-preview-10</version>
    </dependency>           
</dependencies>

Stacktrace 的开始:

sendFileToAWSNoAuth 中的异常=无法将请求编组到 JSON:baseUri 不能为空。 software.amazon.awssdk.core.exception.SdkClientException:无法将请求编组到 JSON:baseUri 不能为空。 在 software.amazon.awssdk.core.exception.SdkClientException$BuilderImpl.build(SdkClientException.java:98) 在 software.amazon.awssdk.services.s3.transform.PutObjectRequestMarshaller.marshall(PutObjectRequestMarshaller.java:53) 在 software.amazon.awssdk.services.s3.transform.PutObjectRequestMarshaller.marshall(PutObjectRequestMarshaller.java:31) 在 software.amazon.awssdk.core.runtime.transform.StreamingRequestMarshaller.marshall(StreamingRequestMarshaller.java:48)

Java 代码:

需要身份验证并通过代理服务器执行。

字符串 endPointURL = appParms.getProxyServer() + ":" + appParms.getProxyServerPort();

URI endPointUri = 新的 URI(endPointURL);

ProxyConfiguration proxyConfig = ProxyConfiguration.builder() .endpoint(endPointUri) .build();

SdkHttpClient httpClient = ApacheSdkHttpClientFactory.builder() .proxyConfiguration(proxyConfig) 。建造() .createHttpClient();

ProfileCredentialsProvider credentialsProfile = ProfileCredentialsProvider.builder().profileName(appParms.getProfileName()).build();

区域区域 = Region.EU_CENTRAL_1; S3Client s3NonEncryption = S3Client.builder() .credentialsProvider(credentialsProfile) .region(地区) .endpointOverride(endPointUri) .httpClient(httpClient) .build();

String folderKey = appParms.getBucketFolder() + "/" + appParms.getFileName();

文件 aFile = new File(appParms.getFilePath() + "/" + appParms.getFileName());

PutObjectRequest putOb = PutObjectRequest.builder() .bucket(appParms.getBucketNameSSE()) .key(文件夹键) .build();

PutObjectResponse 响应 = s3NonEncryption.putObject(putOb, aFile.toPath());

最后一行发生异常 (s3NonEncryption.putObject)

【问题讨论】:

    标签: amazon-s3


    【解决方案1】:

    此“基本 URI 不得为空”错误是由 S3Client.builder 中的“endpointOverride(endPointUri)”引起的。 ..

    在 http 客户端创建中也使用了类 ApacheSdkHttpClientFactory,这会导致运行中的各种类不匹配错误。

    正确的方法是使用类 ApacheHttpClient:

    SdkHttpClient httpClient = ApacheHttpClient.builder() .proxyConfiguration(proxyConfig) .connectionTimeout(Duration.ofMillis(30000)) .build();

    执行中还是有超时错误。

    software.amazon.awssdk.core.exception.SdkClientException:无法执行 HTTP 请求:连接到 bdw-landing-sse-test-s3.s3.eu-central-1.amazonaws.com:443 [bdw-landing -sse-test-s3.s3.eu-central-1.amazonaws.com/52.219.72.81] 失败:连接超时 在 software.amazon.awssdk.core.exception.SdkClientException$BuilderImpl.build(SdkClientException.java:98) 在 software.amazon.awssdk.core.exception.SdkClientException.create(SdkClientException.java:43) 在 software.amazon.awssdk.core.internal.http.pipeline.stages.utils.RetryableStageHelper.setLastException(RetryableStageHelper.java:198)

    【讨论】:

      猜你喜欢
      • 2020-03-17
      • 2017-09-24
      • 2019-04-28
      • 2012-05-28
      • 2018-04-25
      • 2021-07-17
      • 1970-01-01
      • 2014-09-11
      • 2022-08-16
      相关资源
      最近更新 更多