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 存储桶
基础教程推荐
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01