Skip to content

Commit cfb082a

Browse files
committed
---
yaml --- r: 1077 b: refs/heads/master c: cd1c8cf h: refs/heads/master i: 1075: b4b3b49 v: v3
1 parent 81f58c5 commit cfb082a

3 files changed

Lines changed: 44 additions & 1 deletion

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
---
2-
refs/heads/master: 75d90658261a380c1573ecf2a5f533dc3ed986ed
2+
refs/heads/master: cd1c8cf05dd0446be2b3b059ffd5367e9a49fdc4
33
refs/heads/travis: 0fa997e2fc9c6b61b2d91e6d163655aae67d44b6
44
refs/heads/gh-pages: 5a10432ecc75f29812e33a8236c900379509fe99

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,25 @@ public Blob create(String blob, byte[] content, String contentType, BlobTargetOp
193193
return new Blob(storage, storage.create(blobInfo, content, options));
194194
}
195195

196+
/**
197+
* Creates a new blob in this bucket. Direct upload is used to upload {@code content}.
198+
* For large content, {@link Blob#writer(com.google.gcloud.storage.Storage.BlobWriteOption...)}
199+
* is recommended as it uses resumable upload.
200+
*
201+
* @param blob a blob name
202+
* @param content the blob content as a stream
203+
* @param contentType the blob content type
204+
* @param options options for blob creation
205+
* @return a complete blob information.
206+
* @throws StorageException upon failure
207+
*/
208+
public Blob create(String blob, InputStream content, String contentType,
209+
BlobWriteOption... options) {
210+
BlobInfo blobInfo = BlobInfo.builder(BlobId.of(info.name(), blob))
211+
.contentType(MoreObjects.firstNonNull(contentType, Storage.DEFAULT_CONTENT_TYPE)).build();
212+
return new Blob(storage, storage.create(blobInfo, content, options));
213+
}
214+
196215
/**
197216
* Returns the bucket's {@code Storage} object used to issue requests.
198217
*/

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

Lines changed: 24 additions & 0 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;
@@ -180,6 +182,28 @@ public void testCreateNullContentType() throws Exception {
180182
assertEquals(info, blob.info());
181183
}
182184

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);
204+
assertEquals(info, blob.info());
205+
}
206+
183207
@Test
184208
public void testLoad() throws Exception {
185209
expect(storage.get(BUCKET_INFO.name())).andReturn(BUCKET_INFO);

0 commit comments

Comments
 (0)