Skip to content

Commit fa64fa5

Browse files
committed
Merge pull request #762 from mziccard/remove-compose-content-type
Remove default contentType from Storage.compose and Bucket.create
2 parents 353d2db + 5dd0292 commit fa64fa5

File tree

12 files changed

+311
-132
lines changed

12 files changed

+311
-132
lines changed

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

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import static com.google.gcloud.storage.Bucket.BucketSourceOption.toSourceOptions;
2323

2424
import com.google.common.base.Function;
25-
import com.google.common.base.MoreObjects;
2625
import com.google.common.collect.Lists;
2726
import com.google.common.collect.Sets;
2827
import com.google.gcloud.Page;
@@ -633,15 +632,13 @@ public List<Blob> get(String blobName1, String blobName2, String... blobNames) {
633632
*
634633
* @param blob a blob name
635634
* @param content the blob content
636-
* @param contentType the blob content type. If {@code null} then
637-
* {@value com.google.gcloud.storage.Storage#DEFAULT_CONTENT_TYPE} is used.
635+
* @param contentType the blob content type
638636
* @param options options for blob creation
639637
* @return a complete blob information
640638
* @throws StorageException upon failure
641639
*/
642640
public Blob create(String blob, byte[] content, String contentType, BlobTargetOption... options) {
643-
BlobInfo blobInfo = BlobInfo.builder(BlobId.of(name(), blob))
644-
.contentType(MoreObjects.firstNonNull(contentType, Storage.DEFAULT_CONTENT_TYPE)).build();
641+
BlobInfo blobInfo = BlobInfo.builder(BlobId.of(name(), blob)).contentType(contentType).build();
645642
StorageRpc.Tuple<BlobInfo, Storage.BlobTargetOption[]> target =
646643
BlobTargetOption.toTargetOptions(blobInfo, options);
647644
return storage.create(target.x(), content, target.y());
@@ -654,16 +651,51 @@ public Blob create(String blob, byte[] content, String contentType, BlobTargetOp
654651
*
655652
* @param blob a blob name
656653
* @param content the blob content as a stream
657-
* @param contentType the blob content type. If {@code null} then
658-
* {@value com.google.gcloud.storage.Storage#DEFAULT_CONTENT_TYPE} is used.
654+
* @param contentType the blob content type
659655
* @param options options for blob creation
660656
* @return a complete blob information
661657
* @throws StorageException upon failure
662658
*/
663659
public Blob create(String blob, InputStream content, String contentType,
664660
BlobWriteOption... options) {
665-
BlobInfo blobInfo = BlobInfo.builder(BlobId.of(name(), blob))
666-
.contentType(MoreObjects.firstNonNull(contentType, Storage.DEFAULT_CONTENT_TYPE)).build();
661+
BlobInfo blobInfo = BlobInfo.builder(BlobId.of(name(), blob)).contentType(contentType).build();
662+
StorageRpc.Tuple<BlobInfo, Storage.BlobWriteOption[]> write =
663+
BlobWriteOption.toWriteOptions(blobInfo, options);
664+
return storage.create(write.x(), content, write.y());
665+
}
666+
667+
/**
668+
* Creates a new blob in this bucket. Direct upload is used to upload {@code content}.
669+
* For large content, {@link Blob#writer(com.google.gcloud.storage.Storage.BlobWriteOption...)}
670+
* is recommended as it uses resumable upload. MD5 and CRC32C hashes of {@code content} are
671+
* computed and used for validating transferred data.
672+
*
673+
* @param blob a blob name
674+
* @param content the blob content
675+
* @param options options for blob creation
676+
* @return a complete blob information
677+
* @throws StorageException upon failure
678+
*/
679+
public Blob create(String blob, byte[] content, BlobTargetOption... options) {
680+
BlobInfo blobInfo = BlobInfo.builder(BlobId.of(name(), blob)).build();
681+
StorageRpc.Tuple<BlobInfo, Storage.BlobTargetOption[]> target =
682+
BlobTargetOption.toTargetOptions(blobInfo, options);
683+
return storage.create(target.x(), content, target.y());
684+
}
685+
686+
/**
687+
* Creates a new blob in this bucket. Direct upload is used to upload {@code content}.
688+
* For large content, {@link Blob#writer(com.google.gcloud.storage.Storage.BlobWriteOption...)}
689+
* is recommended as it uses resumable upload.
690+
*
691+
* @param blob a blob name
692+
* @param content the blob content as a stream
693+
* @param options options for blob creation
694+
* @return a complete blob information
695+
* @throws StorageException upon failure
696+
*/
697+
public Blob create(String blob, InputStream content, BlobWriteOption... options) {
698+
BlobInfo blobInfo = BlobInfo.builder(BlobId.of(name(), blob)).build();
667699
StorageRpc.Tuple<BlobInfo, Storage.BlobWriteOption[]> write =
668700
BlobWriteOption.toWriteOptions(blobInfo, options);
669701
return storage.create(write.x(), content, write.y());

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

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@
3232
import java.util.concurrent.Callable;
3333

3434
/**
35-
* Google Storage blob copy writer. This class holds the result of a copy request. If source and
35+
* Google Storage blob copy writer. A {@code CopyWriter} object allows to copy both blob's data and
36+
* information. To override source blob's information supply a {@code BlobInfo} to the
37+
* {@code CopyRequest} using either
38+
* {@link Storage.CopyRequest.Builder#target(BlobInfo, Storage.BlobTargetOption...)} or
39+
* {@link Storage.CopyRequest.Builder#target(BlobInfo, Iterable)}.
40+
*
41+
* <p>This class holds the result of a copy request. If source and
3642
* destination blobs share the same location and storage class the copy is completed in one RPC call
3743
* otherwise one or more {@link #copyChunk} calls are necessary to complete the copy. In addition,
3844
* {@link CopyWriter#result()} can be used to automatically complete the copy and return information
@@ -65,11 +71,11 @@ public class CopyWriter implements Restorable<CopyWriter> {
6571
*
6672
* @throws StorageException upon failure
6773
*/
68-
public BlobInfo result() {
74+
public Blob result() {
6975
while (!isDone()) {
7076
copyChunk();
7177
}
72-
return BlobInfo.fromPb(rewriteResponse.result);
78+
return Blob.fromPb(serviceOptions.service(), rewriteResponse.result);
7379
}
7480

7581
/**
@@ -120,8 +126,10 @@ public RestorableState<CopyWriter> capture() {
120126
serviceOptions,
121127
BlobId.fromPb(rewriteResponse.rewriteRequest.source),
122128
rewriteResponse.rewriteRequest.sourceOptions,
129+
rewriteResponse.rewriteRequest.overrideInfo,
123130
BlobInfo.fromPb(rewriteResponse.rewriteRequest.target),
124131
rewriteResponse.rewriteRequest.targetOptions)
132+
.result(rewriteResponse.result != null ? BlobInfo.fromPb(rewriteResponse.result) : null)
125133
.blobSize(blobSize())
126134
.isDone(isDone())
127135
.megabytesCopiedPerChunk(rewriteResponse.rewriteRequest.megabytesRewrittenPerCall)
@@ -132,11 +140,12 @@ public RestorableState<CopyWriter> capture() {
132140

133141
static class StateImpl implements RestorableState<CopyWriter>, Serializable {
134142

135-
private static final long serialVersionUID = 8279287678903181701L;
143+
private static final long serialVersionUID = 1693964441435822700L;
136144

137145
private final StorageOptions serviceOptions;
138146
private final BlobId source;
139147
private final Map<StorageRpc.Option, ?> sourceOptions;
148+
private final boolean overrideInfo;
140149
private final BlobInfo target;
141150
private final Map<StorageRpc.Option, ?> targetOptions;
142151
private final BlobInfo result;
@@ -150,6 +159,7 @@ static class StateImpl implements RestorableState<CopyWriter>, Serializable {
150159
this.serviceOptions = builder.serviceOptions;
151160
this.source = builder.source;
152161
this.sourceOptions = builder.sourceOptions;
162+
this.overrideInfo = builder.overrideInfo;
153163
this.target = builder.target;
154164
this.targetOptions = builder.targetOptions;
155165
this.result = builder.result;
@@ -165,6 +175,7 @@ static class Builder {
165175
private final StorageOptions serviceOptions;
166176
private final BlobId source;
167177
private final Map<StorageRpc.Option, ?> sourceOptions;
178+
private final boolean overrideInfo;
168179
private final BlobInfo target;
169180
private final Map<StorageRpc.Option, ?> targetOptions;
170181
private BlobInfo result;
@@ -175,11 +186,12 @@ static class Builder {
175186
private Long megabytesCopiedPerChunk;
176187

177188
private Builder(StorageOptions options, BlobId source,
178-
Map<StorageRpc.Option, ?> sourceOptions,
179-
BlobInfo target, Map<StorageRpc.Option, ?> targetOptions) {
189+
Map<StorageRpc.Option, ?> sourceOptions, boolean overrideInfo, BlobInfo target,
190+
Map<StorageRpc.Option, ?> targetOptions) {
180191
this.serviceOptions = options;
181192
this.source = source;
182193
this.sourceOptions = sourceOptions;
194+
this.overrideInfo = overrideInfo;
183195
this.target = target;
184196
this.targetOptions = targetOptions;
185197
}
@@ -220,15 +232,15 @@ RestorableState<CopyWriter> build() {
220232
}
221233

222234
static Builder builder(StorageOptions options, BlobId source,
223-
Map<StorageRpc.Option, ?> sourceOptions, BlobInfo target,
235+
Map<StorageRpc.Option, ?> sourceOptions, boolean overrideInfo, BlobInfo target,
224236
Map<StorageRpc.Option, ?> targetOptions) {
225-
return new Builder(options, source, sourceOptions, target, targetOptions);
237+
return new Builder(options, source, sourceOptions, overrideInfo, target, targetOptions);
226238
}
227239

228240
@Override
229241
public CopyWriter restore() {
230-
RewriteRequest rewriteRequest = new RewriteRequest(
231-
source.toPb(), sourceOptions, target.toPb(), targetOptions, megabytesCopiedPerChunk);
242+
RewriteRequest rewriteRequest = new RewriteRequest(source.toPb(), sourceOptions,
243+
overrideInfo, target.toPb(), targetOptions, megabytesCopiedPerChunk);
232244
RewriteResponse rewriteResponse = new RewriteResponse(rewriteRequest,
233245
result != null ? result.toPb() : null, blobSize, isDone, rewriteToken,
234246
totalBytesCopied);
@@ -237,8 +249,9 @@ public CopyWriter restore() {
237249

238250
@Override
239251
public int hashCode() {
240-
return Objects.hash(serviceOptions, source, sourceOptions, target, targetOptions, result,
241-
blobSize, isDone, megabytesCopiedPerChunk, rewriteToken, totalBytesCopied);
252+
return Objects.hash(serviceOptions, source, sourceOptions, overrideInfo, target,
253+
targetOptions, result, blobSize, isDone, megabytesCopiedPerChunk, rewriteToken,
254+
totalBytesCopied);
242255
}
243256

244257
@Override
@@ -253,6 +266,7 @@ public boolean equals(Object obj) {
253266
return Objects.equals(this.serviceOptions, other.serviceOptions)
254267
&& Objects.equals(this.source, other.source)
255268
&& Objects.equals(this.sourceOptions, other.sourceOptions)
269+
&& Objects.equals(this.overrideInfo, other.overrideInfo)
256270
&& Objects.equals(this.target, other.target)
257271
&& Objects.equals(this.targetOptions, other.targetOptions)
258272
&& Objects.equals(this.result, other.result)
@@ -267,10 +281,14 @@ public boolean equals(Object obj) {
267281
public String toString() {
268282
return MoreObjects.toStringHelper(this)
269283
.add("source", source)
284+
.add("overrideInfo", overrideInfo)
270285
.add("target", target)
271-
.add("isDone", isDone)
272-
.add("totalBytesRewritten", totalBytesCopied)
286+
.add("result", result)
273287
.add("blobSize", blobSize)
288+
.add("isDone", isDone)
289+
.add("rewriteToken", rewriteToken)
290+
.add("totalBytesCopied", totalBytesCopied)
291+
.add("megabytesCopiedPerChunk", megabytesCopiedPerChunk)
274292
.toString();
275293
}
276294
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy