gpt4 book ai didi

google-cloud-platform - 如何使用带有 WriteChannel 和 MD5 检查的 JAVA 库上传到 Google Storage

转载 作者:行者123 更新时间:2023-12-04 01:22:20 31 4
gpt4 key购买 nike

在使用写入写入器 channel 的输出流时,如何对上传的内容执行 MD5 检查?当我执行 Storage::create 时,提供的 BlobInfo 中的 MD5 被 EMPTY_BYTE_ARRAY_MD5 覆盖。这是有道理的,因为最初创建 blob 时,确实是空的。但我希望编写器上的某些方法可以设置更新的 MD5,该 MD5 预计将在编写器关闭后有效。我找不到实现此目的的方法,这可能吗?我会附上我的代码:

分级:

api 'com.google.cloud:google-cloud-storage:1.51.0'

Java 代码:

import com.google.cloud.WriteChannel;
import com.google.cloud.storage.*;
import com.google.common.io.ByteStreams;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.Channels;
import java.nio.file.Path;

class StorageServiceImpl {

@Inject
private Storage storage;

public BlobInfo uploadFile(final Path localFile, final String bucketName, final String fileName, final String downloadFileName) throws IOException {
Blob blob = null;
String checksum = md5(localFile);
try (InputStream inputStream = new FileInputStream(localFile.toFile())) {
blob = storage.create(
BlobInfo.newBuilder(bucketName, fileName)
.setContentType("application/octet-stream")
.setContentDisposition(String.format("attachment; filename=\"%s\"", downloadFileName))
.setMd5(checksum)
.build()
);
try (WriteChannel writer = blob.writer(Storage.BlobWriteOption.md5Match())) {
ByteStreams.copy(inputStream, Channels.newOutputStream(writer));
}
} catch (StorageException ex) {
if (!(400 == ex.getCode() && "invalid".equals(ex.getReason()))) {
throw ex;
}
}
return blob;
}
}

一旦我们从已弃用的方法 Blob create(BlobInfo blobInfo, InputStream content, BlobWriteOption... options); 迁移,就会出现此问题。

最佳答案

来自 Google Cloud 支持的回答

不要创建 blob 并请求 blob 的写入器。相反,向存储请求写入器并向该请求提供 blob 信息。

import com.google.cloud.WriteChannel;
import com.google.cloud.storage.*;
import com.google.common.io.ByteStreams;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.Channels;
import java.nio.file.Path;

class StorageServiceImpl {

@Inject
private Storage storage;

public BlobInfo uploadFile(final Path localFile, final String bucketName, final String fileName, final String downloadFileName) throws IOException {
BlobInfo blobInfo = null;
String checksum = md5(localFile);
try (InputStream inputStream = new FileInputStream(localFile.toFile())) {
blobInfo =
BlobInfo.newBuilder(bucketName, fileName)
.setContentType("application/octet-stream")
.setContentDisposition(String.format("attachment; filename=\"%s\"", downloadFileName))
.setMd5(checksum)
.build();
try (WriteChannel writer = storage.writer(blobInfo, Storage.BlobWriteOption.md5Match())) {
ByteStreams.copy(inputStream, Channels.newOutputStream(writer));
}
} catch (StorageException ex) {
if (!(400 == ex.getCode() && "invalid".equals(ex.getReason()))) {
throw ex;
}
}
return blobInfo;
}
}

关于google-cloud-platform - 如何使用带有 WriteChannel 和 MD5 检查的 JAVA 库上传到 Google Storage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53154190/

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