我有一个PHP应用程序,需要将图片上传到Google的
非常感谢您的任何帮助。
Tismon Varghese
Firebase存储
中。我看过一些写入和读取实时数据库的代码片段,但找不到任何从PHP上传到 Firebase存储
的代码。非常感谢您的任何帮助。
Tismon Varghese
Firebase存储
中。我看过一些写入和读取实时数据库的代码片段,但找不到任何从PHP上传到 Firebase存储
的代码。您可以使用官方文档开始使用,文档链接如下:云存储文档和易于在框架内使用的SDK。您可以查看这两个文档以获取其他信息。我是在 Laravel 项目中进行操作的。
您需要安装此软件包:composer require kreait/firebase-php
然后还需遵循这些设置步骤:软件包设置
$storage = app('firebase.storage'); // This is an instance of Google\Cloud\Storage\StorageClient from kreait/firebase-php library
$defaultBucket = $storage->getBucket();
$image = $request->file('image');
$name = (string) Str::uuid().".".$image->getClientOriginalExtension(); // use Illuminate\Support\Str;
$pathName = $image->getPathName();
$file = fopen($pathName, 'r');
$object = $defaultBucket->upload($file, [
'name' => $name,
'predefinedAcl' => 'publicRead'
]);
$image_url = 'https://storage.googleapis.com/'.env('FIREBASE_PROJECT_ID').'.appspot.com/'.$name;
我们建议使用Google API Client PHP library,因为Firebase Storage是由Google Cloud Storage支持的。但是Firebase Storage并没有提供用于前端代码的PHP客户端。
从GCS PHP example code中可以看到:
// composer autoloading
require_once __DIR__ . '/vendor/autoload.php';
// grab the first argument
if (empty($argv[1])) {
die("usage: php listBuckets [project_id]\n");
}
$projectId = $argv[1];
// Authenticate your API Client
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Google_Service_Storage::DEVSTORAGE_FULL_CONTROL);
$storage = new Google_Service_Storage($client);
/**
* Google Cloud Storage API request to retrieve the list of buckets in your project.
*/
$buckets = $storage->buckets->listBuckets($projectId);
foreach ($buckets['items'] as $bucket) {
printf("%s\n", $bucket->getName());
}