Picture being uploaded to Firebase storage using PHP but have to create access token manually to make it visible

徘徊边缘 提交于 2020-12-13 18:46:15

问题


I am using PHP to upload image to firebase storage. the picture is being uploaded but it is not being accessible as i have to manually create " access token " to make it accessible. here is the code im using

 $bucketName = "example.appspot.com";
$objectName = 'Photos/test.jpeg';
$storage = new StorageClient();
$bucket = $storage->bucket($bucketName);
$object = $bucket->upload(fopen('sign.jpeg', 'r'),
    [
       'name' => $objectName
    
    ]
    
 ); 

回答1:


That is indeed working as expected: since your upload is not going through a Firebase SDK, there is not method to generate a download URL.

The common workaround is to create a signed URL with an expiration time far into the future, which is the closest equivalent that Cloud Storage has to Firebase's download URL.




回答2:


In addition to @Frank's answer, you could also assign the publicRead ACL to the uploaded file and compose the public URL manually:

$bucketName = "example.appspot.com";
$objectName = 'Photos/test.jpeg';

$storage = new StorageClient();
$bucket = $storage->bucket($bucketName);

$object = $bucket->upload(fopen('sign.jpeg', 'r'), [
   'name' => $objectName
   'predefinedAcl' => 'publicRead'
]);

$publicUrl = "https://{$bucket->name()}.storage.googleapis.com/{$object->name()}";



回答3:


I have made an indirect way to generate and store the access token.

$payload = file_get_contents('https://firebasestorage.googleapis.com/v0/b/example.appspot.com/o/Photos%2Fpic.jpeg');
$data = json_decode($payload); 
echo $data->downloadTokens; 

This code has created the access token and it shows the downloadToken on screen.

Thank you everyone for your answers.



来源:https://stackoverflow.com/questions/63546484/picture-being-uploaded-to-firebase-storage-using-php-but-have-to-create-access-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!