将 Cache-Control 和 Expires 标头添加到 Azure 存储 Blob
我使用 Azure 存储来提供静态文件 blob,但我想在提供时向文件/blob 添加 Cache-Control 和 Expires 标头,以降低带宽成本。
像 CloudXplorer 和 Cerebrata 的 Cloud Storage Studio 提供了在容器和 blob 上设置元数据属性的选项,但在尝试添加缓存控制时会感到不安。
有人知道是否可以为文件设置这些标头吗?
I'm using Azure Storage to serve up static file blobs but I'd like to add a Cache-Control and Expires header to the files/blobs when served up to reduce bandwidth costs.
Application like CloudXplorer and Cerebrata's Cloud Storage Studio give options to set metadata properties on containers and blobs but get upset when trying to add Cache-Control.
Anyone know if it's possible to set these headers for files?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我必须在大约 600k blob 上运行批处理作业,并发现有两件事确实有帮助:
并行运行操作。 .net v4 中的任务并行库 (TPL) 使这变得非常简单。以下是为容器中的每个 blob 并行设置缓存控制标头的代码:
I had to run a batch job on about 600k blobs and found 2 things that really helped:
Running the operation in parallel. The Task Parallel Library (TPL) in .net v4 makes this really easy. Here is the code to set the cache-control header for every blob in a container in parallel:
这是 Joel Fillmore 使用 Net 5 和 V12 Azure.Storage.Blobs 的答案的更新版本。 (旁白:如果可以在父容器上设置默认标头属性不是很好吗?)
Azure 可以运行“WebJobs”,而不是创建网站并使用 WorkerRole。您可以在存储帐户所在的同一数据中心的网站上按需运行任何可执行文件,以设置缓存标头或任何其他标头字段。
下面的代码为每个容器运行一个单独的任务,并且我每分钟更新多达 100K 个标头(取决于一天中的时间?)。没有出口费用。
Here's an updated version of Joel Fillmore's answer using Net 5 and V12 of Azure.Storage.Blobs. (Aside: wouldn't it be nice if default header properties could be set on the parent container?)
Instead of creating a website and using a WorkerRole, Azure has the ability to run "WebJobs". You can run any executable on demand on a website at the same datacenter where your storage account is located to set cache headers or any other header field.
The code below runs a separate task for each container, and I'm getting up to 100K headers updated per minute (depending on time of day?). No egress charges.
Cerebrata Cloud Storage Studio最新版本,v2011.04.23.00,支持设置对单个 blob 对象的缓存控制。右键单击 Blob 对象,选择“查看/编辑 Blob 属性”,然后设置 Cache-Control 属性的值。 (例如
public,max-age=2592000
)。如果使用curl 检查 blob 对象的 HTTP 标头,您将看到返回的缓存控制标头以及您设置的值。
The latest version of Cerebrata Cloud Storage Studio, v2011.04.23.00, supports setting cache-control on individual blob objects. Right click on the blob object, choose "View/Edit Blob Properties" then set the value for the
Cache-Control
attribute. (e.g.public, max-age=2592000
).If you check the HTTP headers of the blob object using curl, you'll see the cache-control header returned with the value you set.
有时,最简单的答案就是最好的答案。如果您只想管理少量 Blob,可以使用 Azure 管理 更改以下内容的标头/元数据:你的斑点。
在该编辑窗口中,您可以自定义缓存控制、内容编码、内容语言等。
注意:您当前无法从 Azure 编辑此数据门户
Sometimes, the simplest answer is the best one. If you just want to manage a small amount of blobs, you can use Azure Management to change the headers/metadata for your blobs.
In that edit window, you can customize the Cache Control, Content Encoding, Content Language, and more.
Note: you cannot currently edit this data from the Azure Portal
最新的 CloudBerry Explorer 现在支持缓存控制:
http://www.cloudberrylab.com/forum/default.aspx ?g=帖子&t=3047
Latest CloudBerry Explorer now supports Cache-Control:
http://www.cloudberrylab.com/forum/default.aspx?g=posts&t=3047
这是 Joel Fillmore 使用 WindowsAzure.Storage v9.3.3 的答案的更新版本。请注意,ListBlobsSegmentedAsync 返回的页面大小为 5,000,这就是使用 BlobContinuationToken 的原因。
Here's an updated version of Joel Fillmore's answer consuming WindowsAzure.Storage v9.3.3. Note that ListBlobsSegmentedAsync returns a page size of 5,000 which is why the BlobContinuationToken is used.
这可能来不及回答了,但最近我想以不同的方式做同样的事情,我有图像列表,需要使用 powershell 脚本来应用(当然是在 Azure 存储组件的帮助下)
希望将来有人会发现这很有用。
使用 powershell 脚本设置 Azure blob 缓存控制<中给出的完整说明< /a>
This might be too late to answer, but recently I wanted to do the same in different manner, where I have list of images and needed to apply using powershell script (of course with the help of Azure storage assembly)
Hope someone will find this useful in future.
Complete explanation given in Set Azure blob cache-control using powershell script
这是一个批处理/unix 脚本,适用于不使用 PowerShell 的 Windows 计算机的每个人。以下脚本循环遍历所有 blob,并单独设置 blob 上的 Content-Cache 属性(Cache-Control http 标头)。
不幸的是,没有办法同时设置多个 blob 的属性,因此这是一项耗时的任务。每个斑点通常需要大约 1-2 秒。然而,正如 Jay Borseth 指出的那样,如果从与存储帐户位于同一数据中心的服务器运行该过程,则会显着加快该过程。
Here is a batch/unix script for everyone that does not sit on a Windows machine with PowerShell. The following script loops through all blobs and sets the Content-Cache property (Cache-Control http header) on the blobs individually.
Unfortunately, there is no good is no way to set properties on several blobs simultaneously, so this is a time consuming task. It usually takes around 1–2 seconds per blob. However, as Jay Borseth points out, the process is significantly accelerated if run it from a server in the same data center as your storage account.
通过 PowerShell 脚本设置存储 blob 缓存控制属性
https://gallery .technet.microsoft.com/How-to-set-storage-blob-4774aca5
Set storage blob cache-control Properties by PowerShell script
https://gallery.technet.microsoft.com/How-to-set-storage-blob-4774aca5