我在一个应用程序接口中使用了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存储桶?
谢谢你的帮助。
发布于 2021-04-22 11:14:55
我还没有验证过这段代码,但是类PDF成员output()返回了一个字符串。
$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')
]);https://stackoverflow.com/questions/67204491
复制相似问题