【发布时间】:2021-04-21 22:55:59
【问题描述】:
我在 API 中使用 google/cloud-storage 包并成功将 pdf 文件上传到 Google Cloud Storage 存储桶。但是,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 存储桶?
感谢您的帮助。
【问题讨论】:
-
您是否撰写了问题中链接的文章?不错的文章。我不知道 DOMPDF 库,但 Google Cloud Storage PHP SDK 支持 PSR StreamInterface。如果您的 PDF 库支持
StreamInterface,那么您可以将它们链接在一起。 googleapis.github.io/google-cloud-php/#/docs/cloud-storage/… 和 github.com/php-fig/http-message/blob/1.0.1/src/… -
谢谢。是的,我写了这篇文章。
标签: laravel google-cloud-storage laravel-8