如何使用具有WriteChannel和MD5检查功能的JAVA库将其上传到Google Storage

时间:2018-11-05 12:11:48

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

使用写入写程序通道的输出流时,如何对上传的内容执行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);迁移后,就会出现此问题。

1 个答案:

答案 0 :(得分:1)

Google云支持的答案

请勿创建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;
    }
}