Laravel AWS S3存储映像缓存 [英] Laravel AWS S3 Storage image cache

查看:219
本文介绍了Laravel AWS S3存储映像缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于Laravel的Web和移动应用程序,用于在AWS S3上存储图像,并且我想增加缓存支持,因为即使是少数应用程序用户也会在AWS S3上生成数百甚至是大量的GET请求。

I have Laravel based web and mobile application that stores images on AWS S3 and I want to add cache support because even small number of app users produce hundreds and sometimes thouthands of GET requests on AWS S3.

要从移动应用程序中获取图像,我使用GET请求,该请求由此类代码处理

To get image from mobile app I use GET request that is being handled by code like this

public function showImage(....) {
  ...
  return Storage::disk('s3')->response("images/".$image->filename);
}

在下一张图片上,您可以看到我收到的响应标头。缓存控件显示无缓存,因此我认为移动应用程序不会缓存该图像。

On the next image you can see response headers that I receive. Cache-Control shows no-cache so I assume that mobile app won't cache this image.


  1. 如何为该请求添加缓存支持?我应该这样做吗?

  2. 我知道 Laravel Documentaion建议用于文件存储的缓存-我应该为S3实现它吗?它可以帮助减少来自AWS S3的GET请求读取文件的数量吗?在哪里可以找到有关它的更多信息。

  1. How can I add cache support for this request? Should I do it?
  2. I know that Laravel Documentaion suggests caching for Filestorage - should I implement it for S3? Can it help to decrease GET requests count of read files from AWS S3? Where can I find more info about it.


推荐答案

您可以使用CloudFlare这样的CDN服务并设置一个缓存标头,以使CloudFlare在一定时间内保留缓存。

You could use a CDN service like CloudFlare and set a cache header to let CloudFlare keep the cache for a certain amount of time.

$s3->putObject(file_get_contents($path), $bucket, $url, S3::ACL_PUBLIC_READ, array(), array('Cache-Control' => 'max-age=31536000, public'));

这样,文件将由CloudFlare提取一次,存储在其服务器中,并提供给用户,而无需从S3请求图像

This way, files would be fetched once by CloudFlare, stored at their servers, and served to users without requesting images from S3 for every single request.

另请参阅:

如何降低数据传输成本?亚马逊S3-> Cloudflare->访客

如何使用以下命令为AWS S3存储桶中的所有对象设置Expires和Cache-Control标头一个PHP脚本

这篇关于Laravel AWS S3存储映像缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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