0

我正在使用https://zatackcoder.com/upload-file-to-google-cloud-storage-using-php/上的代码示例将文件上传到 Google Cloud。一切正常(我是新手,所以我很高兴让它运行!),但我最近读了一个简介,你可以告诉谷歌云你上传的文件的 md5 是什么,谷歌会验证与实际上传相反,如果存在传输问题导致不同的 md5,则上传失败。

我的问题是两部分:

  1. 如何以 Google Cloud 可以理解的格式计算 md5?我相信计算实际上是从 PHP 的 file_md5() 返回的 md5 的 base64_encode() 但是当我这样做时,结果字符串实际上与从谷歌返回的结果 "md5Hash": "TPmaCjp5uh1jxIQahhOAsQ==" 有很大不同. 我正在使用 Google Cloud STORAGE API 执行上传(请参阅上面的链接和下面的次要代码片段)。

  2. 一旦我有了正确计算的 md5Hash,我如何在 upload() 函数中指定它以将该值作为上传的一部分传递?有没有人这样做并且可以分享他们的专业知识?

非常感谢!

这是上面链接中较大项目的片段。上传工作正常,我只是在寻找如何生成适当的 md5Hash 并将该哈希包含在上传中。

function uploadFile($bucketName, $fileContent, $cloudPath) {
    $privateKeyFileContent = $GLOBALS['privateKeyFileContent'];
    // connect to Google Cloud Storage using private key as authentication
    try {
        $storage = new StorageClient([
            'keyFile' => json_decode($privateKeyFileContent, true)
        ]);
    } catch (Exception $e) {
        // maybe invalid private key ?
        print $e;
        return false;
    }

    // set which bucket to work in
    $bucket = $storage->bucket($bucketName);

    // upload/replace file 
    $storageObject = $bucket->upload(
            $fileContent,
            ['name' => $cloudPath]
            // if $cloudPath is existed then will be overwrite without confirmation
            // NOTE: 
            // a. do not put prefix '/', '/' is a separate folder name  !!
            // b. private key MUST have 'storage.objects.delete' permission if want to replace file !
    );

    // is it succeed ?
    return $storageObject != null;
}

4

1 回答 1

0

根据Cloud Storage 文档的 PHP 客户端库,您可以在对象的upload方法StorageObject中指定metadata参数。

这个参数是一个允许你传递 md5Hash 的数组。您可以通过的属性的完整列表在此处说明。

于 2021-03-09T12:09:53.077 回答