【问题标题】:Change Content Type for files in Azure Storage Blob更改 Azure 存储 Blob 中文件的内容类型
【发布时间】:2014-12-02 15:16:08
【问题描述】:

我只使用 Microsoft Azure 存储,不使用其他 Azure 产品/服务。我通过 ftp 类型客户端 (GoodSync) 将文件上传到我的存储 blob,并且在所有文件已经在 Blob 中之后,我需要根据它们的文件扩展名更改所有文件的内容类型。我环顾四周,并没有发现如何在没有他们的 VPS 和 PowerShell 的情况下做到这一点。我有哪些选择,我该如何做到这一点?我这里真的需要一步一步来。

【问题讨论】:

  • 您使用哪种编程语言?查找特定于语言的存储客户端库并查找更新 blob 属性的函数(您要更新的属性是 Content-Type)。您可以从您的计算机运行代码。无需获取其他 Azure 服务。
  • 我使用 ASP,但通过逐步说明,我可以做任何事情。我精通计算机,只需要有关在我的计算机上做什么/运行/安装的说明。
  • 我觉得解释太多了。我认为如果您使用可用的存储资源管理器之一并更改 blob 的内容类型属性或搜索“azure blob 更改内容类型”会更容易,我相信您会找到很多示例。跨度>
  • 我需要更改 90,000 多个文件,并且每月增加数千个
  • Aah ...在这种情况下,使用工具手动更新内容类型可能会很棘手。给我一些时间,我会提供一些代码(以防其他人打败我:))。但对于未来,请尝试使用 AzCopy 之类的工具来上传 Blob,该工具仅在上传时设置 Blob 内容。

标签: windows azure storage azure-storage azure-blob-storage


【解决方案1】:

我最近遇到了同样的问题,所以我创建了一个简单的实用程序类,以便根据文件的扩展名“修复”内容类型。你可以阅读详情here

您需要做的是解析 Azure 存储容器中的每个文件并根据字典更新 ContentType,该字典定义了适合每个文件扩展名的 MIME 类型。

// Connect to your storage account
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString);

// Load Container with the specified name 
private CloudBlobContainer GetCloudBlobContainer(string name)
{
    CloudBlobClient cloudBlobClient = _storageAccount.CreateCloudBlobClient();
    return cloudBlobClient.GetContainerReference(name.ToLowerInvariant());
}
// Parse all files in your container and apply proper ContentType private void ResetContainer(CloudBlobContainer container) { if (!container.Exists()) return; Trace.WriteLine($"Ready to parse {container.Name} container"); Trace.WriteLine("------------------------------------------------"); var blobs = container.ListBlobs().ToList(); var total = blobs.Count; var counter = 1; foreach (var blob in blobs) { if (blob is CloudBlobDirectory) continue; var cloudBlob = (CloudBlob)blob; var extension = Path.GetExtension(cloudBlob.Uri.AbsoluteUri); string contentType; _contentTypes.TryGetValue(extension, out contentType); if (string.IsNullOrEmpty(contentType)) continue; Trace.Write($"{counter++} of {total} : {cloudBlob.Name}"); if (cloudBlob.Properties.ContentType == contentType) { Trace.WriteLine($" ({cloudBlob.Properties.ContentType}) (skipped)"); continue; } cloudBlob.Properties.ContentType = contentType; cloudBlob.SetProperties(); Trace.WriteLine($" ({cloudBlob.Properties.ContentType}) (reset)"); } }

_contentTypes 是一个字典,其中包含每个文件扩展名的适当 MIME 类型:

私有只读字典 _contentTypes = new Dictionary() { {".jpeg", "图像/jpeg"}, {".jpg", "图像/jpeg" } };

内容类型和源代码的完整列表可以在here找到。

【讨论】:

  • .net 中内置了一种方法来为您获取内容类型。 System.Web.MimeMapping.GetMimeMapping(字符串文件名);
【解决方案2】:

这是最新 Azure.Storage.Blobs SDK 的更新版本。我正在使用 .Net 5 和控制台应用程序。

using Azure.Storage.Blobs.Models;
using System;
using System.Collections.Generic;
using System.IO;

var contentTypes = new Dictionary<string, string>()
{
    {".woff", "font/woff"},
    {".woff2", "font/woff2" }
};

var cloudBlobClient = new BlobServiceClient("connectionstring");
var cloudBlobContainerClient = cloudBlobClient.GetBlobContainerClient("fonts");
await cloudBlobContainerClient.CreateIfNotExistsAsync();

var blobs = cloudBlobContainerClient.GetBlobsAsync();

await foreach (var blob in blobs)
{
    var extension = Path.GetExtension(blob.Name);

    contentTypes.TryGetValue(extension, out var contentType);
    if (string.IsNullOrEmpty(contentType)) continue;

    if (blob.Properties.ContentType == contentType)
    {
        continue;
    }

    try
    {
        // Get the existing properties
        var blobClient = cloudBlobContainerClient.GetBlobClient(blob.Name);
        var properties = await blobClient.GetPropertiesAsync();

        var headers = new BlobHttpHeaders
        {
            ContentType = contentType,
            CacheControl = properties.CacheControl,
            ContentDisposition = properties.ContentDisposition,
            ContentEncoding = properties.ContentEncoding,
            ContentHash = properties.ContentHash,
            ContentLanguage = properties.ContentLanguage
        };

        // Set the blob's properties.
        await blobClient.SetHttpHeadersAsync(headers);
    }
    catch (RequestFailedException e)
    {
        Console.WriteLine($"HTTP error code {e.Status}: {e.ErrorCode}");
        Console.WriteLine(e.Message);
        Console.ReadLine();
    }
}

【讨论】:

    猜你喜欢
    • 2021-09-17
    • 2021-02-27
    • 2020-07-22
    • 2020-05-02
    • 1970-01-01
    • 1970-01-01
    • 2021-04-14
    • 2021-08-14
    • 2021-07-09
    相关资源
    最近更新 更多