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

Remove default contentType from Storage.compose and Bucket.create #762

Merged
merged 6 commits into from
Mar 20, 2016
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -22,7 +22,6 @@
import static com.google.gcloud.storage.Bucket.BucketSourceOption.toSourceOptions;

import com.google.common.base.Function;
import com.google.common.base.MoreObjects;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.gcloud.Page;
Expand Down Expand Up @@ -633,15 +632,13 @@ public List<Blob> get(String blobName1, String blobName2, String... blobNames) {
*
* @param blob a blob name
* @param content the blob content
* @param contentType the blob content type. If {@code null} then
* {@value com.google.gcloud.storage.Storage#DEFAULT_CONTENT_TYPE} is used.
* @param contentType the blob content type
* @param options options for blob creation
* @return a complete blob information
* @throws StorageException upon failure
*/
public Blob create(String blob, byte[] content, String contentType, BlobTargetOption... options) {
BlobInfo blobInfo = BlobInfo.builder(BlobId.of(name(), blob))
.contentType(MoreObjects.firstNonNull(contentType, Storage.DEFAULT_CONTENT_TYPE)).build();
BlobInfo blobInfo = BlobInfo.builder(BlobId.of(name(), blob)).contentType(contentType).build();
StorageRpc.Tuple<BlobInfo, Storage.BlobTargetOption[]> target =
BlobTargetOption.toTargetOptions(blobInfo, options);
return storage.create(target.x(), content, target.y());
Expand All @@ -654,16 +651,51 @@ public Blob create(String blob, byte[] content, String contentType, BlobTargetOp
*
* @param blob a blob name
* @param content the blob content as a stream
* @param contentType the blob content type. If {@code null} then
* {@value com.google.gcloud.storage.Storage#DEFAULT_CONTENT_TYPE} is used.
* @param contentType the blob content type
* @param options options for blob creation
* @return a complete blob information
* @throws StorageException upon failure
*/
public Blob create(String blob, InputStream content, String contentType,
BlobWriteOption... options) {
BlobInfo blobInfo = BlobInfo.builder(BlobId.of(name(), blob))
.contentType(MoreObjects.firstNonNull(contentType, Storage.DEFAULT_CONTENT_TYPE)).build();
BlobInfo blobInfo = BlobInfo.builder(BlobId.of(name(), blob)).contentType(contentType).build();
StorageRpc.Tuple<BlobInfo, Storage.BlobWriteOption[]> write =
BlobWriteOption.toWriteOptions(blobInfo, options);
return storage.create(write.x(), content, write.y());
}

/**
* Creates a new blob in this bucket. Direct upload is used to upload {@code content}.
* For large content, {@link Blob#writer(com.google.gcloud.storage.Storage.BlobWriteOption...)}
* is recommended as it uses resumable upload. MD5 and CRC32C hashes of {@code content} are
* computed and used for validating transferred data.
*
* @param blob a blob name
* @param content the blob content
* @param options options for blob creation
* @return a complete blob information
* @throws StorageException upon failure
*/
public Blob create(String blob, byte[] content, BlobTargetOption... options) {
BlobInfo blobInfo = BlobInfo.builder(BlobId.of(name(), blob)).build();

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

StorageRpc.Tuple<BlobInfo, Storage.BlobTargetOption[]> target =
BlobTargetOption.toTargetOptions(blobInfo, options);
return storage.create(target.x(), content, target.y());
}

/**
* Creates a new blob in this bucket. Direct upload is used to upload {@code content}.
* For large content, {@link Blob#writer(com.google.gcloud.storage.Storage.BlobWriteOption...)}
* is recommended as it uses resumable upload.
*
* @param blob a blob name
* @param content the blob content as a stream
* @param options options for blob creation
* @return a complete blob information
* @throws StorageException upon failure
*/
public Blob create(String blob, InputStream content, BlobWriteOption... options) {
BlobInfo blobInfo = BlobInfo.builder(BlobId.of(name(), blob)).build();
StorageRpc.Tuple<BlobInfo, Storage.BlobWriteOption[]> write =
BlobWriteOption.toWriteOptions(blobInfo, options);
return storage.create(write.x(), content, write.y());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@
*/
public interface Storage extends Service<StorageOptions> {

String DEFAULT_CONTENT_TYPE = "application/octet-stream";

enum PredefinedAcl {
AUTHENTICATED_READ("authenticatedRead"),
ALL_AUTHENTICATED_USERS("allAuthenticatedUsers"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,6 @@ private Storage.Objects.Delete deleteRequest(StorageObject blob, Map<Option, ?>
public StorageObject compose(Iterable<StorageObject> sources, StorageObject target,
Map<Option, ?> targetOptions) {
ComposeRequest request = new ComposeRequest();
if (target.getContentType() == null) {
target.setContentType("application/octet-stream");
}
request.setDestination(target);
List<ComposeRequest.SourceObjects> sourceObjects = new ArrayList<>();
for (StorageObject source : sources) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,16 +293,16 @@ public void testCreate() throws Exception {
}

@Test
public void testCreateNullContentType() throws Exception {
public void testCreateNoContentType() throws Exception {
initializeExpectedBucket(5);
BlobInfo info = BlobInfo.builder("b", "n").contentType(Storage.DEFAULT_CONTENT_TYPE).build();
BlobInfo info = BlobInfo.builder("b", "n").build();
Blob expectedBlob = new Blob(serviceMockReturnsOptions, new BlobInfo.BuilderImpl(info));
byte[] content = {0xD, 0xE, 0xA, 0xD};
expect(storage.options()).andReturn(mockOptions);
expect(storage.create(info, content)).andReturn(expectedBlob);
replay(storage);
initializeBucket();
Blob blob = bucket.create("n", content, null);
Blob blob = bucket.create("n", content);
assertEquals(expectedBlob, blob);
}

Expand Down Expand Up @@ -388,17 +388,17 @@ public void testCreateFromStream() throws Exception {
}

@Test
public void testCreateFromStreamNullContentType() throws Exception {
public void testCreateFromStreamNoContentType() throws Exception {
initializeExpectedBucket(5);
BlobInfo info = BlobInfo.builder("b", "n").contentType(Storage.DEFAULT_CONTENT_TYPE).build();
BlobInfo info = BlobInfo.builder("b", "n").build();
Blob expectedBlob = new Blob(serviceMockReturnsOptions, new BlobInfo.BuilderImpl(info));
byte[] content = {0xD, 0xE, 0xA, 0xD};
InputStream streamContent = new ByteArrayInputStream(content);
expect(storage.options()).andReturn(mockOptions);
expect(storage.create(info, streamContent)).andReturn(expectedBlob);
replay(storage);
initializeBucket();
Blob blob = bucket.create("n", streamContent, null);
Blob blob = bucket.create("n", streamContent);
assertEquals(expectedBlob, blob);
}

Expand Down