Skip to content

Commit 75d9065

Browse files
committed
Add contentType parameter to Blob.create
1 parent 0dd3d97 commit 75d9065

3 files changed

Lines changed: 27 additions & 6 deletions

File tree

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

Lines changed: 12 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,22 @@ 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
179186
* @param options options for blob creation
180187
* @return a complete blob information.
181188
* @throws StorageException upon failure
182189
*/
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));
190+
public Blob create(String blob, byte[] content, String contentType, BlobTargetOption... options) {
191+
BlobInfo blobInfo = BlobInfo.builder(BlobId.of(info.name(), blob))
192+
.contentType(MoreObjects.firstNonNull(contentType, Storage.DEFAULT_CONTENT_TYPE)).build();
193+
return new Blob(storage, storage.create(blobInfo, content, options));
186194
}
187195

188196
/**

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: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class BucketTest {
4545
BlobInfo.builder("b", "n1").build(),
4646
BlobInfo.builder("b", "n2").build(),
4747
BlobInfo.builder("b", "n3").build());
48+
private static final String CONTENT_TYPE = "text/plain";
4849

4950
private Storage storage;
5051
private Bucket bucket;
@@ -161,11 +162,21 @@ public void testGetAll() throws Exception {
161162

162163
@Test
163164
public void testCreate() throws Exception {
164-
BlobInfo info = BlobInfo.builder("b", "n").build();
165+
BlobInfo info = BlobInfo.builder("b", "n").contentType(CONTENT_TYPE).build();
166+
byte[] content = {0xD, 0xE, 0xA, 0xD};
167+
expect(storage.create(info, content)).andReturn(info);
168+
replay(storage);
169+
Blob blob = bucket.create("n", content, CONTENT_TYPE);
170+
assertEquals(info, blob.info());
171+
}
172+
173+
@Test
174+
public void testCreateNullContentType() throws Exception {
175+
BlobInfo info = BlobInfo.builder("b", "n").contentType(Storage.DEFAULT_CONTENT_TYPE).build();
165176
byte[] content = {0xD, 0xE, 0xA, 0xD};
166177
expect(storage.create(info, content)).andReturn(info);
167178
replay(storage);
168-
Blob blob = bucket.create("n", content);
179+
Blob blob = bucket.create("n", content, null);
169180
assertEquals(info, blob.info());
170181
}
171182

0 commit comments

Comments
 (0)