Tanino
Tanino

Reputation: 184

Error to send a file to AWS S3 from Java/Spring

I'm trying to send a file to AWS S3 from my Java/Spring application, but I'm getting this error. I check around for this param and I didn't find anything in the docs. Does someone know how to fix it?

Thanks in advance for the help.

Java method

    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());
    }
}

Error:

org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoSuchFieldError: SIGNING_NAME at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1055) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)

Upvotes: 1

Views: 2661

Answers (2)

BERGUIGA Mohamed Amine
BERGUIGA Mohamed Amine

Reputation: 6280

you can use another way to upload your files to S3 like showed bellow on the image

enter image description here

A pre-signed URL allows you to grant temporary access to users who don’t have permission to directly run AWS operations in your account. A pre-signed URL is signed with your credentials and can be used by any user

this link explain how to use this technical

Upvotes: 0

Maciej Walkowiak
Maciej Walkowiak

Reputation: 12932

In cases like this, when the exception relates to missing field or missing class it most likely means that there are multiple versions of AWS SDK on the classpath. Always make sure that different dependencies from AWS SDK have the version aligned.

To make the process simpler, instead of listing all dependencies with versions like this:

  <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>

You can use a BOM (Bill of Materials) in the dependencyManagement section:

  <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>

Upvotes: 5

Related Questions