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

Remove default contentType from Storage.compose and Bucket.create #762

Merged
merged 6 commits into from
Mar 20, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
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 @@ -22,7 +22,6 @@
import static com.google.gcloud.storage.Bucket.BucketSourceOption.toSourceOptions;

import com.google.common.base.Function;
import com.google.common.base.MoreObjects;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.gcloud.Page;
Expand Down Expand Up @@ -633,15 +632,13 @@ public List<Blob> get(String blobName1, String blobName2, String... blobNames) {
*
* @param blob a blob name
* @param content the blob content
* @param contentType the blob content type. If {@code null} then
* {@value com.google.gcloud.storage.Storage#DEFAULT_CONTENT_TYPE} is used.
* @param contentType the blob content type
* @param options options for blob creation
* @return a complete blob information
* @throws StorageException upon failure
*/
public Blob create(String blob, byte[] content, String contentType, BlobTargetOption... options) {
BlobInfo blobInfo = BlobInfo.builder(BlobId.of(name(), blob))
.contentType(MoreObjects.firstNonNull(contentType, Storage.DEFAULT_CONTENT_TYPE)).build();
BlobInfo blobInfo = BlobInfo.builder(BlobId.of(name(), blob)).contentType(contentType).build();
StorageRpc.Tuple<BlobInfo, Storage.BlobTargetOption[]> target =
BlobTargetOption.toTargetOptions(blobInfo, options);
return storage.create(target.x(), content, target.y());
Expand All @@ -654,16 +651,51 @@ public Blob create(String blob, byte[] content, String contentType, BlobTargetOp
*
* @param blob a blob name
* @param content the blob content as a stream
* @param contentType the blob content type. If {@code null} then
* {@value com.google.gcloud.storage.Storage#DEFAULT_CONTENT_TYPE} is used.
* @param contentType the blob content type
* @param options options for blob creation
* @return a complete blob information
* @throws StorageException upon failure
*/
public Blob create(String blob, InputStream content, String contentType,
BlobWriteOption... options) {
BlobInfo blobInfo = BlobInfo.builder(BlobId.of(name(), blob))
.contentType(MoreObjects.firstNonNull(contentType, Storage.DEFAULT_CONTENT_TYPE)).build();
BlobInfo blobInfo = BlobInfo.builder(BlobId.of(name(), blob)).contentType(contentType).build();
StorageRpc.Tuple<BlobInfo, Storage.BlobWriteOption[]> write =
BlobWriteOption.toWriteOptions(blobInfo, options);
return storage.create(write.x(), content, write.y());
}

/**
* Creates a new blob in this bucket. Direct upload is used to upload {@code content}.
* For large content, {@link Blob#writer(com.google.gcloud.storage.Storage.BlobWriteOption...)}
* is recommended as it uses resumable upload. MD5 and CRC32C hashes of {@code content} are
* computed and used for validating transferred data.
*
* @param blob a blob name
* @param content the blob content
* @param options options for blob creation
* @return a complete blob information
* @throws StorageException upon failure
*/
public Blob create(String blob, byte[] content, BlobTargetOption... options) {
BlobInfo blobInfo = BlobInfo.builder(BlobId.of(name(), blob)).build();

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

StorageRpc.Tuple<BlobInfo, Storage.BlobTargetOption[]> target =
BlobTargetOption.toTargetOptions(blobInfo, options);
return storage.create(target.x(), content, target.y());
}

/**
* Creates a new blob in this bucket. Direct upload is used to upload {@code content}.
* For large content, {@link Blob#writer(com.google.gcloud.storage.Storage.BlobWriteOption...)}
* is recommended as it uses resumable upload.
*
* @param blob a blob name
* @param content the blob content as a stream
* @param options options for blob creation
* @return a complete blob information
* @throws StorageException upon failure
*/
public Blob create(String blob, InputStream content, BlobWriteOption... options) {
BlobInfo blobInfo = BlobInfo.builder(BlobId.of(name(), blob)).build();
StorageRpc.Tuple<BlobInfo, Storage.BlobWriteOption[]> write =
BlobWriteOption.toWriteOptions(blobInfo, options);
return storage.create(write.x(), content, write.y());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ public class CopyWriter implements Restorable<CopyWriter> {
*
* @throws StorageException upon failure
*/
public BlobInfo result() {
public Blob result() {
while (!isDone()) {
copyChunk();
}
return BlobInfo.fromPb(rewriteResponse.result);
return Blob.fromPb(serviceOptions.service(), rewriteResponse.result);
}

/**
Expand Down Expand Up @@ -120,8 +120,12 @@ public RestorableState<CopyWriter> capture() {
serviceOptions,
BlobId.fromPb(rewriteResponse.rewriteRequest.source),
rewriteResponse.rewriteRequest.sourceOptions,
BlobInfo.fromPb(rewriteResponse.rewriteRequest.target),
BlobId.of(rewriteResponse.rewriteRequest.targetBucket,
rewriteResponse.rewriteRequest.targetName),
rewriteResponse.rewriteRequest.targetOptions)
.targetInfo(rewriteResponse.rewriteRequest.targetObject != null
? BlobInfo.fromPb(rewriteResponse.rewriteRequest.targetObject) : null)
.result(rewriteResponse.result != null ? BlobInfo.fromPb(rewriteResponse.result) : null)

This comment was marked as spam.

This comment was marked as spam.

.blobSize(blobSize())
.isDone(isDone())
.megabytesCopiedPerChunk(rewriteResponse.rewriteRequest.megabytesRewrittenPerCall)
Expand All @@ -132,12 +136,13 @@ public RestorableState<CopyWriter> capture() {

static class StateImpl implements RestorableState<CopyWriter>, Serializable {

private static final long serialVersionUID = 8279287678903181701L;
private static final long serialVersionUID = 1693964441435822700L;

private final StorageOptions serviceOptions;
private final BlobId source;
private final Map<StorageRpc.Option, ?> sourceOptions;
private final BlobInfo target;
private final BlobId targetId;
private final BlobInfo targetInfo;
private final Map<StorageRpc.Option, ?> targetOptions;
private final BlobInfo result;
private final long blobSize;
Expand All @@ -150,7 +155,8 @@ static class StateImpl implements RestorableState<CopyWriter>, Serializable {
this.serviceOptions = builder.serviceOptions;
this.source = builder.source;
this.sourceOptions = builder.sourceOptions;
this.target = builder.target;
this.targetId = builder.targetId;
this.targetInfo = builder.targetInfo;
this.targetOptions = builder.targetOptions;
this.result = builder.result;
this.blobSize = builder.blobSize;
Expand All @@ -165,8 +171,9 @@ static class Builder {
private final StorageOptions serviceOptions;
private final BlobId source;
private final Map<StorageRpc.Option, ?> sourceOptions;
private final BlobInfo target;
private final BlobId targetId;
private final Map<StorageRpc.Option, ?> targetOptions;
private BlobInfo targetInfo;
private BlobInfo result;
private long blobSize;
private boolean isDone;
Expand All @@ -176,14 +183,19 @@ static class Builder {

private Builder(StorageOptions options, BlobId source,
Map<StorageRpc.Option, ?> sourceOptions,
BlobInfo target, Map<StorageRpc.Option, ?> targetOptions) {
BlobId targetId, Map<StorageRpc.Option, ?> targetOptions) {
this.serviceOptions = options;
this.source = source;
this.sourceOptions = sourceOptions;
this.target = target;
this.targetId = targetId;
this.targetOptions = targetOptions;
}

Builder targetInfo(BlobInfo targetInfo) {
this.targetInfo = targetInfo;
return this;
}

Builder result(BlobInfo result) {
this.result = result;
return this;
Expand Down Expand Up @@ -220,15 +232,16 @@ RestorableState<CopyWriter> build() {
}

static Builder builder(StorageOptions options, BlobId source,
Map<StorageRpc.Option, ?> sourceOptions, BlobInfo target,
Map<StorageRpc.Option, ?> sourceOptions, BlobId targetId,
Map<StorageRpc.Option, ?> targetOptions) {
return new Builder(options, source, sourceOptions, target, targetOptions);
return new Builder(options, source, sourceOptions, targetId, targetOptions);
}

@Override
public CopyWriter restore() {
RewriteRequest rewriteRequest = new RewriteRequest(
source.toPb(), sourceOptions, target.toPb(), targetOptions, megabytesCopiedPerChunk);
RewriteRequest rewriteRequest = new RewriteRequest(source.toPb(), sourceOptions,
targetId.bucket(), targetId.name(), targetInfo != null ? targetInfo.toPb() : null,
targetOptions, megabytesCopiedPerChunk);
RewriteResponse rewriteResponse = new RewriteResponse(rewriteRequest,
result != null ? result.toPb() : null, blobSize, isDone, rewriteToken,
totalBytesCopied);
Expand All @@ -237,8 +250,9 @@ public CopyWriter restore() {

@Override
public int hashCode() {
return Objects.hash(serviceOptions, source, sourceOptions, target, targetOptions, result,
blobSize, isDone, megabytesCopiedPerChunk, rewriteToken, totalBytesCopied);
return Objects.hash(serviceOptions, source, sourceOptions, targetId, targetInfo,
targetOptions, result, blobSize, isDone, megabytesCopiedPerChunk, rewriteToken,
totalBytesCopied);
}

@Override
Expand All @@ -253,7 +267,8 @@ public boolean equals(Object obj) {
return Objects.equals(this.serviceOptions, other.serviceOptions)
&& Objects.equals(this.source, other.source)
&& Objects.equals(this.sourceOptions, other.sourceOptions)
&& Objects.equals(this.target, other.target)
&& Objects.equals(this.targetId, other.targetId)
&& Objects.equals(this.targetInfo, other.targetInfo)
&& Objects.equals(this.targetOptions, other.targetOptions)
&& Objects.equals(this.result, other.result)
&& Objects.equals(this.rewriteToken, other.rewriteToken)
Expand All @@ -267,10 +282,14 @@ public boolean equals(Object obj) {
public String toString() {
return MoreObjects.toStringHelper(this)
.add("source", source)
.add("target", target)
.add("isDone", isDone)
.add("totalBytesRewritten", totalBytesCopied)
.add("targetId", targetId)
.add("targetInfo", targetInfo)
.add("result", result)
.add("blobSize", blobSize)
.add("isDone", isDone)
.add("rewriteToken", rewriteToken)
.add("totalBytesCopied", totalBytesCopied)
.add("megabytesCopiedPerChunk", megabytesCopiedPerChunk)
.toString();
}
}
Expand Down
Loading