0

I follow this link to upload images to firebase storage, the images is uploaded in storage. However to view the images I've to manually generate the access token inside the image in storage. I'm hoping is there any idea to auto generate the access token for that particular image from my code.

Below are my code to upload image

$factory = (new Factory)->withServiceAccount(__DIR__.'/myfirebase.json');
$storage = $factory->createStorage();
$image = $request->file('image');

$localfolder = public_path('firebase-temp-uploads') .'/';

if (!file_exists($localfolder)) {
    mkdir($localfolder, 0777, true);
}

$extension  = $image->getClientOriginalExtension();

$file = $shopId. '.' . $extension;

if ($image->move($localfolder, $file)) {
    $uploadedfile = fopen($localfolder.$file, 'r');
    $storage->getBucket()->upload($uploadedfile, [
        'name' => 'categories/'.$file,
        // 'predefinedAcl' => 'PUBLICREAD',
    ]);
    unlink($localfolder . $file);
}

I try to used 'predefinedAcl' => 'PUBLICREAD' but it's not working and also manually created access token is not available anymore in the storage

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Vpa
  • 703
  • 2
  • 9
  • 30

1 Answers1

1

No download URLs are generated for files that are uploaded to Cloud Storage through server-side SDKs.

You have two options to generate a publicly accessible URL for these files in your own code:

  1. Generate a so-called signed URL, which is possibly from the server-side code. The format will be different from Firebase's download URLs, but a signed URL also providers public, read-only access.

  2. Set the necessary access token in the metadata of the uploaded file yourself. Note that this is not a documented API, so while it seems to work for many developers right now, it may stop working at some point.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807