Laravel-如何使用PHP/Laravel从Google云端存储访问存储的图像? [英] Laravel - How to access stored image from Google Cloud Storage using PHP/Laravel?

查看:77
本文介绍了Laravel-如何使用PHP/Laravel从Google云端存储访问存储的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个Laravel应用中,我想显示存储在 Google Cloud Storage 中的客户图像.客户映像名称存储在本地数据库中.我手上所有可用的信息是:

In one of my Laravel apps, I want to show customer images that are stored in Google Cloud Storage. The Customer image name is stored in the local database. All of the information available in my hand is:

基本URL:gs://<my_custom_url>/
客户图像存储在:客户/图像/
数据库中的文件名:customer_one.jpg [该图像在Google Cloud Storage中可用]

Base URL: gs:// < my_custom_url >/
Customer Image Stored in: customer/images/
File Name in DB: customer_one.jpg [The image is available in Google Cloud Storage]

现在我的问题是如何从 Google云存储检索/获取该图像并以 HTML 标签显示该图像?

Now my question is how can I retrieve/fetch that image from Google Cloud Storage and show the image in HTML tag?

有人知道怎么做吗?

推荐答案

我说从Google云存储中检索图像的最简单方法是为此安装软件包.

I'd say easiest way to retrieve images from google cloud storage is install package for that.

我个人建议您使用 Laravel Google Cloud Storage 软件包.

Personally I'd recommend you to use Laravel Google Cloud Storage package.

我将为您写出基本步骤

  • 首先安装软件包

撰写者需要superbalist/laravel-google-cloud-storage

  • 在文件系统下的第二个定义所需的凭据
'gcs' => [
    'driver' => 'gcs',
    'project_id' => env('GOOGLE_CLOUD_PROJECT_ID', 'your-project-id'),
    'key_file' => env('GOOGLE_CLOUD_KEY_FILE', null), // optional: /path/to/service-account.json
    'bucket' => env('GOOGLE_CLOUD_STORAGE_BUCKET', 'your-bucket'),
    'path_prefix' => env('GOOGLE_CLOUD_STORAGE_PATH_PREFIX', null), // optional: /default/path/to/apply/in/bucket
    'storage_api_uri' => env('GOOGLE_CLOUD_STORAGE_API_URI', null), // see: Public URLs below
    'visibility' => 'public', // optional: public|private
],

之后,您只需定义存储驱动程序和路径即可检索图像.假设您在 customer/images 文件夹

Afterwards you can just define storage driver and path to retrieve your images. Assuming that you have an image foo.jpg under customer/images folder

$disk = Storage::disk('gcs');
$url = $disk->url('customer/images/foo.jpg');
>>> http://storage.googleapis.com/bucket-name/customer/images/foo.jpg

,或者如果您将 path_prefix 定义为默认设置,则只需执行

or if you define path_prefix as in default you can retrieve images by just doing

$disk = Storage::disk('gcs');
$url = $disk->url('foo.jpg');
>>> http://storage.googleapis.com/bucket-name/customer/images/foo.jpg

将为您提供Google云存储中的图像.

Will give you images from your google cloud storage.

有关更多信息,请检查:

For more information check:

https://github.com/Superbalist/laravel-google-cloud-storage

这篇关于Laravel-如何使用PHP/Laravel从Google云端存储访问存储的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆