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

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

问题描述

我使用Azure存储来提供静态文件的斑点,但我想补充一个Cache-Control和Expires头时担任了降低带宽成本的文件/文件斑点。

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的的云存储工作室给选项来设置的容器和斑点的元数据属性,但生气尝试添加缓存控制时。

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?

推荐答案

我不得不在约600K斑点运行批处理作业,发现两件事情真正帮助:

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


  1. 从在同一个数据中心的辅助角色运行操作。只要它们是相同的亲和基团中的Azure服务之间的速度是很大的。再加上有没有数据传输费用。

  2. 在并行运行的操作。在.NET V4任务并行库(TPL),使这很容易。这里是code设置为每个BLOB中的Cache-Control头在并行容器:

  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存储斑点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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