Skip to content

Commit

Permalink
Remove default contentType from Bucket's create
Browse files Browse the repository at this point in the history
  • Loading branch information
mziccard committed Mar 18, 2016
1 parent 0a113cd commit 8840b33
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 17 deletions.
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();
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 @@ -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

0 comments on commit 8840b33

Please sign in to comment.