中使用自动生成的访问令牌将图像上传到

中使用自动生成的访问令牌将图像上传到

本文介绍了在 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:

  1. 生成所谓的签名网址,其中可能来自服务器端代码.格式与 Firebase 的下载 URL 不同,但签名 URL 还提供公开的只读访问权限.

  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.

自行在上传文件的元数据中设置必要的访问令牌.请注意,这不是一个记录在案的 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 存储桶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 01:06