Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.io.InputStream;
import org.springframework.util.Assert;
import org.springframework.core.ParametrizedTypeReference;
import software.amazon.awssdk.core.sync.RequestBody;
import tools.jackson.core.JacksonException;
import tools.jackson.databind.json.JsonMapper;
Expand Down Expand Up @@ -58,6 +59,19 @@ public <T> T read(InputStream is, Class<T> clazz) {
}
}

@Override
public <T> T read(InputStream is, ParametrizedTypeReference<T> valueTypeRef) {
Assert.notNull(is, "InputStream is required");
Assert.notNull(valueTypeRef, "valueTypeRef is required");
try {
return jsonMapper.readValue(is, jsonMapper.constructType(valueTypeRef.getType()));
}
catch (IOException e) {
throw new S3Exception("Failed to deserialize object from JSON", e);
}
}


@Override
public String contentType() {
return "application/json";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.IOException;
import java.io.InputStream;
import org.springframework.util.Assert;
import org.springframework.core.ParameterizedTypeReference;
import software.amazon.awssdk.core.sync.RequestBody;

/**
Expand Down Expand Up @@ -60,6 +61,18 @@ public <T> T read(InputStream is, Class<T> clazz) {
}
}

@Override
public <T> T read(InputStream is, ParametrizedTypeReference<T> valueTypeRef) {
Assert.notNull(is, "InputStream is required");
Assert.notNull(valueTypeRef, "valueTypeRef is required");
try {
return objectMapper.readValue(is, objectMapper.constructType(valueTypeRef.getType()));
}
catch (IOException e) {
throw new S3Exception("Failed to deserialize object from JSON", e);
}
}

@Override
public String contentType() {
return "application/json";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.awspring.cloud.s3;

import java.io.InputStream;
import org.springframework.core.ParameterizedTypeReference;
import software.amazon.awssdk.core.sync.RequestBody;

/**
Expand Down Expand Up @@ -43,6 +44,15 @@ public interface S3ObjectConverter {
*/
<T> T read(InputStream is, Class<T> clazz);

/**
* Reads S3 object from the input stream into a Java object.
* @param is - the input stream
* @param valueTypeRef - the type reference of the object
* @param <T> - the the type of the object
* @return deserialized object
*/
<T> T read(InputStream is, ParameterizedTypeReference<T> valueTypeRef);

/**
* Supported content type.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.time.Duration;
import java.util.List;
import org.jspecify.annotations.Nullable;
import org.springframework.core.ParameterizedTypeReference;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.CreateBucketResponse;

Expand Down Expand Up @@ -132,6 +133,17 @@ public interface S3Operations {
*/
<T> T read(String bucketName, String key, Class<T> clazz);

/**
* Reads a Java object from a S3 bucket. Uses {@link S3ObjectConverter} for deserialization.
*
* @param bucketName - the bucket name
* @param key - the object key
* @param valueTypeRef - the parameterized type reference of the read object
* @param <T> - the type of the read object
* @return an object
*/
<T> T read(String bucketName, String key, ParameterizedTypeReference<T> valueTypeRef);

/**
* Uploads data from an input stream to a S3 bucket.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.time.Duration;
import java.util.List;
import org.jspecify.annotations.Nullable;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.util.Assert;
import org.springframework.util.StreamUtils;
import software.amazon.awssdk.services.s3.S3Client;
Expand Down Expand Up @@ -165,6 +166,21 @@ public <T> T read(String bucketName, String key, Class<T> clazz) {
}
}

@Override
public <T> T read(String bucketName, String key, ParameterizedTypeReference<T> valueTypeRef) {
Assert.notNull(bucketName, "bucketName is required");
Assert.notNull(key, "key is required");
Assert.notNull(valueTypeRef, "valueTypeRef is required");

try (InputStream is = s3Client.getObject(r -> r.bucket(bucketName).key(key))) {
return s3ObjectConverter.read(is, valueTypeRef);
}
catch (Exception e) {
throw new S3Exception(
String.format("Failed to read object with a key '%s' from bucket '%s'", key, bucketName), e);
}
}

@Override
public S3Resource upload(String bucketName, String key, InputStream inputStream,
@Nullable ObjectMetadata objectMetadata) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.util.StreamUtils;
import org.springframework.core.ParameterizedTypeReference;
import software.amazon.awssdk.core.ResponseInputStream;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.http.HttpStatusCode;
Expand Down Expand Up @@ -232,6 +233,20 @@ void readsObject() {
assertThat(person.lastName).isEqualTo("Doe");
}

@Test
void readsParameterizedTypeObject() {
client.putObject(r -> r.bucket(BUCKET_NAME).key("persons.json"),
RequestBody.fromString("[{\"firstName\":\"John\",\"lastName\":\"Doe\"}]"));

List<Person> persons = s3Template.read(BUCKET_NAME, "persons.json", new ParamaterizedTypeReference<List<Person>>(){});

assertThat(persons.size()).isEqualTo(1);

Person person = persons.get(0);
assertThat(person.firstName).isEqualTo("John");
assertThat(person.lastName).isEqualTo("Doe");
}

@Test
void uploadsFile() throws IOException {
try (InputStream is = new ByteArrayInputStream("hello".getBytes(StandardCharsets.UTF_8))) {
Expand Down