Firebase存储和从PHP上传

3
我有一个PHP应用程序,需要将图片上传到Google的 Firebase存储中。我看过一些写入和读取实时数据库的代码片段,但找不到任何从PHP上传到 Firebase存储的代码。
非常感谢您的任何帮助。
Tismon Varghese
2个回答

4

您可以使用官方文档开始使用,文档链接如下:云存储文档易于在框架内使用的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;

我希望这可以帮到你。

0

我们建议使用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());
}

1
这段代码片段用于读取已上传的文件。那么如何将新图像上传到Firebase存储?找不到任何示例。 - Firze
我想在核心PHP项目中使用Firebase存储,我尝试使用上面给出的示例,但是它显示错误。 - Devi A

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接