我想获取我的GCS文件的内容md5。
我试着这样做:
Storage.Objects.Get get = storage.objects().get(BUCKET_NAME_MUSIC_DATA, fileName);
serverHash = get.execute().getMd5Hash();
bufferedInputStream = new BufferedInputStream(get.executeMediaAsInputStream());
//process the stream
我得到了md5和流,但这在技术上涉及2个网络呼叫。
然后我尝试了这个:
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());
这是一个单一的网络调用,但哈希值为null .... Plz帮助。
答案 0 :(得分:1)
好的,我在完成文档后找到了解决方案。 md5哈希位于x-goog-hash标头密钥中。 这是代码:
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());