将 Cache-Control 和 Expires 标头添加到 Azure 存储 Blob [英] Add Cache-Control and Expires headers to Azure Storage Blobs

查看:36
本文介绍了将 Cache-Control 和 Expires 标头添加到 Azure 存储 Blob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Azure 存储来提供静态文件 blob,但我想在提供时向文件/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.

应用程序,如 CloudXplorer 和 Cerebrata 的 Cloud Storage Studio 提供了在容器和 blob 上设置元数据属性的选项,但在尝试添加缓存控制时会感到不安.

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?

推荐答案

我不得不在大约 60 万个 blob 上运行批处理作业,发现 2 件事确实有帮助:

I had to run a batch job on about 600k blobs and found 2 things that really helped:

  1. 在同一数据中心以辅助角色运行操作.只要Azure 服务在同一个关联组中,它们之间的速度就很好.此外,没有数据传输费用.
  2. 并行运行操作..net v4 中的任务并行库 (TPL) 使这非常容易.这是为容器中的每个 blob 并行设置缓存控制标头的代码:

  1. Running the operation from a worker role in the same data center. The speed between Azure services is great as long as they are in the same affinity group. Plus there are no data transfer costs.
  2. 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:

// get the info for every blob in the container
var blobInfos = cloudBlobContainer.ListBlobs(
    new BlobRequestOptions() { UseFlatBlobListing = true });
Parallel.ForEach(blobInfos, (blobInfo) =>
{
    // get the blob properties
    CloudBlob blob = container.GetBlobReference(blobInfo.Uri.ToString());
    blob.FetchAttributes();

    // set cache-control header if necessary
    if (blob.Properties.CacheControl != YOUR_CACHE_CONTROL_HEADER)
    {
        blob.Properties.CacheControl = YOUR_CACHE_CONTROL_HEADER;
        blob.SetProperties();
    }
});

这篇关于将 Cache-Control 和 Expires 标头添加到 Azure 存储 Blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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