skip to Main Content

I am trying to write code to upload images to an S3 bucket. I am having issues with the S3Client and am interested in using the S3REsource construct to upload the file.

In Spring Cloud AWS documentation at
https://docs.awspring.io/spring-cloud-aws/docs/3.0.0-M1/reference/html/index.html#spring-
cloud-aws-s3
there is a section (4.4. S3 Objects as Spring Resources).

The documentation states "To work with the (Spring) Resource as a S3 resource, cast it to

io.awspring.cloud.s3.S3Resource. 

Using S3Resource directly lets you set the S3 object metadata."

I am am unable to locate the package containing

io.awspring.cloud.s3.S3Resource.  Can someone please direct me to it.  I am using Maven so 

the dependency definition would be wonderful to have.

This code fails with a class cast Exception because the Spring Resource being returned does not implement the S3REsource interface. The S3Resource I am using is:

software.amazon.awssdk.services.s3.internal.resource.S3Resource.  Another benefit of  
io.awspring.cloud.s3.S3Resource is that it it is purportedly castable to 
org.springframework.core.io.WritableResource:

S3Resource resource = null;
try {
ResourceLoader resourceLoader = new DefaultResourceLoader();
resource = (S3Resource) resourceLoader.getResource(MessageFormat.format(awsResourceFormat,         
} catch (Exception e) {
e.printStackTrace();
}

2

Answers


  1. What issues do you have uploading an object using https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/s3/S3Client.html?

    Using a product API is typically recommended over using a 3rd party API starting with support. The AWS SDK team will offer support and will not if you are using this 3rd party API.

    If you setup your POM dependencies and setup your creds properly, the Java code works to upload an object. If you do not know how to setup the AWS SDK For Java V2, refer to this topic: Set up the AWS SDK for Java 2.x.

    Full example to upload an object to an S3 bucket.

    //snippet-sourcedescription:[PutObject.java demonstrates how to upload an object to an Amazon Simple Storage Service (Amazon S3) bucket.]
    //snippet-keyword:[AWS SDK for Java v2]
    //snippet-service:[Amazon S3]
    
    /*
       Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
       SPDX-License-Identifier: Apache-2.0
    */
    package com.example.s3;
    
    // snippet-start:[s3.java2.s3_object_upload.import]
    import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
    import software.amazon.awssdk.core.sync.RequestBody;
    import software.amazon.awssdk.regions.Region;
    import software.amazon.awssdk.services.s3.S3Client;
    import software.amazon.awssdk.services.s3.model.PutObjectRequest;
    import software.amazon.awssdk.services.s3.model.S3Exception;
    import java.io.File;
    import java.util.HashMap;
    import java.util.Map;
    // snippet-end:[s3.java2.s3_object_upload.import]
    
    /**
     * Before running this Java V2 code example, set up your development environment, including your credentials.
     *
     * For more information, see the following documentation topic:
     *
     * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
     */
    
    public class PutObject {
    
        public static void main(String[] args) {
            final String usage = "n" +
                "Usage:n" +
                "  <bucketName> <objectKey> <objectPath> nn" +
                "Where:n" +
                "  bucketName - The Amazon S3 bucket to upload an object into.n" +
                "  objectKey - The object to upload (for example, book.pdf).n" +
                "  objectPath - The path where the file is located (for example, C:/AWS/book2.pdf). nn" ;
    
            if (args.length != 3) {
                System.out.println(usage);
                System.exit(1);
            }
    
            String bucketName = args[0];
            String objectKey = args[1];
            String objectPath = args[2];
            ProfileCredentialsProvider credentialsProvider = ProfileCredentialsProvider.create();
            Region region = Region.US_EAST_1;
            S3Client s3 = S3Client.builder()
                .region(region)
                .credentialsProvider(credentialsProvider)
                .build();
    
            putS3Object(s3, bucketName, objectKey, objectPath);
            s3.close();
        }
    
        // snippet-start:[s3.java2.s3_object_upload.main]
        // This example uses RequestBody.fromFile to avoid loading the whole file into memory.
        public static void putS3Object(S3Client s3, String bucketName, String objectKey, String objectPath) {
            try {
                Map<String, String> metadata = new HashMap<>();
                metadata.put("x-amz-meta-myVal", "test");
                PutObjectRequest putOb = PutObjectRequest.builder()
                    .bucket(bucketName)
                    .key(objectKey)
                    .metadata(metadata)
                    .build();
    
                s3.putObject(putOb, RequestBody.fromFile(new File(objectPath)));
                System.out.println("Successfully placed " + objectKey +" into bucket "+bucketName);
    
            } catch (S3Exception e) {
                System.err.println(e.getMessage());
                System.exit(1);
            }
        }
       // snippet-end:[s3.java2.s3_object_upload.main]
    }
    
    Login or Signup to reply.
  2. Here is the source code for S3Resource in the Spring Cloud project on GitHub.

    I am am unable to locate the package containing io.awspring.cloud.s3.S3Resource

    I’m not sure what you are asking for exactly. The Java package is obviously io.awspring.cloud.s3. So make sure you are importing io.awspring.cloud.s3.S3Resource and not some other class named S3Resource.

    If you are asking about the library that contains this class, it looks like it is on Maven here, but I recommend using the appropriate Maven or Gradle BOM detailed here.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search