How to upload image to Firestore Storage using PHP

Viewed 183

I'm having a trouble with uploading image to Firestore Storage. Here is the problem:

Fatal error: Uncaught Google\Cloud\Core\Exception\ServiceException:
{
  "error": {
    "code": 403,
    "message": "firebase-adminsdk-5jy3x@carimakan-bd835.iam.gserviceaccount.com does not have storage.objects.create access to images/test.jpeg.",
    "errors": [
      {
        "message": "firebase-adminsdk-5jy3x@carimakan-bd835.iam.gserviceaccount.com does not have storage.objects.create access to images/test.jpeg.",
        "domain": "global",
        "reason": "forbidden"
      }
    ]
  }
} in C:\xampp\htdocs\cariMakan\vendor\google\cloud-core\src\RequestWrapper.php:368
Stack trace:
#0 C:\xampp\htdocs\cariMakan\vendor\google\cloud-core\src\RequestWrapper.php(207): Google\Cloud\Core\RequestWrapper->convertToGoogleException(Object(GuzzleHttp\Exception\ClientException))
#1 C:\xampp\htdocs\cariMakan\vendor\google\cloud-core\src\Upload\MultipartUploader.php(44): Google\Cloud\Core\RequestWrapper->send(Object(GuzzleHttp\Psr7\Request), Array)
#2 C:\xampp\htdocs\cariMakan\vendor\google\cloud-storage\src\Bucket.php(294): Googl in C:\xampp\htdocs\cariMakan\vendor\google\cloud-core\src\RequestWrapper.php on line 368

I have search on the internet that I need to give permission to the users. I give it like this

screenshot

but still no luck.

Here is my code:

$this->bucket = $this->storage->bucket($ref);
$this->bucket->upload(
    file_get_contents($data['tmp_name']),
    [
        'name' => $data['name']
    ]
);

How to fix it?

2 Answers
message": "firebase-adminsdk-5jy3x@carimakan-bd835.iam.gserviceaccount.com does not have storage.objects.create access to images/test.jpeg.", "domain": "global", "reason": "forbidden"

The error clearly says that the service account is not allowed to access the storage bucket.

You need to make sure that the service account is properly setup to access the storage bucket.

That is where your problem lies. I am assuming you are using https://firebase-php.readthedocs.io/en/5.14.0/cloud-storage.html

You should make sure that there is a successful connection to firebase. Then make sure that the storage rules apply to your application.

I solve the problem, the things that make an error message like this

```message": "firebase-adminsdk-5jy3x@carimakan-bd835.iam.gserviceaccount.com does not have storage.objects.create access to images/test.jpeg.", "domain": "global", "reason": "forbidden"```

is because I call wrong bucket name. So before call upload function make sure you call exist function first so you can be sure that your bucket is correct.

Related