Skip to content

Commit

Permalink
Merge pull request #762 from mziccard/remove-compose-content-type
Browse files Browse the repository at this point in the history
Remove default contentType from Storage.compose and Bucket.create
  • Loading branch information
mziccard committed Mar 20, 2016
2 parents 353d2db + 5dd0292 commit fa64fa5
Show file tree
Hide file tree
Showing 12 changed files with 311 additions and 132 deletions.
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();
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 @@ -32,7 +32,13 @@
import java.util.concurrent.Callable;

/**
* Google Storage blob copy writer. This class holds the result of a copy request. If source and
* Google Storage blob copy writer. A {@code CopyWriter} object allows to copy both blob's data and
* information. To override source blob's information supply a {@code BlobInfo} to the
* {@code CopyRequest} using either
* {@link Storage.CopyRequest.Builder#target(BlobInfo, Storage.BlobTargetOption...)} or
* {@link Storage.CopyRequest.Builder#target(BlobInfo, Iterable)}.
*
* <p>This class holds the result of a copy request. If source and
* destination blobs share the same location and storage class the copy is completed in one RPC call
* otherwise one or more {@link #copyChunk} calls are necessary to complete the copy. In addition,
* {@link CopyWriter#result()} can be used to automatically complete the copy and return information
Expand Down Expand Up @@ -65,11 +71,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 +126,10 @@ public RestorableState<CopyWriter> capture() {
serviceOptions,
BlobId.fromPb(rewriteResponse.rewriteRequest.source),
rewriteResponse.rewriteRequest.sourceOptions,
rewriteResponse.rewriteRequest.overrideInfo,
BlobInfo.fromPb(rewriteResponse.rewriteRequest.target),
rewriteResponse.rewriteRequest.targetOptions)
.result(rewriteResponse.result != null ? BlobInfo.fromPb(rewriteResponse.result) : null)
.blobSize(blobSize())
.isDone(isDone())
.megabytesCopiedPerChunk(rewriteResponse.rewriteRequest.megabytesRewrittenPerCall)
Expand All @@ -132,11 +140,12 @@ 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 boolean overrideInfo;
private final BlobInfo target;
private final Map<StorageRpc.Option, ?> targetOptions;
private final BlobInfo result;
Expand All @@ -150,6 +159,7 @@ static class StateImpl implements RestorableState<CopyWriter>, Serializable {
this.serviceOptions = builder.serviceOptions;
this.source = builder.source;
this.sourceOptions = builder.sourceOptions;
this.overrideInfo = builder.overrideInfo;
this.target = builder.target;
this.targetOptions = builder.targetOptions;
this.result = builder.result;
Expand All @@ -165,6 +175,7 @@ static class Builder {
private final StorageOptions serviceOptions;
private final BlobId source;
private final Map<StorageRpc.Option, ?> sourceOptions;
private final boolean overrideInfo;
private final BlobInfo target;
private final Map<StorageRpc.Option, ?> targetOptions;
private BlobInfo result;
Expand All @@ -175,11 +186,12 @@ static class Builder {
private Long megabytesCopiedPerChunk;

private Builder(StorageOptions options, BlobId source,
Map<StorageRpc.Option, ?> sourceOptions,
BlobInfo target, Map<StorageRpc.Option, ?> targetOptions) {
Map<StorageRpc.Option, ?> sourceOptions, boolean overrideInfo, BlobInfo target,
Map<StorageRpc.Option, ?> targetOptions) {
this.serviceOptions = options;
this.source = source;
this.sourceOptions = sourceOptions;
this.overrideInfo = overrideInfo;
this.target = target;
this.targetOptions = targetOptions;
}
Expand Down Expand Up @@ -220,15 +232,15 @@ RestorableState<CopyWriter> build() {
}

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

@Override
public CopyWriter restore() {
RewriteRequest rewriteRequest = new RewriteRequest(
source.toPb(), sourceOptions, target.toPb(), targetOptions, megabytesCopiedPerChunk);
RewriteRequest rewriteRequest = new RewriteRequest(source.toPb(), sourceOptions,
overrideInfo, target.toPb(), targetOptions, megabytesCopiedPerChunk);
RewriteResponse rewriteResponse = new RewriteResponse(rewriteRequest,
result != null ? result.toPb() : null, blobSize, isDone, rewriteToken,
totalBytesCopied);
Expand All @@ -237,8 +249,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, overrideInfo, target,
targetOptions, result, blobSize, isDone, megabytesCopiedPerChunk, rewriteToken,
totalBytesCopied);
}

@Override
Expand All @@ -253,6 +266,7 @@ 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.overrideInfo, other.overrideInfo)
&& Objects.equals(this.target, other.target)
&& Objects.equals(this.targetOptions, other.targetOptions)
&& Objects.equals(this.result, other.result)
Expand All @@ -267,10 +281,14 @@ public boolean equals(Object obj) {
public String toString() {
return MoreObjects.toStringHelper(this)
.add("source", source)
.add("overrideInfo", overrideInfo)
.add("target", target)
.add("isDone", isDone)
.add("totalBytesRewritten", totalBytesCopied)
.add("result", result)
.add("blobSize", blobSize)
.add("isDone", isDone)
.add("rewriteToken", rewriteToken)
.add("totalBytesCopied", totalBytesCopied)
.add("megabytesCopiedPerChunk", megabytesCopiedPerChunk)
.toString();
}
}
Expand Down
Loading

0 comments on commit fa64fa5

Please sign in to comment.