Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add createFromStream to Storage #186

Merged
merged 3 commits into from
Sep 30, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -500,6 +501,14 @@ public static Builder builder() {
*/
BlobInfo create(BlobInfo blobInfo, byte[] content, BlobTargetOption... options);

/**
* Create a new blob.

This comment was marked as spam.

*
* @return a complete blob information.
* @throws StorageException upon failure
*/
BlobInfo createFromStream(BlobInfo blobInfo, InputStream content, BlobTargetOption... options);

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.


/**
* 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 @@ -114,12 +116,20 @@ public com.google.api.services.storage.model.Bucket call() {

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

@Override
public BlobInfo createFromStream(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