Skip to content

Commit 21fb8fe

Browse files
committed
chore: added crc to upload part
1 parent feb1912 commit 21fb8fe

File tree

2 files changed

+54
-15
lines changed

2 files changed

+54
-15
lines changed

google-cloud-storage/src/main/java/com/google/cloud/storage/ChecksumResponseParser.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,21 @@
2222
import java.io.IOException;
2323
import java.util.Arrays;
2424
import java.util.Collections;
25+
import java.util.List;
2526
import java.util.Map;
26-
import java.util.Optional;
2727
import java.util.stream.Collectors;
2828

29-
/** A utility class to parse {@link HttpResponse} and create a {@link UploadPartResponse}. */
29+
/** A utility class to parse checksums from an {@link HttpResponse}. */
3030
final class ChecksumResponseParser {
3131

32+
private static final String X_GOOG_HASH = "x-goog-hash";
33+
3234
private ChecksumResponseParser() {}
3335

3436
static UploadPartResponse parseUploadResponse(HttpResponse response) {
3537
String eTag = response.getHeaders().getETag();
3638
Map<String, String> hashes = extractHashesFromHeader(response);
37-
return UploadPartResponse.builder().eTag(eTag).md5(hashes.get("md5")).build();
39+
return UploadPartResponse.builder().eTag(eTag).md5(hashes.get("md5")).crc32c(hashes.get("crc32c")).build();
3840
}
3941

4042
static CompleteMultipartUploadResponse parseCompleteResponse(HttpResponse response)
@@ -52,14 +54,18 @@ static CompleteMultipartUploadResponse parseCompleteResponse(HttpResponse respon
5254
}
5355

5456
static Map<String, String> extractHashesFromHeader(HttpResponse response) {
55-
return Optional.ofNullable(response.getHeaders().getFirstHeaderStringValue("x-goog-hash"))
56-
.map(
57-
h ->
58-
Arrays.stream(h.split(","))
59-
.map(s -> s.trim().split("=", 2))
60-
.filter(a -> a.length == 2)
61-
.filter(a -> "crc32c".equalsIgnoreCase(a[0]) || "md5".equalsIgnoreCase(a[0]))
62-
.collect(Collectors.toMap(a -> a[0].toLowerCase(), a -> a[1], (v1, v2) -> v1)))
63-
.orElse(Collections.emptyMap());
57+
List<String> hashHeaders = response.getHeaders().getHeaderStringValues(X_GOOG_HASH);
58+
if (hashHeaders == null || hashHeaders.isEmpty()) {
59+
return Collections.emptyMap();
60+
}
61+
62+
return hashHeaders.stream()
63+
.flatMap(h -> Arrays.stream(h.split(",")))
64+
.map(String::trim)
65+
.filter(s -> !s.isEmpty())
66+
.map(s -> s.split("=", 2))
67+
.filter(a -> a.length == 2)
68+
.filter(a -> "crc32c".equalsIgnoreCase(a[0]) || "md5".equalsIgnoreCase(a[0]))
69+
.collect(Collectors.toMap(a -> a[0].toLowerCase(), a -> a[1], (v1, v2) -> v1));
6470
}
6571
}

google-cloud-storage/src/main/java/com/google/cloud/storage/multipartupload/model/UploadPartResponse.java

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ public final class UploadPartResponse {
3131

3232
private final String eTag;
3333
private final String md5;
34+
private final String crc32c;
3435

3536
private UploadPartResponse(Builder builder) {
3637
this.eTag = builder.etag;
3738
this.md5 = builder.md5;
39+
this.crc32c = builder.crc32c;
3840
}
3941

4042
/**
@@ -59,6 +61,17 @@ public String md5() {
5961
return md5;
6062
}
6163

64+
/**
65+
* Returns the CRC32C checksum of the uploaded part.
66+
*
67+
* @return The CRC32C checksum.
68+
* @since 2.60.0 This new api is in preview and is subject to breaking changes.
69+
*/
70+
@BetaApi
71+
public String crc32c() {
72+
return crc32c;
73+
}
74+
6275
@Override
6376
public boolean equals(Object o) {
6477
if (this == o) {
@@ -68,17 +81,23 @@ public boolean equals(Object o) {
6881
return false;
6982
}
7083
UploadPartResponse that = (UploadPartResponse) o;
71-
return Objects.equals(eTag, that.eTag) && Objects.equals(md5, that.md5);
84+
return Objects.equals(eTag, that.eTag)
85+
&& Objects.equals(md5, that.md5)
86+
&& Objects.equals(crc32c, that.crc32c);
7287
}
7388

7489
@Override
7590
public int hashCode() {
76-
return Objects.hash(eTag, md5);
91+
return Objects.hash(eTag, md5, crc32c);
7792
}
7893

7994
@Override
8095
public String toString() {
81-
return MoreObjects.toStringHelper(this).add("etag", eTag).add("md5", md5).toString();
96+
return MoreObjects.toStringHelper(this)
97+
.add("etag", eTag)
98+
.add("md5", md5)
99+
.add("crc32c", crc32c)
100+
.toString();
82101
}
83102

84103
/**
@@ -101,6 +120,7 @@ public static Builder builder() {
101120
public static class Builder {
102121
private String etag;
103122
private String md5;
123+
private String crc32c;
104124

105125
private Builder() {}
106126

@@ -130,6 +150,19 @@ public Builder md5(String md5) {
130150
return this;
131151
}
132152

153+
/**
154+
* Sets the CRC32C checksum for the uploaded part.
155+
*
156+
* @param crc32c The CRC32C checksum.
157+
* @return This builder.
158+
* @since 2.60.0 This new api is in preview and is subject to breaking changes.
159+
*/
160+
@BetaApi
161+
public Builder crc32c(String crc32c) {
162+
this.crc32c = crc32c;
163+
return this;
164+
}
165+
133166
/**
134167
* Builds the {@code UploadPartResponse} object.
135168
*

0 commit comments

Comments
 (0)