首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >谷歌云存储md5检查文件

谷歌云存储md5检查文件
EN

Stack Overflow用户
提问于 2016-01-05 19:07:09
回答 1查看 1.4K关注 0票数 0

我想获得我的GCS文件的内容md5。

我试过这样做:

代码语言:javascript
复制
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和流,但这在技术上涉及两个网络呼叫。

然后我试了一下:

代码语言:javascript
复制
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形式出现.请帮帮忙。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-07 09:06:44

好的,我在看完文档后找到了解决方案。md5哈希位于头键中。以下是代码:

代码语言:javascript
复制
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());
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34619410

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档