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 contentType parameter to Bucket.create #281

Merged
merged 3 commits into from
Oct 22, 2015
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 @@ -19,10 +19,13 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.MoreObjects;
import com.google.gcloud.storage.Storage.BlobSourceOption;
import com.google.gcloud.storage.Storage.BlobTargetOption;
import com.google.gcloud.storage.Storage.BlobWriteOption;
import com.google.gcloud.storage.Storage.BucketSourceOption;
import com.google.gcloud.storage.Storage.BucketTargetOption;
import java.io.InputStream;

import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -172,17 +175,41 @@ public List<Blob> get(String blobName1, String blobName2, String... blobNames) {
}

/**
* Creates a new blob in this bucket.
* 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 contentType the blob content type

This comment was marked as spam.

This comment was marked as spam.

* @param options options for blob creation
* @return a complete blob information.
* @throws StorageException upon failure
*/
Blob create(String blob, byte[] content, BlobTargetOption... options) {
BlobId blobId = BlobId.of(info.name(), blob);
return new Blob(storage, storage.create(BlobInfo.builder(blobId).build(), content, options));
public Blob create(String blob, byte[] content, String contentType, BlobTargetOption... options) {
BlobInfo blobInfo = BlobInfo.builder(BlobId.of(info.name(), blob))
.contentType(MoreObjects.firstNonNull(contentType, Storage.DEFAULT_CONTENT_TYPE)).build();
return new Blob(storage, storage.create(blobInfo, content, options));
}

/**
* 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 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(info.name(), blob))
.contentType(MoreObjects.firstNonNull(contentType, Storage.DEFAULT_CONTENT_TYPE)).build();
return new Blob(storage, storage.create(blobInfo, content, options));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
*/
public interface Storage extends Service<StorageOptions> {

public static final 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 @@ -28,6 +28,8 @@

import com.google.common.collect.ImmutableList;
import com.google.gcloud.storage.BatchResponse.Result;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
Expand All @@ -45,6 +47,7 @@ public class BucketTest {
BlobInfo.builder("b", "n1").build(),
BlobInfo.builder("b", "n2").build(),
BlobInfo.builder("b", "n3").build());
private static final String CONTENT_TYPE = "text/plain";

private Storage storage;
private Bucket bucket;
Expand Down Expand Up @@ -161,11 +164,43 @@ public void testGetAll() throws Exception {

@Test
public void testCreate() throws Exception {
BlobInfo info = BlobInfo.builder("b", "n").build();
BlobInfo info = BlobInfo.builder("b", "n").contentType(CONTENT_TYPE).build();
byte[] content = {0xD, 0xE, 0xA, 0xD};
expect(storage.create(info, content)).andReturn(info);
replay(storage);
Blob blob = bucket.create("n", content, CONTENT_TYPE);
assertEquals(info, blob.info());
}

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

@Test
public void testCreateFromStream() throws Exception {
BlobInfo info = BlobInfo.builder("b", "n").contentType(CONTENT_TYPE).build();
byte[] content = {0xD, 0xE, 0xA, 0xD};
InputStream streamContent = new ByteArrayInputStream(content);
expect(storage.create(info, streamContent)).andReturn(info);
replay(storage);
Blob blob = bucket.create("n", streamContent, CONTENT_TYPE);
assertEquals(info, blob.info());
}

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

Expand Down