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
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.
Here is the source code for
S3Resource
in the Spring Cloud project on GitHub.I’m not sure what you are asking for exactly. The Java package is obviously
io.awspring.cloud.s3
. So make sure you are importingio.awspring.cloud.s3.S3Resource
and not some other class namedS3Resource
.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.