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

Google cloud platform 如何使用带有WriteChannel和MD5检查的JAVA库上传到Google存储,google-cloud-platform,google-cloud-storage,Google Cloud Platform,Google Cloud Storage,在使用写入写入器通道的输出流时,如何对上载内容执行MD5检查?当我执行Storage::create时,所提供BlobInfo中的MD5将被空字节数组\u MD5覆盖。这是有意义的,因为在最初创建blob时,它确实是空的。但是我希望writer上的某些方法能够设置一个更新的MD5,该MD5在writer关闭后应该是有效的。我找不到一个方法来实现这一点,有可能吗?我将附上我的代码: 格拉德尔: api 'com.google.cloud:google-cloud-storage:1.51.0'

在使用写入写入器通道的输出流时,如何对上载内容执行MD5检查?当我执行
Storage::create
时,所提供BlobInfo中的MD5将被
空字节数组\u MD5
覆盖。这是有意义的,因为在最初创建blob时,它确实是空的。但是我希望writer上的某些方法能够设置一个更新的MD5,该MD5在writer关闭后应该是有效的。我找不到一个方法来实现这一点,有可能吗?我将附上我的代码:

格拉德尔:

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)迁移时,这个问题就出现了

来自谷歌云支持的回答

不要创建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;
    }
}

在分块上传中额外存储哈希值的非常好的方法。