Skip to content

Commit cd1c8cf

Browse files
committed
Add Bucket.create from input stream
1 parent 75d9065 commit cd1c8cf

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

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
*/

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)