Skip to content

Commit 6f4585e

Browse files
feat: add support of customTime metadata (#413)
* feat: support customTime metadata * feat: add javaDoc * feat: address few nits * feat: fix the api breaking change * Add exception to avoid breaking change * Add exception to avoid breaking change Co-authored-by: Jesse Lovelace <[email protected]>
1 parent 1af8288 commit 6f4585e

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,12 @@ Builder setCreateTime(Long createTime) {
434434
return this;
435435
}
436436

437+
@Override
438+
public Builder setCustomTime(Long customTime) {
439+
infoBuilder.setCustomTime(customTime);
440+
return this;
441+
}
442+
437443
@Override
438444
Builder setIsDirectory(boolean isDirectory) {
439445
infoBuilder.setIsDirectory(isDirectory);

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public StorageObject apply(BlobInfo blobInfo) {
8484
private final String etag;
8585
private final String md5;
8686
private final String crc32c;
87+
private final Long customTime;
8788
private final String mediaLink;
8889
private final Map<String, String> metadata;
8990
private final Long metageneration;
@@ -260,6 +261,26 @@ public abstract static class Builder {
260261
*/
261262
public abstract Builder setCrc32c(String crc32c);
262263

264+
/**
265+
* Sets the custom time for an object. Once set it can't be unset and only changed to a custom
266+
* datetime in the future. To unset the custom time, you must either perform a rewrite operation
267+
* or upload the data again.
268+
*
269+
* <p>Example of setting the custom time.
270+
*
271+
* <pre>{@code
272+
* String bucketName = "my-unique-bucket";
273+
* String blobName = "my-blob-name";
274+
* long customTime = 1598423868301L;
275+
* BlobInfo blob = BlobInfo.newBuilder(bucketName, blobName).setCustomTime(customTime).build();
276+
* }</pre>
277+
*/
278+
public Builder setCustomTime(Long customTime) {
279+
throw new UnsupportedOperationException(
280+
"Override setCustomTime with your own implementation,"
281+
+ " or use com.google.cloud.storage.Blob.");
282+
}
283+
263284
/**
264285
* Sets the CRC32C checksum of blob's data as described in <a
265286
* href="http://tools.ietf.org/html/rfc4960#appendix-B">RFC 4960, Appendix B;</a> from hex
@@ -325,6 +346,7 @@ static final class BuilderImpl extends Builder {
325346
private String selfLink;
326347
private String md5;
327348
private String crc32c;
349+
private Long customTime;
328350
private String mediaLink;
329351
private Map<String, String> metadata;
330352
private Long metageneration;
@@ -360,6 +382,7 @@ static final class BuilderImpl extends Builder {
360382
selfLink = blobInfo.selfLink;
361383
md5 = blobInfo.md5;
362384
crc32c = blobInfo.crc32c;
385+
customTime = blobInfo.customTime;
363386
mediaLink = blobInfo.mediaLink;
364387
metadata = blobInfo.metadata;
365388
metageneration = blobInfo.metageneration;
@@ -488,6 +511,12 @@ public Builder setCrc32c(String crc32c) {
488511
return this;
489512
}
490513

514+
@Override
515+
public Builder setCustomTime(Long customTime) {
516+
this.customTime = customTime;
517+
return this;
518+
}
519+
491520
@Override
492521
public Builder setCrc32cFromHexString(String crc32cHexString) {
493522
if (crc32cHexString == null) {
@@ -619,6 +648,7 @@ public BlobInfo build() {
619648
selfLink = builder.selfLink;
620649
md5 = builder.md5;
621650
crc32c = builder.crc32c;
651+
customTime = builder.customTime;
622652
mediaLink = builder.mediaLink;
623653
metadata = builder.metadata;
624654
metageneration = builder.metageneration;
@@ -857,6 +887,11 @@ public Long getCreateTime() {
857887
return createTime;
858888
}
859889

890+
/** Returns the custom time specified by the user for an object. */
891+
public Long getCustomTime() {
892+
return customTime;
893+
}
894+
860895
/**
861896
* Returns {@code true} if the current blob represents a directory. This can only happen if the
862897
* blob is returned by {@link Storage#list(String, Storage.BlobListOption...)} when the {@link
@@ -1002,6 +1037,9 @@ public ObjectAccessControl apply(Acl acl) {
10021037
if (createTime != null) {
10031038
storageObject.setTimeCreated(new DateTime(createTime));
10041039
}
1040+
if (customTime != null) {
1041+
storageObject.setCustomTime(new DateTime(customTime));
1042+
}
10051043
if (size != null) {
10061044
storageObject.setSize(BigInteger.valueOf(size));
10071045
}
@@ -1125,6 +1163,9 @@ static BlobInfo fromPb(StorageObject storageObject) {
11251163
if (storageObject.getTimeCreated() != null) {
11261164
builder.setCreateTime(storageObject.getTimeCreated().getValue());
11271165
}
1166+
if (storageObject.getCustomTime() != null) {
1167+
builder.setCustomTime(storageObject.getCustomTime().getValue());
1168+
}
11281169
if (storageObject.getSize() != null) {
11291170
builder.setSize(storageObject.getSize().longValue());
11301171
}

google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public class BlobInfoTest {
6767
private static final Long SIZE = 1024L;
6868
private static final Long UPDATE_TIME = DELETE_TIME - 1L;
6969
private static final Long CREATE_TIME = UPDATE_TIME - 1L;
70+
private static final Long CUSTOM_TIME = CREATE_TIME - 1L;
7071
private static final String ENCRYPTION_ALGORITHM = "AES256";
7172
private static final String KEY_SHA256 = "keySha";
7273
private static final CustomerEncryption CUSTOMER_ENCRYPTION =
@@ -101,6 +102,7 @@ public class BlobInfoTest {
101102
.setSize(SIZE)
102103
.setUpdateTime(UPDATE_TIME)
103104
.setCreateTime(CREATE_TIME)
105+
.setCustomTime(CUSTOM_TIME)
104106
.setStorageClass(STORAGE_CLASS)
105107
.setKmsKeyName(KMS_KEY_NAME)
106108
.setEventBasedHold(EVENT_BASED_HOLD)
@@ -195,6 +197,7 @@ public void testBuilder() {
195197
assertEquals(SIZE, BLOB_INFO.getSize());
196198
assertEquals(UPDATE_TIME, BLOB_INFO.getUpdateTime());
197199
assertEquals(CREATE_TIME, BLOB_INFO.getCreateTime());
200+
assertEquals(CUSTOM_TIME, BLOB_INFO.getCustomTime());
198201
assertEquals(STORAGE_CLASS, BLOB_INFO.getStorageClass());
199202
assertEquals(KMS_KEY_NAME, BLOB_INFO.getKmsKeyName());
200203
assertEquals(EVENT_BASED_HOLD, BLOB_INFO.getEventBasedHold());
@@ -214,6 +217,7 @@ public void testBuilder() {
214217
assertNull(DIRECTORY_INFO.getCrc32c());
215218
assertNull(DIRECTORY_INFO.getCrc32cToHexString());
216219
assertNull(DIRECTORY_INFO.getCreateTime());
220+
assertNull(DIRECTORY_INFO.getCustomTime());
217221
assertNull(DIRECTORY_INFO.getDeleteTime());
218222
assertNull(DIRECTORY_INFO.getEtag());
219223
assertNull(DIRECTORY_INFO.getGeneration());
@@ -257,6 +261,7 @@ private void compareBlobs(BlobInfo expected, BlobInfo value) {
257261
assertEquals(expected.getOwner(), value.getOwner());
258262
assertEquals(expected.getSelfLink(), value.getSelfLink());
259263
assertEquals(expected.getSize(), value.getSize());
264+
assertEquals(expected.getCustomTime(), value.getCustomTime());
260265
assertEquals(expected.getUpdateTime(), value.getUpdateTime());
261266
assertEquals(expected.getStorageClass(), value.getStorageClass());
262267
assertEquals(expected.getKmsKeyName(), value.getKmsKeyName());
@@ -299,6 +304,7 @@ public void testToPbAndFromPb() {
299304
assertNull(blobInfo.getCrc32c());
300305
assertNull(blobInfo.getCrc32cToHexString());
301306
assertNull(blobInfo.getCreateTime());
307+
assertNull(blobInfo.getCustomTime());
302308
assertNull(blobInfo.getDeleteTime());
303309
assertNull(blobInfo.getEtag());
304310
assertNull(blobInfo.getGeneration());

google-cloud-storage/src/test/java/com/google/cloud/storage/BlobTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public class BlobTest {
9191
private static final Long SIZE = 1024L;
9292
private static final Long UPDATE_TIME = DELETE_TIME - 1L;
9393
private static final Long CREATE_TIME = UPDATE_TIME - 1L;
94+
private static final Long CUSTOM_TIME = CREATE_TIME - 1L;
9495
private static final String ENCRYPTION_ALGORITHM = "AES256";
9596
private static final String KEY_SHA256 = "keySha";
9697
private static final BlobInfo.CustomerEncryption CUSTOMER_ENCRYPTION =
@@ -122,6 +123,7 @@ public class BlobTest {
122123
.setSize(SIZE)
123124
.setUpdateTime(UPDATE_TIME)
124125
.setCreateTime(CREATE_TIME)
126+
.setCustomTime(CUSTOM_TIME)
125127
.setCustomerEncryption(CUSTOMER_ENCRYPTION)
126128
.setKmsKeyName(KMS_KEY_NAME)
127129
.setEventBasedHold(EVENT_BASED_HOLD)
@@ -510,6 +512,7 @@ public void testBuilder() {
510512
.setContentLanguage(CONTENT_LANGUAGE)
511513
.setCrc32c(CRC32)
512514
.setCreateTime(CREATE_TIME)
515+
.setCustomTime(CUSTOM_TIME)
513516
.setCustomerEncryption(CUSTOMER_ENCRYPTION)
514517
.setKmsKeyName(KMS_KEY_NAME)
515518
.setEventBasedHold(EVENT_BASED_HOLD)
@@ -539,6 +542,7 @@ public void testBuilder() {
539542
assertEquals(CRC32, blob.getCrc32c());
540543
assertEquals(CRC32_HEX_STRING, blob.getCrc32cToHexString());
541544
assertEquals(CREATE_TIME, blob.getCreateTime());
545+
assertEquals(CUSTOM_TIME, blob.getCustomTime());
542546
assertEquals(CUSTOMER_ENCRYPTION, blob.getCustomerEncryption());
543547
assertEquals(KMS_KEY_NAME, blob.getKmsKeyName());
544548
assertEquals(EVENT_BASED_HOLD, blob.getEventBasedHold());
@@ -589,6 +593,7 @@ public void testBuilder() {
589593
assertNull(blob.getSelfLink());
590594
assertEquals(0L, (long) blob.getSize());
591595
assertNull(blob.getUpdateTime());
596+
assertNull(blob.getCustomTime());
592597
assertTrue(blob.isDirectory());
593598
}
594599

google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,9 +527,11 @@ public void testUpdateBucketDefaultKmsKeyName() throws ExecutionException, Inter
527527
@Test
528528
public void testCreateBlob() {
529529
String blobName = "test-create-blob";
530-
BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
530+
BlobInfo blob =
531+
BlobInfo.newBuilder(BUCKET, blobName).setCustomTime(System.currentTimeMillis()).build();
531532
Blob remoteBlob = storage.create(blob, BLOB_BYTE_CONTENT);
532533
assertNotNull(remoteBlob);
534+
assertNotNull(remoteBlob.getCustomTime());
533535
assertEquals(blob.getBucket(), remoteBlob.getBucket());
534536
assertEquals(blob.getName(), remoteBlob.getName());
535537
byte[] readBytes = storage.readAllBytes(BUCKET, blobName);

0 commit comments

Comments
 (0)