谷歌' gsutil cp' command对传输的文件运行校验和。 Google的Java存储API是否也这样做?

时间:2016-09-19 16:46:12

标签: java google-cloud-storage gsutil

来自documentation on gsutil

"在每次上传或下载结束时,gsutil cp命令验证它为源文件/对象计算的校验和是否与服务计算的校验和相匹配。如果校验和不匹配,gsutil将删除损坏的对象并打印警告消息。"

这里有一些sample code使用他们的java存储API:

public static void uploadFile(String name, String contentType, File file, String bucketName)
    throws IOException, GeneralSecurityException {
    InputStreamContent contentStream = new InputStreamContent(
            contentType, new FileInputStream(file));
    // Setting the length improves upload performance
    contentStream.setLength(file.length());
    StorageObject objectMetadata = new StorageObject()
        // Set the destination object name
        .setName(name)
        // Set the access control list to publicly read-only
        .setAcl(Arrays.asList(
            new ObjectAccessControl().setEntity("allUsers").setRole("READER")));

    // Do the insert
    Storage client = StorageFactory.getService();
    Storage.Objects.Insert insertRequest = client.objects().insert(
            bucketName, objectMetadata, contentStream);

    insertRequest.execute();
}

是否需要执行校验和?我应该手动完成吗?

1 个答案:

答案 0 :(得分:3)

我们建议在所有上传和下载中计算校验和,并验证您计算的校验和是否与服务所具有的校验和相匹配。这样做可以保护您免受客户端或客户端库错误的影响,这些错误可能会破坏往/来自云端的数据。

我将打开一个错误来更新该示例代码,包括计算校验和。

相关问题