当前位置: 首页 > 知识库问答 >
问题:

S3Exception with AWS SDK for Java Version2-我们计算的请求签名与您提供的签名不匹配

束福
2023-03-14

我试图使用AWS SDK for Java Version2获取S3 bucket中的对象列表。获取以下异常:

software.amazon.awssdk.services.s3.model.S3Exception: 
The request signature we calculated does not match the signature you provided.
Check your key and signing method. (Service: S3, Status Code: 403, Request ID: DCE23DE2BC35653C)

我的代码片段:

import software.amazon.awssdk.auth.credentials.AwsSessionCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
....
private software.amazon.awssdk.services.s3.S3Client getS3Client(Region region) {
    //Getting values from properties file -- AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN
    AwsSessionCredentials awsCreds =
        AwsSessionCredentials.create(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN);
    S3Client s3 =
        S3Client.builder()
            .region(region)
            .credentialsProvider(StaticCredentialsProvider.create(awsCreds))
            .build();
    return s3;
}

public void listObjects(Region region, String bucketName) {
    S3Client s3 = getS3Client(region);
    ListObjectsRequest listObjects = ListObjectsRequest
          .builder()
          .bucket(bucketName)
          .build();
    ListObjectsResponse res = s3.listObjects(listObjects); // This throws S3Exception
    List<S3Object> objects = res.contents();
    ....
    //Iterate over 'objects' and do something
}
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>software.amazon.awssdk</groupId>
        <artifactId>bom</artifactId>
        <version>2.7.5</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
...
    <dependency>
      <groupId>software.amazon.awssdk</groupId>
      <artifactId>s3</artifactId>
    </dependency>
software.amazon.awssdk.services.s3.model.S3Exception: The request signature we calculated does not match the signature you provided. Check your key and signing method. (Service: S3, Status Code: 403, Request ID: DCE23DE2BC35653C)
software.amazon.awssdk.core.internal.http.pipeline.stages.HandleResponseStage.handleErrorResponse(HandleResponseStage.java:115)
software.amazon.awssdk.core.internal.http.pipeline.stages.HandleResponseStage.handleResponse(HandleResponseStage.java:73)
software.amazon.awssdk.core.internal.http.pipeline.stages.HandleResponseStage.execute(HandleResponseStage.java:58)
software.amazon.awssdk.core.internal.http.pipeline.stages.HandleResponseStage.execute(HandleResponseStage.java:41)
software.amazon.awssdk.core.internal.http.pipeline.RequestPipelineBuilder$ComposingRequestPipelineStage.execute(RequestPipelineBuilder.java:206)
software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallAttemptTimeoutTrackingStage.execute(ApiCallAttemptTimeoutTrackingStage.java:64)
software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallAttemptTimeoutTrackingStage.execute(ApiCallAttemptTimeoutTrackingStage.java:36)
software.amazon.awssdk.core.internal.http.pipeline.stages.TimeoutExceptionHandlingStage.execute(TimeoutExceptionHandlingStage.java:77)
software.amazon.awssdk.core.internal.http.pipeline.stages.TimeoutExceptionHandlingStage.execute(TimeoutExceptionHandlingStage.java:39)
software.amazon.awssdk.core.internal.http.pipeline.stages.RetryableStage$RetryExecutor.doExecute(RetryableStage.java:113)
software.amazon.awssdk.core.internal.http.pipeline.stages.RetryableStage$RetryExecutor.execute(RetryableStage.java:86)
software.amazon.awssdk.core.internal.http.pipeline.stages.RetryableStage.execute(RetryableStage.java:62)
software.amazon.awssdk.core.internal.http.pipeline.stages.RetryableStage.execute(RetryableStage.java:42)
software.amazon.awssdk.core.internal.http.pipeline.RequestPipelineBuilder$ComposingRequestPipelineStage.execute(RequestPipelineBuilder.java:206)
software.amazon.awssdk.core.internal.http.StreamManagingStage.execute(StreamManagingStage.java:57)
software.amazon.awssdk.core.internal.http.StreamManagingStage.execute(StreamManagingStage.java:37)
software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallTimeoutTrackingStage.executeWithTimer(ApiCallTimeoutTrackingStage.java:80)
software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallTimeoutTrackingStage.execute(ApiCallTimeoutTrackingStage.java:60)
software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallTimeoutTrackingStage.execute(ApiCallTimeoutTrackingStage.java:42)
software.amazon.awssdk.core.internal.http.pipeline.RequestPipelineBuilder$ComposingRequestPipelineStage.execute(RequestPipelineBuilder.java:206)
software.amazon.awssdk.core.internal.http.pipeline.RequestPipelineBuilder$ComposingRequestPipelineStage.execute(RequestPipelineBuilder.java:206)
software.amazon.awssdk.core.internal.http.pipeline.stages.ExecutionFailureExceptionReportingStage.execute(ExecutionFailureExceptionReportingStage.java:37)
software.amazon.awssdk.core.internal.http.pipeline.stages.ExecutionFailureExceptionReportingStage.execute(ExecutionFailureExceptionReportingStage.java:26)
software.amazon.awssdk.core.internal.http.AmazonSyncHttpClient$RequestExecutionBuilderImpl.execute(AmazonSyncHttpClient.java:240)
software.amazon.awssdk.core.client.handler.BaseSyncClientHandler.invoke(BaseSyncClientHandler.java:96)
software.amazon.awssdk.core.client.handler.BaseSyncClientHandler.execute(BaseSyncClientHandler.java:120)
software.amazon.awssdk.core.client.handler.BaseSyncClientHandler.execute(BaseSyncClientHandler.java:73)
software.amazon.awssdk.core.client.handler.SdkSyncClientHandler.execute(SdkSyncClientHandler.java:44)
software.amazon.awssdk.awscore.client.handler.AwsSyncClientHandler.execute(AwsSyncClientHandler.java:55)
software.amazon.awssdk.services.s3.DefaultS3Client.listObjects(DefaultS3Client.java:2278)
com.mycompany.test.MyS3Driver.listObjects(MyS3Driver.java:44)
  1. https://stackoverflow.com/a/30519762/948268--提到键以句点开始。
  2. https://github.com/aws/aws-sdk-js/issues/86--讨论如何确保URI查询字符串参数值的正确编码。但在我的例子中,我使用的是AwsSessionCredentials类。

还将该问题发布在:https://github.com/aws/aws-sdk-java-v2/issues/1767

共有1个答案

赵宏达
2023-03-14

我的错,问题是通过的桶名。除了桶名之外,它还包括文件夹名。是 / 导致了这个问题。在只传递 之后,它工作得很好。

 类似资料:
  • 我正试图上传一张使用AWS S3 SDK的图片。现在,这以前是有效的,但由于某种原因,它停止了工作,我开始得到这个错误: 现在,当我点击链接时,它会给我一个'NoSuchKey'错误: 我真的不明白这一点,因为我的键实际上只是一个字符串。我也试图上传一些东西,所以显然关键不会存在-我正在努力上传它! 另一个教程来自一个博客,我没有它的链接,但我上面的代码与文档中的这个教程相同。我所做的只是把它移到

  • 我有不同的对象来生成保存在S3中的文件。每个对象调用以下函数来保存文件。 我只有以下几个问题。对象要么总是保存文件,要么永远无法保存文件。 错误是 我们计算的请求签名与您提供的签名不匹配。检查您的AWS秘密访问密钥和签名方法。有关详细信息,请参阅REST身份验证和SOAP身份验证。(服务:S3,状态代码:403,请求ID:79b6c0a2-0d74-1f30-a4bf-246e9631ccd8,扩

  • 我试图按照教程HTTP://docs.aws.amazon.com/amazons3/latest/api/sigv4-post-example.html使用HTTP post方法将图像上传到s3 bucket。但我每次都得到以下错误 政策如下: 生成策略和签名的代码将粘贴到相关的表单字段中:

  • 我想从网站content.geappliances.io使用AWS API网关得到响应,但总是得到错误: {“信息”:"我们计算的请求签名与您提供的签名不匹配。请检查您的AWS秘密访问密钥和签名方法。有关详细信息,请参阅服务文档。\n\n此请求的规范字符串应为\n'GET\n/search/b2b/results\n\n内容类型:\n host:content.geapplications.io\

  • 我试图上传图片到s3与预先签署的url从aws SDK生成。 对于上面的返回url,当我试图访问它时,它给出了一个错误 null CORS配置 更新的解决方案

  • 我正试图在iOS上执行AWS上传。我正在遍历一个对象数组,并将大量大小不同的图像(1000+)上传到S3 bucket中。使用: 当我到达大约第700张图像时,上传过程开始变得非常缓慢,然后我开始在随后的每一次图像上传中收到这些错误: 编辑2:每个Yosuke请求的代码(我只是构建了一个请求对象数组,然后循环遍历数组并上传每个请求):