Skip to content

Commit 93b7a0c

Browse files
committed
createFromStream added to Storage, StorageRpc.create uses InputStream
1 parent abb421e commit 93b7a0c

4 files changed

Lines changed: 29 additions & 7 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: 9 additions & 0 deletions
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;
@@ -500,6 +501,14 @@ public static Builder builder() {
500501
*/
501502
BlobInfo create(BlobInfo blobInfo, byte[] content, BlobTargetOption... options);
502503

504+
/**
505+
* Create a new blob.
506+
*
507+
* @return a complete blob information.
508+
* @throws StorageException upon failure
509+
*/
510+
BlobInfo createFromStream(BlobInfo blobInfo, InputStream content, BlobTargetOption... options);
511+
503512
/**
504513
* Return the requested bucket or {@code null} if not found.
505514
*

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

Lines changed: 11 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;
@@ -114,12 +116,20 @@ public com.google.api.services.storage.model.Bucket call() {
114116

115117
@Override
116118
public BlobInfo create(BlobInfo blobInfo, final byte[] content, BlobTargetOption... options) {
119+
return createFromStream(blobInfo,
120+
new ByteArrayInputStream(firstNonNull(content, EMPTY_BYTE_ARRAY)), options);
121+
}
122+
123+
@Override
124+
public BlobInfo createFromStream(BlobInfo blobInfo, final InputStream content,
125+
BlobTargetOption... options) {
117126
final StorageObject blobPb = blobInfo.toPb();
118127
final Map<StorageRpc.Option, ?> optionsMap = optionMap(blobInfo, options);
119128
return BlobInfo.fromPb(runWithRetries(new Callable<StorageObject>() {
120129
@Override
121130
public StorageObject call() {
122-
return storageRpc.create(blobPb, firstNonNull(content, EMPTY_BYTE_ARRAY), optionsMap);
131+
return storageRpc.create(blobPb,
132+
firstNonNull(content, new ByteArrayInputStream(EMPTY_BYTE_ARRAY)), optionsMap);
123133
}
124134
}, options().retryParams(), EXCEPTION_HANDLER));
125135
}

0 commit comments

Comments
 (0)