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

add disableGzipContent option for create from InputStream #82

Merged
Merged
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
Expand Up @@ -567,7 +567,8 @@ enum Option {
IF_CRC32C_MATCH,
CUSTOMER_SUPPLIED_KEY,
KMS_KEY_NAME,
USER_PROJECT;
USER_PROJECT,
IF_DISABLE_GZIP_CONTENT;

StorageRpc.Option toRpcOption() {
return StorageRpc.Option.valueOf(this.name());
Expand Down Expand Up @@ -699,6 +700,14 @@ public static BlobWriteOption kmsKeyName(String kmsKeyName) {
public static BlobWriteOption userProject(String userProject) {
return new BlobWriteOption(Option.USER_PROJECT, userProject);
}

/**
* Returns an option that signals automatic gzip compression should not be performed en route to
* the bucket.
*/
public static BlobWriteOption disableGzipContent() {
return new BlobWriteOption(Option.IF_DISABLE_GZIP_CONTENT, true);
}
}

/** Class for specifying blob source options. */
Expand Down
Expand Up @@ -741,6 +741,33 @@ public void testCreateBlobFromStream() throws IOException {
assertEquals(-1, byteStream.read(streamBytes));
}

@Test
public void testCreateBlobFromStreamDisableGzipContent() throws IOException {
Capture<ByteArrayInputStream> capturedStream = Capture.newInstance();

ByteArrayInputStream fileStream = new ByteArrayInputStream(BLOB_CONTENT);
BlobInfo.Builder infoBuilder = BLOB_INFO1.toBuilder();
BlobInfo infoWithHashes = infoBuilder.setMd5(CONTENT_MD5).setCrc32c(CONTENT_CRC32C).build();
BlobInfo infoWithoutHashes = infoBuilder.setMd5(null).setCrc32c(null).build();
EasyMock.expect(
storageRpcMock.create(
EasyMock.eq(infoWithoutHashes.toPb()),
EasyMock.capture(capturedStream),
EasyMock.eq(BLOB_TARGET_OPTIONS_CREATE_DISABLE_GZIP_CONTENT)))
.andReturn(BLOB_INFO1.toPb());
EasyMock.replay(storageRpcMock);
initializeService();

Blob blob = storage.create(infoWithHashes, fileStream, BlobWriteOption.disableGzipContent());

assertEquals(expectedBlob1, blob);
ByteArrayInputStream byteStream = capturedStream.getValue();
byte[] streamBytes = new byte[BLOB_CONTENT.length];
assertEquals(BLOB_CONTENT.length, byteStream.read(streamBytes));
assertArrayEquals(BLOB_CONTENT, streamBytes);
assertEquals(-1, byteStream.read(streamBytes));
}

@Test
public void testCreateBlobFromStreamWithEncryptionKey() throws IOException {
ByteArrayInputStream fileStream = new ByteArrayInputStream(BLOB_CONTENT);
Expand Down
Expand Up @@ -69,6 +69,7 @@
import com.google.cloud.storage.ServiceAccount;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.Storage.BlobField;
import com.google.cloud.storage.Storage.BlobWriteOption;
import com.google.cloud.storage.Storage.BucketField;
import com.google.cloud.storage.StorageBatch;
import com.google.cloud.storage.StorageBatchResult;
Expand Down Expand Up @@ -597,6 +598,20 @@ public void testCreateBlobStream() {
assertEquals(BLOB_STRING_CONTENT, new String(readBytes, UTF_8));
}

@Test
public void testCreateBlobStreamDisableGzipContent() {
String blobName = "test-create-blob-stream-disable-gzip-compression";
BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).setContentType(CONTENT_TYPE).build();
ByteArrayInputStream stream = new ByteArrayInputStream(BLOB_STRING_CONTENT.getBytes(UTF_8));
Blob remoteBlob = storage.create(blob, stream, BlobWriteOption.disableGzipContent());
assertNotNull(remoteBlob);
assertEquals(blob.getBucket(), remoteBlob.getBucket());
assertEquals(blob.getName(), remoteBlob.getName());
assertEquals(blob.getContentType(), remoteBlob.getContentType());
byte[] readBytes = storage.readAllBytes(BUCKET, blobName);
assertEquals(BLOB_STRING_CONTENT, new String(readBytes, UTF_8));
}

@Test
public void testCreateBlobFail() {
String blobName = "test-create-blob-fail";
Expand Down