【问题标题】:Google cloud storage md5 check on file谷歌云存储 md5 检查文件
【发布时间】:2016-01-07 17:06:44
【问题描述】:

我想获取我的 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.... 请帮忙。

【问题讨论】:

标签: java google-cloud-storage google-cloud-platform


【解决方案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());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-10
    • 2016-10-12
    • 1970-01-01
    • 2017-05-24
    • 2019-02-16
    相关资源
    最近更新 更多