简体   繁体   English

只有14个字节上载到Google Cloud Storage

[英]Only 14 bytes are uploaded into Google Cloud Storage

I'm trying to upload some files into Google Cloud Storage using PHP. 我正在尝试使用PHP将一些文件上传到Google Cloud Storage。 It seems to be partially working, since I'm seeing the files in the web console, however I've noticed that all files are only 14 bytes in size, while the original is much bigger. 由于似乎在Web控制台中看到了文件,因此它似乎部分起作用,但是我注意到所有文件的大小仅为14字节,而原始文件更大。

This is the code I'm using: 这是我正在使用的代码:

/**
 *
 */
public static function saveFile($originalFile, $newFilename) {

    $bucket = MyStorage::getBucket();

    if ($bucket == null) {

        // Save locally
        Storage::disk('local')->put(FileController::$FILE_DIR . $newFilename, file_get_contents($originalFile));

    } else {

        // Save to Google Cloud Platform
        $bucket->upload($originalFile->getRealPath(), ['name' => $newFilename]);

    }
}

/**
 *
 */
private static function getBucket() {

    $appEnv = getenv('APP_ENV');

    if ($appEnv == "production" || $appEnv == "test") {

        $GOOGLE_CLOUD_PROJECT_ID = getenv('GOOGLE_PROJECT_ID');
        $GOOGLE_CLOUD_BUCKET_NAME = getenv('GOOGLE_BUCKET_NAME');

        $storage = new StorageClient(['projectId' => $GOOGLE_CLOUD_PROJECT_ID]);
        $bucket = $storage->bucket($GOOGLE_CLOUD_BUCKET_NAME);

        return $bucket;

    } else {

        return null;
    }
}

$originalFile->getRealPath() returns something like /tmp/asdfasdfadf and I've verified that that file exists and is a reasonable size. $originalFile->getRealPath()返回类似/tmp/asdfasdfadf $originalFile->getRealPath()并且我已验证该文件存在并且大小合理。 The $newFilename variable name is what I want it to be stored as and this part seems to work as I'm seeing that filename in the web console. $newFilename变量名是我希望将其存储为的名称,而这部分在我在Web控制台中看到该文件名时似乎可以正常工作。

Any ideas what could be happening? 有什么想法会发生什么吗?

It seems you're only uploading the path to the file as a string, as opposed to the actual file. 看来您只是将路径上传为文件的字符串,而不是实际的文件。

If you look at the client library, they seem to use fopen 如果您查看客户端库, 它们似乎使用了fopen

$bucket->upload( fopen($originalFile->getRealPath(), 'rb'), ['name' => $newFilename]);

Yes for using file upload, you need to read the stream of bytes to send it to remote cloud. 是的,对于使用文件上传,您需要读取字节流以将其发送到远程云。 fopen will do this job for you, to create stream of bytes. fopen将为您完成这项工作,以创建字节流。

else {

    // Save to Google Cloud Platform
    $bucket->upload($originalFile->getRealPath(), ['name' => $newFilename]);

}

Should be: 应该:

else {

    // Save to Google Cloud Platform
    $bucket->upload(fopen($originalFile->getRealPath(), 'rb'), ['name' => $newFilename]);

    if( fclose($originalFile->getRealPath()) === false){
       // Success
    } else{
       // Failure
    }


}

The reason that your code didn't work is because you were uploading only the filename to Google Cloud. 您的代码无效的原因是因为您仅将文件名上传到Google Cloud。 "/tmp/asdfasd" is 14 characters. “ / tmp / asdfasd”为14个字符。

Here is an answer on why the 'b' is important for the fopen command. 是为什么'b'对于fopen命令很重要的答案。

FClose closes the resource, preventing data leaks. FClose关闭资源,防止数据泄漏。 Also, check the return value from fclose(), as some OS only flush file output. 另外,检查fclose()的返回值,因为某些OS仅刷新文件输出。 Checking the return value can save severe data leaks. 检查返回值可以节省严重的数据泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM