Skip to content

Commit

Permalink
Merge pull request #186 from mziccard/add-storage-create-from-stream
Browse files Browse the repository at this point in the history
Add createFromStream to Storage
  • Loading branch information
aozarov committed Sep 30, 2015
2 parents 5d8d123 + 86065a7 commit 1fa53a8
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpResponseException;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.InputStreamContent;
import com.google.api.client.http.json.JsonHttpContent;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
Expand All @@ -63,6 +64,7 @@

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -119,13 +121,14 @@ public Bucket create(Bucket bucket, Map<Option, ?> options) throws StorageExcept
}

@Override
public StorageObject create(StorageObject storageObject, final byte[] content,
public StorageObject create(StorageObject storageObject, final InputStream content,
Map<Option, ?> options) throws StorageException {
try {
return storage.objects()
Storage.Objects.Insert insert = storage.objects()
.insert(storageObject.getBucket(), storageObject,
new ByteArrayContent(storageObject.getContentType(), content))
.setProjection(DEFAULT_PROJECTION)
new InputStreamContent(storageObject.getContentType(), content));
insert.getMediaHttpUploader().setDirectUploadEnabled(true);
return insert.setProjection(DEFAULT_PROJECTION)
.setPredefinedAcl(PREDEFINED_ACL.getString(options))
.setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options))
.setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options))
Expand Down Expand Up @@ -521,4 +524,3 @@ public String open(StorageObject object, Map<Option, ?> options)
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.gcloud.storage.StorageException;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

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

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

StorageObject create(StorageObject object, byte[] content, Map<Option, ?> options)
StorageObject create(StorageObject object, InputStream content, Map<Option, ?> options)
throws StorageException;

Tuple<String, Iterable<Bucket>> list(Map<Option, ?> options) throws StorageException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.gcloud.Service;
import com.google.gcloud.spi.StorageRpc;

import java.io.InputStream;
import java.io.Serializable;
import java.net.URL;
import java.util.Arrays;
Expand Down Expand Up @@ -493,13 +494,31 @@ public static Builder builder() {
BucketInfo create(BucketInfo bucketInfo, BucketTargetOption... options);

/**
* Create a new blob.
* Create a new blob with no content.
*
* @return a complete blob information.
* @throws StorageException upon failure
*/
BlobInfo create(BlobInfo blobInfo, BlobTargetOption... options);

/**
* Create a new blob. Direct upload is used to upload {@code content}. For large content,
* {@link #writer} is recommended as it uses resumable upload.
*
* @return a complete blob information.
* @throws StorageException upon failure
*/
BlobInfo create(BlobInfo blobInfo, byte[] content, BlobTargetOption... options);

/**
* Create a new blob. Direct upload is used to upload {@code content}. For large content,
* {@link #writer} is recommended as it uses resumable upload.
*
* @return a complete blob information.
* @throws StorageException upon failure
*/
BlobInfo create(BlobInfo blobInfo, InputStream content, BlobTargetOption... options);

/**
* Return the requested bucket or {@code null} if not found.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
import com.google.gcloud.spi.StorageRpc;
import com.google.gcloud.spi.StorageRpc.Tuple;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
Expand Down Expand Up @@ -112,14 +114,27 @@ public com.google.api.services.storage.model.Bucket call() {
}, options().retryParams(), EXCEPTION_HANDLER));
}

@Override
public BlobInfo create(BlobInfo blobInfo, BlobTargetOption... options) {
return create(blobInfo, new ByteArrayInputStream(EMPTY_BYTE_ARRAY), options);
}

@Override
public BlobInfo create(BlobInfo blobInfo, final byte[] content, BlobTargetOption... options) {
return create(blobInfo,
new ByteArrayInputStream(firstNonNull(content, EMPTY_BYTE_ARRAY)), options);
}

@Override
public BlobInfo create(BlobInfo blobInfo, final InputStream content,
BlobTargetOption... options) {
final StorageObject blobPb = blobInfo.toPb();
final Map<StorageRpc.Option, ?> optionsMap = optionMap(blobInfo, options);
return BlobInfo.fromPb(runWithRetries(new Callable<StorageObject>() {
@Override
public StorageObject call() {
return storageRpc.create(blobPb, firstNonNull(content, EMPTY_BYTE_ARRAY), optionsMap);
return storageRpc.create(blobPb,
firstNonNull(content, new ByteArrayInputStream(EMPTY_BYTE_ARRAY)), optionsMap);
}
}, options().retryParams(), EXCEPTION_HANDLER));
}
Expand Down

0 comments on commit 1fa53a8

Please sign in to comment.