简体   繁体   English

谷歌云存储MD5检查文件

[英]Google cloud storage md5 check on file

I want to get the content md5 of my GCS file. 我想获取我的GCS文件的内容md5。

I tried to do this : 我试图这样做:

Storage.Objects.Get get = storage.objects().get(BUCKET_NAME_MUSIC_DATA, fileName);
serverHash = get.execute().getMd5Hash();
bufferedInputStream = new BufferedInputStream(get.executeMediaAsInputStream());
//process the stream

I get the md5 and the stream, but this technically involves 2 network calls. 我得到了md5和流,但这从技术上讲涉及2个网络调用。

Then I tried this : 然后我尝试了这个:

Storage.Objects.Get get = storage.objects().get(BUCKET_NAME_MUSIC_DATA, fileName);
HttpResponse httpResponse = get.executeMedia();
serverHash = httpResponse.getHeaders().getContentMD5();
bufferedInputStream = new BufferedInputStream(httpResponse.getContent());

This is a single network call, but the hash is coming as null.... Plz help. 这是一个单一的网络调用,但是哈希为null。...Plz帮助。

Ok I found a solution after going through the docs. 好的,我在浏览了文档后找到了解决方案。 The md5 hash is in the x-goog-hash header key. md5哈希值位于x-goog-hash标头键中。 Here is the code : 这是代码:

Storage.Objects.Get get = storage.objects().get(BUCKET_NAME_APP_DATA, fileName);
HttpResponse httpResponse = get.executeMedia();
String x_goog_hash = httpResponse.getHeaders().get("x-goog-hash").toString().replaceAll("\\s+","");
if (TextUtils.isEmpty(x_goog_hash))
    throw new IOException("Response x-goog-hash was null");

final int start = x_goog_hash.indexOf("md5=");
final int end = x_goog_hash.indexOf("==", start);

if (start == -1 || end == -1 || end <= start)
    throw new IOException("Response x-goog-hash of unexpected type " + x_goog_hash);

serverHash = x_goog_hash.substring(start + 4, end + 2);
if (TextUtils.isEmpty(serverHash))
    throw new IOException("Response md5 was null");

Log.i("Ayush", "Md5 hash = ? " + serverHash);
bufferedInputStream = new BufferedInputStream(httpResponse.getContent());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM