我只使用Microsoft Azure存储,不使用其他Azure产品/服务。我通过ftp类型客户端(GoodSync)将文件上传到我的存储Blob,在文件已经在blob中之后,我需要根据文件扩展名更改所有文件的内容类型。我已经环顾了四周,没有他们的PowerShell VPS,我还没有发现如何做到这一点。我的选择是什么?我如何实现这一点?我真的需要在这里一步一步来。
发布于 2021-01-27 06:33:11
这是最新Azure.Storage.Blobs软件开发工具包的更新版本。我正在使用.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 };
// 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();
}
}https://stackoverflow.com/questions/27252751
复制相似问题