从java/spring向aws s3发送文件时出错

ecfdbz9o  于 2021-06-27  发布在  Java
关注(0)|答案(2)|浏览(425)

我试图从我的java/spring应用程序向awss3发送一个文件,但是出现了这个错误。我查了一下这个参数,在文件里什么也没找到。有人知道怎么修吗?
事先谢谢你的帮助。
java方法

public void uploadS3(File file) throws IOException {

    AWSCredentials credentials = new BasicAWSCredentials(applicationProperties.getAwsAccessKey(),applicationProperties.getAwsAccessSecret());
    AmazonS3 s3 = AmazonS3ClientBuilder
        .standard()
        .withCredentials(new AWSStaticCredentialsProvider(credentials))
        .withRegion(Regions.EU_WEST_1)
        .build();

    String fileNameHash = buildNameHash(file.getOriginalFilename(), System.currentTimeMillis());
    String filename = fileNameHash+"."+ FilenameUtils.getExtension(file.getOriginalFilename());

    try {
        s3.putObject(applicationProperties.getAwsImageBucket(), filename, file);
    } catch (AmazonServiceException e) {
        log.error("[uploadS3] error: {}", e.getErrorMessage());
    }
}

错误:
org.springframework.web.util.nestedservletexception:处理程序调度失败;嵌套的异常是java.lang.nosuchfielderror:在org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet)上签名\u名称。java:1055)位于org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet。java:943)位于org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet。java:1006)在org.springframework.web.servlet.frameworkservlet.dopost(frameworkservlet。java:909)

rnmwe5a2

rnmwe5a21#

在这种情况下,当异常与缺少字段或缺少类相关时,很可能意味着类路径上有多个版本的aws sdk。始终确保来自aws sdk的不同依赖项的版本一致。
为了使过程更简单,不要列出具有以下版本的所有依赖项:

<dependencies>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-java-sdk-ec2</artifactId>
      <version>1.10.2</version>
    </dependency>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-java-sdk-s3</artifactId>
      <version>1.10.5</version>
    </dependency>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-java-sdk-dynamodb</artifactId>
      <version>1.10.10</version>
    </dependency>
  <dependencies>

可以在中使用bom表(bom表) dependencyManagement 章节:

<dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-bom</artifactId>
        <version>1.10.10</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-java-sdk-ec2</artifactId>
    </dependency>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-java-sdk-s3</artifactId>
    </dependency>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-java-sdk-dynamodb</artifactId>
    </dependency>
  </dependencies>
kokeuurv

kokeuurv2#

您可以使用另一种方式将文件上传到s3,如下面的图片所示

预签名的url允许您向没有权限在您的帐户中直接运行aws操作的用户授予临时访问权限。预签名的url是用您的凭据签名的,任何用户都可以使用它
此链接解释如何使用此技术

相关问题