firebase存储并从php上传

时间:2016-07-04 11:38:34

标签: php firebase firebase-storage

我有一个php应用程序,我需要上传图片到谷歌firebase storage。我曾经看过写入和读取实时数据库的片段,但无法找到从php上传到firebase storage的任何代码。

非常感谢任何帮助。

Tismon Varghese

2 个答案:

答案 0 :(得分:1)

我们建议使用Google API Client PHP library,因为Firebase存储由Google云端存储支持。 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 :(得分:1)

您可以使用他们的官方文档开始 The storage documentationAn easier to use SDK within a Framework 您可以探索两者以获取其他信息。所以开始我是如何做我的。不过我当时正在做一个 Laravel 项目。

你必须安装这个包composer require kreait/firebase-php

然后阅读 Package Setup

中的设置步骤
     $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;

我希望这会有所帮助。