【发布时间】:2018-05-16 10:01:16
【问题描述】:
我正在编写一个从 Azure Blob 存储上传/下载项目的服务。当我上传文件时,我设置了 ContentType。
public async Task UploadFileStream(Stream filestream, string filename, string contentType)
{
CloudBlockBlob blockBlobImage = this._container.GetBlockBlobReference(filename);
blockBlobImage.Properties.ContentType = contentType;
blockBlobImage.Metadata.Add("DateCreated", DateTime.UtcNow.ToLongDateString());
blockBlobImage.Metadata.Add("TimeCreated", DateTime.UtcNow.ToLongTimeString());
await blockBlobImage.UploadFromStreamAsync(filestream);
}
但是,当我检索文件时,ContentType 为空。
public async Task<CloudBlockBlob> GetBlobItem(string filename)
{
var doesBlobExist = await this.DoesBlobExist(filename);
return doesBlobExist ? this._container.GetBlockBlobReference(filename) : null;
}
在使用这些方法的代码中,我检查了返回的 Blob 的 ContentType,但它为空。
var blob = await service.GetBlobItem(blobname);
string contentType = blob.Properties.ContentType; //this is null!
我曾尝试在我的 UploadFileStream() 方法(上图)中使用 SetProperties() 方法,但这也不起作用。
CloudBlockBlob blockBlobImage = this._container.GetBlockBlobReference(filename);
blockBlobImage.Properties.ContentType = contentType;
blockBlobImage.SetProperties(); //adding this has no effect
blockBlobImage.Metadata.Add("DateCreated", DateTime.UtcNow.ToLongDateString());
blockBlobImage.Metadata.Add("TimeCreated", DateTime.UtcNow.ToLongTimeString());
await blockBlobImage.UploadFromStreamAsync(filestream);
那么如何为 Azure Blob 存储中的 Blob 项设置 ContentType?
【问题讨论】:
-
可以分享
this.DoesBlobExist方法的代码吗? -
删除本质上是错误检查代码 -- return await this._container.GetBlockBlobReference(filename).ExistsAsync();
-
ExistsAsync的问题在于它返回一个Task<bool>。您需要能够进行网络调用并返回Task<CloudBlockBlob>的东西。
标签: azure azure-storage azure-blob-storage