Correct the API used to fetch the version for a GCS object (#16097)

Current API used to fetch the version for a GCS object is incorrect. This PR fixes that API.
This commit is contained in:
Vishesh Garg 2024-03-11 18:30:34 +05:30 committed by GitHub
parent c7f1872bd1
commit 2dd8b16467
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 6 deletions

View File

@ -198,13 +198,22 @@ public class GoogleStorage
return blob.getSize();
}
/**
* Return the etag for an object. This is a value that changes whenever the object's data or metadata changes and is
* typically but not always the MD5 hash of the object. Ref:
* <a href="https://cloud.google.com/storage/docs/hashes-etags#etags">ETags</a>
* @param bucket
* @param path
* @return
* @throws IOException
*/
public String version(final String bucket, final String path) throws IOException
{
Blob blob = storage.get().get(bucket, path, Storage.BlobGetOption.fields(Storage.BlobField.GENERATION));
Blob blob = storage.get().get(bucket, path, Storage.BlobGetOption.fields(Storage.BlobField.ETAG));
if (blob == null) {
throw new IOE("Failed to fetch google cloud storage object from bucket [%s] and path [%s].", bucket, path);
}
return blob.getGeneratedId();
return blob.getEtag();
}
/***

View File

@ -76,7 +76,7 @@ public class GoogleTimestampVersionedDataFinder extends GoogleDataSegmentPuller
return latest;
}
catch (IOException e) {
throw new RuntimeException();
throw new RuntimeException(e);
}
}
}

View File

@ -203,18 +203,18 @@ public class GoogleStorageTest
@Test
public void testVersion() throws IOException
{
final String version = "7";
final String etag = "abcd";
EasyMock.expect(mockStorage.get(
EasyMock.eq(BUCKET),
EasyMock.eq(PATH),
EasyMock.anyObject(Storage.BlobGetOption.class)
)).andReturn(blob);
EasyMock.expect(blob.getGeneratedId()).andReturn(version);
EasyMock.expect(blob.getEtag()).andReturn(etag);
EasyMock.replay(mockStorage, blob);
assertEquals(version, googleStorage.version(BUCKET, PATH));
assertEquals(etag, googleStorage.version(BUCKET, PATH));
}
@Test