简体   繁体   English

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

[英]How can I reduce my data transfer cost? Amazon S3 --> Cloudflare --> Visitor

I recently started using Amazon S3 to serve images to my visitors since this will reduce the server load. 我最近开始使用Amazon S3向访问者提供图像,因为这会减少服务器负载。 Now, there is a new problem: Today I looked into my AWS billings. 现在,出现了一个新问题:今天我查看了我的AWS账单。 I noticed that I have a huge bill waiting for me - there has been a total of 4TB AWS Data Transfer in 20 days. 我注意到我有一个巨大的账单等着我 - 在20天内总共有4TB的AWS数据传输。

Obviously this is because the high amount of outgoing Amazon S3 traffic (to Cloudflare which then serves it to the visitors). 显然这是因为大量传出的Amazon S3流量(到Cloudflare然后将其提供给访问者)。 Now I should to reduce the amount of requested files by setting a Cache header (since Cloudflare's Crawler will respect that). 现在我应该通过设置Cache头来减少所请求文件的数量(因为Cloudflare的Crawler会尊重它)。 I have modified my code like this: 我修改了我的代码:

$s3->putObjectFile($path, $bucket , 'images/'.$id.'.jpg', S3::ACL_PUBLIC_READ);

to

$s3->putObjectFile($path, $bucket , 'images/'.$id.'.jpg', S3::ACL_PUBLIC_READ, array('Cache-Control' => 'public,max-age=31536000'));

Still, it does not work. 不过,它不起作用。 Cloudflare does not respect the Cache because the Cache-Control does not show up as "Cache-Control" in the Header but instead as "x-amz-meta-cachecontrol". Cloudflare不尊重Cache,因为Cache-Control在Header中不显示为“Cache-Control”,而是显示为“x-amz-meta-cachecontrol”。 Cloudflare ignores this. Cloudflare忽略了这一点。

Does anyone have an easy solution for this? 有人有一个简单的解决方案吗?

TL;DR: I have more or less the same problem as this guy: http://support.bucketexplorer.com/topic734.html (that was in 2008) TL; DR:我和这个人有或多或少相同的问题: http//support.bucketexplorer.com/topic734.html (那是在2008年)

EDIT: I have stumbled upon this: Amazon S3 not caching images but unfortunately that solution does not work for me. 编辑:我偶然发现了这一点: 亚马逊S3没有缓存图像,但不幸的是,该解决方案对我不起作用。

EDIT 2: Turns out it didn't work because I was using an old version of the "Amazon S3 class". 编辑2:事实证明它没有用,因为我使用的是旧版“亚马逊S3类”。 I updated and the code works now. 我更新了,代码现在正常工作。

Thank you for your time. 感谢您的时间。

If you are getting "x-amz-meta-cachecontrol", it is likely you are not setting the headers correctly. 如果您正在获取“x-amz-meta-cachecontrol”,则可能是您没有正确设置标头。 It might just be the exact way you are doing it in your code. 它可能就是您在代码中执行此操作的确切方式。 This is supposed to work. 应该工作。 I am deducing this is php using Amazon S3 PHP Class? 我推断这是使用Amazon S3 PHP类的PHP?

Try this: 试试这个:

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

In the S3 PHP docs putObjectFile is listed under Legacy Methods: S3 PHP文档中, putObjectFile列在Legacy Methods下:

putObjectFile (string $file, 
               string $bucket, 
               string $uri, 
               [constant $acl = S3::ACL_PRIVATE], 
               [array $metaHeaders = array()], 
               [string $contentType = null])

Compare to this: 与此相比:

putObject (mixed $input, 
           string $bucket, 
           string $uri, 
           [constant $acl = S3::ACL_PRIVATE], 
           [array $metaHeaders = array()], 
           [array $requestHeaders = array()])

You need to set cache-control as a request header, but appears that there is no way to set request headers with putObjectFile , only meta headers. 您需要将缓存控制设置为请求标头,但似乎无法使用putObjectFile设置请求标头,只能设置元标头。 You have to use putObject and give it an empty array for meta headers and then another array with the request headers (including cache-control). 你必须使用putObject并为元头提供一个空数组,然后为另一个包含请求头的数组(包括缓存控制)。

You can also try some of the other working examples I have listed below. 您还可以尝试下面列出的其他一些工作示例。

See also: 也可以看看:

How to set the Expires and Cache-Control headers for all objects in an AWS S3 bucket with a PHP script (php) 如何使用PHP脚本 (php) 为AWS S3存储桶中的所有对象设置Expires和Cache-Control标头

Updating caching headers for Amazon S3 and CloudFront (python) 更新Amazon S3和CloudFront的缓存标头 (python)

Set cache-control for entire S3 bucket automatically (using bucket policies?) 自动为整个S3存储桶设置缓存控制(使用存储桶策略?)

http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectGET.html?r=5225 http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectGET.html?r=5225

You can now. 你现在可以。 Go to s3 bucket. 转到s3桶。 Open the file and set property 打开文件并设置属性

Aws控制台

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM