Skip to content

Commit 1fa53a8

Browse files
committed
Merge pull request #186 from mziccard/add-storage-create-from-stream
Add createFromStream to Storage
2 parents 5d8d123 + 86065a7 commit 1fa53a8

4 files changed

Lines changed: 45 additions & 8 deletions

File tree

gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import com.google.api.client.http.HttpResponse;
4444
import com.google.api.client.http.HttpResponseException;
4545
import com.google.api.client.http.HttpTransport;
46+
import com.google.api.client.http.InputStreamContent;
4647
import com.google.api.client.http.json.JsonHttpContent;
4748
import com.google.api.client.json.JsonFactory;
4849
import com.google.api.client.json.jackson.JacksonFactory;
@@ -63,6 +64,7 @@
6364

6465
import java.io.ByteArrayOutputStream;
6566
import java.io.IOException;
67+
import java.io.InputStream;
6668
import java.util.ArrayList;
6769
import java.util.List;
6870
import java.util.Map;
@@ -119,13 +121,14 @@ public Bucket create(Bucket bucket, Map<Option, ?> options) throws StorageExcept
119121
}
120122

121123
@Override
122-
public StorageObject create(StorageObject storageObject, final byte[] content,
124+
public StorageObject create(StorageObject storageObject, final InputStream content,
123125
Map<Option, ?> options) throws StorageException {
124126
try {
125-
return storage.objects()
127+
Storage.Objects.Insert insert = storage.objects()
126128
.insert(storageObject.getBucket(), storageObject,
127-
new ByteArrayContent(storageObject.getContentType(), content))
128-
.setProjection(DEFAULT_PROJECTION)
129+
new InputStreamContent(storageObject.getContentType(), content));
130+
insert.getMediaHttpUploader().setDirectUploadEnabled(true);
131+
return insert.setProjection(DEFAULT_PROJECTION)
129132
.setPredefinedAcl(PREDEFINED_ACL.getString(options))
130133
.setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options))
131134
.setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options))
@@ -521,4 +524,3 @@ public String open(StorageObject object, Map<Option, ?> options)
521524
}
522525
}
523526
}
524-

gcloud-java-storage/src/main/java/com/google/gcloud/spi/StorageRpc.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.common.collect.ImmutableMap;
2323
import com.google.gcloud.storage.StorageException;
2424

25+
import java.io.InputStream;
2526
import java.util.List;
2627
import java.util.Map;
2728

@@ -128,7 +129,7 @@ public BatchResponse(Map<StorageObject, Tuple<Boolean, StorageException>> delete
128129

129130
Bucket create(Bucket bucket, Map<Option, ?> options) throws StorageException;
130131

131-
StorageObject create(StorageObject object, byte[] content, Map<Option, ?> options)
132+
StorageObject create(StorageObject object, InputStream content, Map<Option, ?> options)
132133
throws StorageException;
133134

134135
Tuple<String, Iterable<Bucket>> list(Map<Option, ?> options) throws StorageException;

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.gcloud.Service;
2626
import com.google.gcloud.spi.StorageRpc;
2727

28+
import java.io.InputStream;
2829
import java.io.Serializable;
2930
import java.net.URL;
3031
import java.util.Arrays;
@@ -493,13 +494,31 @@ public static Builder builder() {
493494
BucketInfo create(BucketInfo bucketInfo, BucketTargetOption... options);
494495

495496
/**
496-
* Create a new blob.
497+
* Create a new blob with no content.
498+
*
499+
* @return a complete blob information.
500+
* @throws StorageException upon failure
501+
*/
502+
BlobInfo create(BlobInfo blobInfo, BlobTargetOption... options);
503+
504+
/**
505+
* Create a new blob. Direct upload is used to upload {@code content}. For large content,
506+
* {@link #writer} is recommended as it uses resumable upload.
497507
*
498508
* @return a complete blob information.
499509
* @throws StorageException upon failure
500510
*/
501511
BlobInfo create(BlobInfo blobInfo, byte[] content, BlobTargetOption... options);
502512

513+
/**
514+
* Create a new blob. Direct upload is used to upload {@code content}. For large content,
515+
* {@link #writer} is recommended as it uses resumable upload.
516+
*
517+
* @return a complete blob information.
518+
* @throws StorageException upon failure
519+
*/
520+
BlobInfo create(BlobInfo blobInfo, InputStream content, BlobTargetOption... options);
521+
503522
/**
504523
* Return the requested bucket or {@code null} if not found.
505524
*

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
import com.google.gcloud.spi.StorageRpc;
5050
import com.google.gcloud.spi.StorageRpc.Tuple;
5151

52+
import java.io.ByteArrayInputStream;
53+
import java.io.InputStream;
5254
import java.io.Serializable;
5355
import java.io.UnsupportedEncodingException;
5456
import java.net.MalformedURLException;
@@ -112,14 +114,27 @@ public com.google.api.services.storage.model.Bucket call() {
112114
}, options().retryParams(), EXCEPTION_HANDLER));
113115
}
114116

117+
@Override
118+
public BlobInfo create(BlobInfo blobInfo, BlobTargetOption... options) {
119+
return create(blobInfo, new ByteArrayInputStream(EMPTY_BYTE_ARRAY), options);
120+
}
121+
115122
@Override
116123
public BlobInfo create(BlobInfo blobInfo, final byte[] content, BlobTargetOption... options) {
124+
return create(blobInfo,
125+
new ByteArrayInputStream(firstNonNull(content, EMPTY_BYTE_ARRAY)), options);
126+
}
127+
128+
@Override
129+
public BlobInfo create(BlobInfo blobInfo, final InputStream content,
130+
BlobTargetOption... options) {
117131
final StorageObject blobPb = blobInfo.toPb();
118132
final Map<StorageRpc.Option, ?> optionsMap = optionMap(blobInfo, options);
119133
return BlobInfo.fromPb(runWithRetries(new Callable<StorageObject>() {
120134
@Override
121135
public StorageObject call() {
122-
return storageRpc.create(blobPb, firstNonNull(content, EMPTY_BYTE_ARRAY), optionsMap);
136+
return storageRpc.create(blobPb,
137+
firstNonNull(content, new ByteArrayInputStream(EMPTY_BYTE_ARRAY)), optionsMap);
123138
}
124139
}, options().retryParams(), EXCEPTION_HANDLER));
125140
}

0 commit comments

Comments
 (0)