Skip to content

feat: add method createBlobFrom(InputStream) to GoogleStorageResource #3739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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 @@ -205,6 +205,21 @@ public Blob createBlob(byte[] contents) {
return this.storage.create(BlobInfo.newBuilder(getBlobId()).build(), contents);
}

/**
* Creates the blob that this {@link GoogleStorageResource} represents in Google Cloud Storage and
* fills it with provided stream of data.
*
* @param inputStream data read from the InputStream will be used as the initial content of the
* bucket object
* @return the created blob object
* @throws IOException on I/O error
* @throws StorageException if any errors during blob creation arise, such as if the blob already
* exists
*/
public Blob createBlobFrom(InputStream inputStream) throws IOException {
return this.storage.createFrom(BlobInfo.newBuilder(getBlobId()).build(), inputStream);
}

/**
* Creates the bucket that this resource references in Google Cloud Storage.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.cloud.spring.storage.GoogleStorageResource;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -95,6 +96,28 @@ void createAndWriteTest() throws IOException {
}
}

@Test
void createFromStreamTest() throws IOException {

String message = "test message";
InputStream inputStream = new ByteArrayInputStream(message.getBytes());

thisResource().createBlobFrom(inputStream);

assertThat(this.resource)
.returns(true, Resource::exists)
.extracting(
res -> {
try {
return res.getInputStream();
} catch (IOException e) {
throw new AssertionError(e);
}
})
.asString()
.isEqualTo(message);
}

@Test
void createWithContent() throws IOException {

Expand Down