Laravel:如何将 pdf 文件直接上传到 Google Cloud Storage 存储桶而不先在本地保存

Laravel: how to upload pdf file directly to Google Cloud Storage bucket without first saving it locally

我在 API 中使用 google/cloud-storage 包并成功将 pdf 文件上传到 Google 云存储桶。但是,pdf 文件在上传到 Google Cloud Storage 存储桶之前首先保存在本地。

如何跳过将它们保存在本地,而是直接将它们上传到 Google Cloud Storage 存储桶?我计划在 Google App Engine 上托管 API。 这是它的post

这是我目前正在做的事情:

$filename = $request['firstname'] . '.pdf';
$fileStoragePath = '/storage/pdf/' . $filename;
$publicPath = public_path($fileStoragePath);

$pdf = App::make('dompdf.wrapper');
$pdf->loadView('pdfdocument', $validatedData)
$pdf->save($publicPath); 

$googleConfigFile = file_get_contents(config_path('googlecloud.json'));
$storage = new StorageClient([
    'keyFile' => json_decode($googleConfigFile, true)
]);
$storageBucketName = config('googlecloud.storage_bucket');
$bucket = $storage->bucket($storageBucketName);
$fileSource = fopen($publicPath, 'r');
$newFolderName = $request['firstname'].'_'.date("Y-m-d").'_'.date("H:i:s");
$googleCloudStoragePath = $newFolderName.'/'.$filename;

/*
*  Upload a file to the bucket.
*  Using Predefined ACLs to manage object permissions, you may
*  upload a file and give read access to anyone with the URL.
*/
$bucket->upload($fileSource, [
    'predefinedAcl' => 'publicRead',
    'name' => $googleCloudStoragePath
]);

是否可以在不先将文件保存在本地的情况下将文件上传到 Google Cloud Storage 存储桶?

感谢您的帮助。

我没有验证这个代码,但是 class PDF 成员 output() returns 一个字符串。

$pdf = App::make('dompdf.wrapper');
$pdf->loadView('pdfdocument', $validatedData)

...

$bucket->upload($pdf->output(), [
    'predefinedAcl' => 'publicRead',
    'name' => $googleCloudStoragePath
]);

你可以简单的客户端代码。替换:

$googleConfigFile = file_get_contents(config_path('googlecloud.json'));
$storage = new StorageClient([
    'keyFile' => json_decode($googleConfigFile, true)
]);

$storage = new StorageClient([
    'keyFilePath' => config_path('googlecloud.json')
]);