Skip to content
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

fix: Add Blob.uploadFrom(InputStream) #104

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.cloud.RetryHelper.runWithRetries;
import static com.google.cloud.storage.Blob.BlobSourceOption.toGetOptions;
import static com.google.cloud.storage.Blob.BlobSourceOption.toSourceOptions;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.concurrent.Executors.callable;

Expand All @@ -38,9 +39,11 @@
import com.google.common.io.BaseEncoding;
import com.google.common.io.CountingOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.Key;
Expand Down Expand Up @@ -260,6 +263,52 @@ public void downloadTo(Path path) {
downloadTo(path, new BlobSourceOption[0]);
}

/**
* Uploads the given file path to this blob using specified blob write options.
*
* @param path file to be uploaded
* @param options blob write options
* @throws StorageException upon failure
*/
public void uploadFrom(Path path, BlobWriteOption... options) {
uploadFrom(path, DEFAULT_CHUNK_SIZE, options);
}

/**
* Uploads the given file path to this blob using specified blob write options
* and the provided chunk size.
* The default chunk size is 2*1024*1024. Larger chunk sizes may improve
* the upload performance, but require more memory, which can cause OutOfMemoryError
* or add significant garbage collection overhead. Chunk sizes less than 256*1024
* are not allowed, they will be treated as 256*1024.
*
* @param path file to be uploaded
* @param chunkSize the minimum size that will be written by a single RPC.
* @param options blob write options
* @throws StorageException upon failure
*/
public void uploadFrom(Path path, int chunkSize, BlobWriteOption... options) {
if (!Files.exists(path)) {
throw new StorageException(0, path + ": No such file");
}
if (Files.isDirectory(path)) {
throw new StorageException(0, path + ": Is a directory");
}
chunkSize = Math.max(chunkSize, 262144); // adjust with MinChunkSize of BaseWriteChannel
try (WriteChannel writer = storage.writer(this, options)) {
writer.setChunkSize(chunkSize);
byte[] buffer = new byte[chunkSize];
try (InputStream input = Files.newInputStream(path)) {
int length;
while ((length = input.read(buffer)) >= 0) {
writer.write(ByteBuffer.wrap(buffer, 0, length));
}
}
} catch (IOException e) {
throw new StorageException(e);
}
}

/** Builder for {@code Blob}. */
public static class Builder extends BlobInfo.Builder {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.google.api.gax.retrying.RetrySettings;
import com.google.api.services.storage.model.StorageObject;
import com.google.cloud.ReadChannel;
import com.google.cloud.WriteChannel;
import com.google.cloud.storage.Acl.Project;
import com.google.cloud.storage.Acl.Project.ProjectRole;
import com.google.cloud.storage.Acl.Role;
Expand All @@ -51,7 +52,10 @@
import java.io.File;
import java.io.OutputStream;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.Key;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -586,7 +590,7 @@ public void testBuilder() {
}

@Test
public void testDownload() throws Exception {
public void testDownloadTo() throws Exception {
final byte[] expected = {1, 2};
StorageRpc mockStorageRpc = createNiceMock(StorageRpc.class);
expect(storage.getOptions()).andReturn(mockOptions).times(1);
Expand Down Expand Up @@ -618,7 +622,7 @@ public Long answer() throws Throwable {
}

@Test
public void testDownloadWithRetries() throws Exception {
public void testDownloadToWithRetries() throws Exception {
final byte[] expected = {1, 2};
StorageRpc mockStorageRpc = createNiceMock(StorageRpc.class);
expect(storage.getOptions()).andReturn(mockOptions);
Expand Down Expand Up @@ -662,4 +666,52 @@ public Long answer() throws Throwable {
byte actual[] = Files.readAllBytes(file.toPath());
assertArrayEquals(expected, actual);
}

@Test
public void testUploadFromNonExistentFile() {
initializeExpectedBlob(1);
expect(storage.getOptions()).andReturn(mockOptions);
replay(storage);
blob = new Blob(storage, new BlobInfo.BuilderImpl(BLOB_INFO));
String fileName = "non_existing_file.txt";
try {
blob.uploadFrom(Paths.get(fileName));
} catch (StorageException e) {
assertEquals(fileName + ": No such file", e.getMessage());
}
}

@Test
public void testUploadFromDirectory() throws Exception {
initializeExpectedBlob(1);
expect(storage.getOptions()).andReturn(mockOptions);
replay(storage);
blob = new Blob(storage, new BlobInfo.BuilderImpl(BLOB_INFO));
Path dir = Files.createTempDirectory("unit_");
try {
blob.uploadFrom(dir);
} catch (StorageException e) {
assertEquals(dir + ": Is a directory", e.getMessage());
}
}

@Test
public void testUploadFrom() throws Exception {
final byte[] dataToSend = {1,2,3};
ByteBuffer expectedByteBuffer = ByteBuffer.wrap(dataToSend, 0, dataToSend.length);
WriteChannel channel = createMock(WriteChannel.class);
channel.setChunkSize(eq(2097152));
expect(channel.write(expectedByteBuffer)).andReturn(dataToSend.length);
channel.close();
replay(channel);
initializeExpectedBlob(1);
expect(storage.getOptions()).andReturn(mockOptions);
Bucket.BlobWriteOption[] writeOptions = {};
expect(storage.writer(eq(expectedBlob))).andReturn(channel);
replay(storage);
blob = new Blob(storage, new BlobInfo.BuilderImpl(BLOB_INFO));
Path tempFile = Files.createTempFile("testUpload", ".tmp");
Files.write(tempFile, dataToSend);
blob.uploadFrom(tempFile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@
import java.net.URL;
import java.net.URLConnection;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.Key;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -2939,4 +2941,49 @@ public void testBucketLogging() throws ExecutionException, InterruptedException
RemoteStorageHelper.forceDelete(storage, loggingBucket, 5, TimeUnit.SECONDS);
}
}

@Test
public void testUploadFromDownloadTo() throws Exception {
String blobName = "test-uploadFrom-downloadTo-blob";
BlobInfo blobInfo = BlobInfo.newBuilder(BUCKET, blobName).build();

Blob blob1 = storage.create(blobInfo);
byte stringBytes[] = BLOB_STRING_CONTENT.getBytes(UTF_8);
Path tempFileFrom = Files.createTempFile("ITStorageTest_", ".tmp");
Files.write(tempFileFrom, stringBytes);
blob1.uploadFrom(tempFileFrom);

Blob blob2 = storage.get(blobInfo.getBlobId());
Path tempFileTo = Files.createTempFile("ITStorageTest_", ".tmp");
blob2.downloadTo(tempFileTo);
byte[] readBytes = Files.readAllBytes(tempFileTo);
String readString = new String(readBytes, UTF_8);
assertEquals(BLOB_STRING_CONTENT, readString);
}

@Test
public void testUploadFromDownloadToWithEncryption() throws Exception {
String blobName = "test-uploadFrom-downloadTo-withEncryption-blob";
BlobInfo blobInfo = BlobInfo.newBuilder(BUCKET, blobName).build();

Blob blob1 = storage.create(blobInfo);
byte stringBytes[] = BLOB_STRING_CONTENT.getBytes(UTF_8);
Path tempFileFrom = Files.createTempFile("ITStorageTest_", ".tmp");
Files.write(tempFileFrom, stringBytes);
blob1.uploadFrom(tempFileFrom, Storage.BlobWriteOption.encryptionKey(KEY));

Blob blob2 = storage.get(blobInfo.getBlobId());
Path tempFileTo = Files.createTempFile("ITStorageTest_", ".tmp");
try {
blob2.downloadTo(tempFileTo);
} catch (Exception e) {
// Expected to be StorageException
String expectedMessage = "The target object is encrypted by a customer-supplied encryption key.";
assertTrue( e.getMessage().contains(expectedMessage));
}
blob2.downloadTo(tempFileTo, Blob.BlobSourceOption.decryptionKey(KEY));
byte[] readBytes = Files.readAllBytes(tempFileTo);
String readString = new String(readBytes, UTF_8);
assertEquals(BLOB_STRING_CONTENT, readString);
}
}