Upload image to firebase bucket with auto generated access token in laravel(在 laravel 中使用自动生成的访问令牌将图像上传到 firebase 存储桶)
问题描述
我关注这个 link 将图片上传到 Firebase 存储,图片上传到存储中.但是,要查看图像,我必须在存储中的图像内手动生成访问令牌.我希望有什么想法可以从我的代码中自动生成该特定图像的访问令牌.
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);
}
我尝试使用 'predefinedAcl' =>'PUBLICREAD'
但它不起作用,并且手动创建的访问令牌在存储中也不再可用
I try to used 'predefinedAcl' => 'PUBLICREAD'
but it's not working and also manually created access token is not available anymore in the storage
推荐答案
通过服务器端 SDK 上传到 Cloud Storage 的文件不会生成下载 URL.
No download URLs are generated for files that are uploaded to Cloud Storage through server-side SDKs.
您有两种选择可以在自己的代码中为这些文件生成可公开访问的 URL:
You have two options to generate a publicly accessible URL for these files in your own code:
生成所谓的签名网址,其中是可能来自服务器端代码.格式与 Firebase 的下载 URL 不同,但签名 URL 还提供公开的只读访问权限.
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.
自行在上传文件的元数据中设置必要的访问令牌.请注意,这不是一个记录在案的 API,因此虽然它现在似乎适用于许多开发人员,但它可能会在某个时候停止工作.
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.
这篇关于在 laravel 中使用自动生成的访问令牌将图像上传到 firebase 存储桶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 laravel 中使用自动生成的访问令牌将图像上传到 firebase 存储桶


基础教程推荐
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 如何替换eregi() 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01