How to upload a file to Firebase storage using PHP (Larvel) and obtain its public URL

Asked 2 years ago, Updated 2 years ago, 166 views

Using the following libraries in Ravel,
I have confirmed that the file can be uploaded to Firebase storage.

https://github.com/kreait/laravel-firebase

Official Documentation
https://firebase-php.readthedocs.io/en/stable/cloud-storage.html
(Isn't this site helpful at all?)

The code looks like this.

storage=app('firebase.storage');
$bucket=$storage->getBucket();
$object = $bucket->upload(
    fopen(public_path().'/storage/thumbnail/'.$file->hashName(), 'r')
);

If you check the storage of firebase from the console, the uploaded file exists.
When I try to open it, it becomes loaded and I cannot see the image itself.

1. The file is uploaded, but I can't see it,
  The public URL is not generated either because you can specify options when uploading something, or
  Do I need to set up permissions on the Firebase side?

2. If I can upload it in a public state, how can I get a public URL?

I checked the official document and the source, but it got stuck, so I would appreciate it if you could give me some advice.
Thank you for your cooperation.

php laravel firebase google-cloud-storage

2022-09-30 17:11

1 Answers

Self-resolved.

I referred to the code in the sample below.
https://github.com/jeromegamez/firebase-php-examples

$storage=app('firebase.storage');
$bucket=$storage->getBucket();
$object = $bucket->upload(
    fopen(public_path().'/storage/thumbnail/'.$file->hashName(), 'r'),
    [
  'predefinedAcl' = > 'publicRead'
    ]
);
$downloadUrl="https://storage.googleapis.com/".$backet->name().'/'.$object->name();

Added options and uploaded.
I was able to get the download URL.

However, when I try to see the uploaded image on the Firebase console, it is read and not displayed.
I wonder if this part is a bug.


2022-09-30 17:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.