How do I display protected Amazon S3 images on my secure site using PHP?

前端 未结 4 2095
太阳男子
太阳男子 2021-01-30 17:39

I am trying to move images for my site from my host to Amazon S3 cloud hosting. These images are of client work sites and cannot be publicly available. I would like them to be d

4条回答
  •  情书的邮戳
    2021-01-30 18:14

    The best way to serve your images is to generate a url using the PHP SDK. That way the downloads go directly from S3 to your users.

    You don't need to download via your servers as @mfonda suggested - you can set any caching headers you like on S3 objects - and if you did you would be losing some major benefits of using S3.

    However, as you pointed out in your question, the url will always be changing (actually the querystring) so browsers won't cache the file. The easy work around is simply to always use the same expiry date so that the same querystring is always generated. Or better still 'cache' the url yourself (eg in the database) and reuse it every time.

    You'll obviously have to set the expiry time somewhere far into the future, but you can regenerate these urls every so often if you prefer. eg in your database you would store the generated url and the expiry date(you could parse that from the url too). Then either you just use the existing url or, if the expiry date has passed, generate a new one. etc...

提交回复
热议问题