设置 blob 属性 return 一个错误

set blob properties return an error

我之前想为所有上传的 blob 设置缓存控制 属性,但它抛出异常 "The remote server returned an error: (404) Not Found."

 CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
              CloudConfigurationManager.GetSetting("StorageConnectionString"));

        // Create the blob client.
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        List<CloudBlobContainer> containers = blobClient.ListContainers().ToList();

        // Retrieve reference to a previously created container.
        CloudBlobContainer container = blobClient.GetContainerReference(containerName);



        List<IListBlobItem>  blobs = container.ListBlobs().ToList();

        int count = 0;
        foreach (IListBlobItem blob in blobs)
        {
            CloudBlockBlob b = container.GetBlockBlobReference(blob.Uri.ToString());
            b.Properties.CacheControl = "public, max-age=1296000";
            b.SetProperties();

            Console.WriteLine("cached"+count.ToString());
            count++;
        }

在 SetProperties 中抛出错误。

您正在执行分层列表,它可以 return 虚拟目录和 blob。例如,如果您有一个名为 "foo/bar" 的 blob,那么您的列表将 return 一个名为 "foo/" 的 CloudBlobDirectory。当您尝试将其用作 blob 名称时,服务会返回 404,因为 blob 不存在。

要完成您想要的,请将 "useFlatBlobListing: true" 传递给对 ListBlobs 的调用,然后将每个 returned IListBlobItem 转换为 CloudBlob。