gpt4 book ai didi

java - 谷歌云存储md5检查文件

转载 作者:行者123 更新时间:2023-12-01 10:37:07 24 4
gpt4 key购买 nike

我想获取我的 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());

这是一个单一的网络调用,但哈希值为空......请帮忙。

最佳答案

好的,我在查看文档后找到了解决方案。md5 哈希值位于 x-goog-hash header 键中。这是代码:

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());

关于java - 谷歌云存储md5检查文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34619410/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com