Skip to content

Commit 10e1cb4

Browse files
committed
Merge pull request #281 from mziccard/add-content-type-bucket-create
Add contentType parameter to Bucket.create
2 parents 0dd3d97 + 53328f7 commit 10e1cb4

3 files changed

Lines changed: 72 additions & 6 deletions

File tree

gcloud-java-storage/src/main/java/com/google/gcloud/storage/Bucket.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@
1919
import static com.google.common.base.Preconditions.checkArgument;
2020
import static com.google.common.base.Preconditions.checkNotNull;
2121

22+
import com.google.common.base.MoreObjects;
2223
import com.google.gcloud.storage.Storage.BlobSourceOption;
2324
import com.google.gcloud.storage.Storage.BlobTargetOption;
25+
import com.google.gcloud.storage.Storage.BlobWriteOption;
2426
import com.google.gcloud.storage.Storage.BucketSourceOption;
2527
import com.google.gcloud.storage.Storage.BucketTargetOption;
28+
import java.io.InputStream;
2629

2730
import java.util.ArrayList;
2831
import java.util.Collections;
@@ -172,17 +175,43 @@ public List<Blob> get(String blobName1, String blobName2, String... blobNames) {
172175
}
173176

174177
/**
175-
* Creates a new blob in this bucket.
178+
* Creates a new blob in this bucket. Direct upload is used to upload {@code content}.
179+
* For large content, {@link Blob#writer(com.google.gcloud.storage.Storage.BlobWriteOption...)}
180+
* is recommended as it uses resumable upload. MD5 and CRC32C hashes of {@code content} are
181+
* computed and used for validating transferred data.
176182
*
177183
* @param blob a blob name
178184
* @param content the blob content
185+
* @param contentType the blob content type. If {@code null} then
186+
* {@value com.google.gcloud.storage.Storage#DEFAULT_CONTENT_TYPE} is used.
179187
* @param options options for blob creation
180188
* @return a complete blob information.
181189
* @throws StorageException upon failure
182190
*/
183-
Blob create(String blob, byte[] content, BlobTargetOption... options) {
184-
BlobId blobId = BlobId.of(info.name(), blob);
185-
return new Blob(storage, storage.create(BlobInfo.builder(blobId).build(), content, options));
191+
public Blob create(String blob, byte[] content, String contentType, BlobTargetOption... options) {
192+
BlobInfo blobInfo = BlobInfo.builder(BlobId.of(info.name(), blob))
193+
.contentType(MoreObjects.firstNonNull(contentType, Storage.DEFAULT_CONTENT_TYPE)).build();
194+
return new Blob(storage, storage.create(blobInfo, content, options));
195+
}
196+
197+
/**
198+
* Creates a new blob in this bucket. Direct upload is used to upload {@code content}.
199+
* For large content, {@link Blob#writer(com.google.gcloud.storage.Storage.BlobWriteOption...)}
200+
* is recommended as it uses resumable upload.
201+
*
202+
* @param blob a blob name
203+
* @param content the blob content as a stream
204+
* @param contentType the blob content type. If {@code null} then
205+
* {@value com.google.gcloud.storage.Storage#DEFAULT_CONTENT_TYPE} is used.
206+
* @param options options for blob creation
207+
* @return a complete blob information.
208+
* @throws StorageException upon failure
209+
*/
210+
public Blob create(String blob, InputStream content, String contentType,
211+
BlobWriteOption... options) {
212+
BlobInfo blobInfo = BlobInfo.builder(BlobId.of(info.name(), blob))
213+
.contentType(MoreObjects.firstNonNull(contentType, Storage.DEFAULT_CONTENT_TYPE)).build();
214+
return new Blob(storage, storage.create(blobInfo, content, options));
186215
}
187216

188217
/**

gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
*/
4747
public interface Storage extends Service<StorageOptions> {
4848

49+
public static final String DEFAULT_CONTENT_TYPE = "application/octet-stream";
50+
4951
enum PredefinedAcl {
5052
AUTHENTICATED_READ("authenticatedRead"),
5153
ALL_AUTHENTICATED_USERS("allAuthenticatedUsers"),

gcloud-java-storage/src/test/java/com/google/gcloud/storage/BucketTest.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
import com.google.common.collect.ImmutableList;
3030
import com.google.gcloud.storage.BatchResponse.Result;
31+
import java.io.ByteArrayInputStream;
32+
import java.io.InputStream;
3133
import java.util.Collections;
3234
import java.util.Iterator;
3335
import java.util.LinkedList;
@@ -45,6 +47,7 @@ public class BucketTest {
4547
BlobInfo.builder("b", "n1").build(),
4648
BlobInfo.builder("b", "n2").build(),
4749
BlobInfo.builder("b", "n3").build());
50+
private static final String CONTENT_TYPE = "text/plain";
4851

4952
private Storage storage;
5053
private Bucket bucket;
@@ -161,11 +164,43 @@ public void testGetAll() throws Exception {
161164

162165
@Test
163166
public void testCreate() throws Exception {
164-
BlobInfo info = BlobInfo.builder("b", "n").build();
167+
BlobInfo info = BlobInfo.builder("b", "n").contentType(CONTENT_TYPE).build();
168+
byte[] content = {0xD, 0xE, 0xA, 0xD};
169+
expect(storage.create(info, content)).andReturn(info);
170+
replay(storage);
171+
Blob blob = bucket.create("n", content, CONTENT_TYPE);
172+
assertEquals(info, blob.info());
173+
}
174+
175+
@Test
176+
public void testCreateNullContentType() throws Exception {
177+
BlobInfo info = BlobInfo.builder("b", "n").contentType(Storage.DEFAULT_CONTENT_TYPE).build();
165178
byte[] content = {0xD, 0xE, 0xA, 0xD};
166179
expect(storage.create(info, content)).andReturn(info);
167180
replay(storage);
168-
Blob blob = bucket.create("n", content);
181+
Blob blob = bucket.create("n", content, null);
182+
assertEquals(info, blob.info());
183+
}
184+
185+
@Test
186+
public void testCreateFromStream() throws Exception {
187+
BlobInfo info = BlobInfo.builder("b", "n").contentType(CONTENT_TYPE).build();
188+
byte[] content = {0xD, 0xE, 0xA, 0xD};
189+
InputStream streamContent = new ByteArrayInputStream(content);
190+
expect(storage.create(info, streamContent)).andReturn(info);
191+
replay(storage);
192+
Blob blob = bucket.create("n", streamContent, CONTENT_TYPE);
193+
assertEquals(info, blob.info());
194+
}
195+
196+
@Test
197+
public void testCreateFromStreamNullContentType() throws Exception {
198+
BlobInfo info = BlobInfo.builder("b", "n").contentType(Storage.DEFAULT_CONTENT_TYPE).build();
199+
byte[] content = {0xD, 0xE, 0xA, 0xD};
200+
InputStream streamContent = new ByteArrayInputStream(content);
201+
expect(storage.create(info, streamContent)).andReturn(info);
202+
replay(storage);
203+
Blob blob = bucket.create("n", streamContent, null);
169204
assertEquals(info, blob.info());
170205
}
171206

0 commit comments

Comments
 (0)