Skip to content

Commit

Permalink
Add method S3Operations#createResource. (#1223)
Browse files Browse the repository at this point in the history
Fixes #1023
  • Loading branch information
maciejwalkowiak authored Sep 19, 2024
1 parent e7a4cdb commit 0fd457c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ public interface S3Operations {
*/
S3Resource store(String bucketName, String key, Object object);

/**
* Creates an {@link S3Resource} for given bucket name and object key using {@link S3OutputStreamProvider}
* configured on the implementation class ({@link S3Template}).
* <p>
* Note that calling this method does not create an actual object on S3.
*
* @param bucketName - the bucket name
* @param key - the object key
* @return the {@link S3Resource}
*/
S3Resource createResource(String bucketName, String key);

/**
* Reads a Java object from a S3 bucket. Uses {@link S3ObjectConverter} for deserialization.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ public S3Resource store(String bucketName, String key, Object object) {
return new S3Resource(bucketName, key, s3Client, s3OutputStreamProvider);
}

@Override
public S3Resource createResource(String bucketName, String key) {
return new S3Resource(bucketName, key, s3Client, s3OutputStreamProvider);
}

@Override
public <T> T read(String bucketName, String key, Class<T> clazz) {
Assert.notNull(bucketName, "bucketName is required");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -77,4 +78,17 @@ void throwsExceptionWhenReadFails() {
});
}

@Test
void createsS3Resource() throws IOException {
S3OutputStream outputStream = mock(S3OutputStream.class);
when(s3OutputStreamProvider.create(eq("bucket"), eq("key"), any())).thenReturn(outputStream);

S3Resource resource = s3Template.createResource("bucket", "key");

assertThat(resource).isNotNull();
assertThat(resource.getOutputStream()).isEqualTo(outputStream);
assertThat(resource.getLocation().getBucket()).isEqualTo("bucket");
assertThat(resource.getLocation().getObject()).isEqualTo("key");
}

}

0 comments on commit 0fd457c

Please sign in to comment.