Blob 存储图像 - Azure
Blob Storage Images - Azure
我在通过网络 api 上传到 blob 存储中的 public 容器的图像时遇到了一些问题。问题是我需要我通过 api 上传的图片必须是可浏览的,我的意思是当把 public link 放在浏览器中时你可以在浏览器中看到图片,但是实际行为是,当我把 link 图像下载图像并且在浏览器中没有显示任何内容但是当我通过 azure 门户上传图像时,我可以看到我想要的图像.. ..我有我的容器public,我不知道还能做什么....我上传图片的代码是这样的:
private readonly CloudBlobContainer blobContainer;
public UploadBlobController()
{
var storageConnectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
var storageAccount = CloudStorageAccount.Parse(storageConnectionString);
var blobClient = storageAccount.CreateCloudBlobClient();
blobContainer = blobClient.GetContainerReference("messagesthredimages");
blobContainer.CreateIfNotExists();
blobContainer.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
}
[HttpPost]
[Route("UploadImagetoBlob")]
public async Task<IHttpActionResult> UploadImagetoBlob()//string imagePath
{
try
{
var image = WebImage.GetImageFromRequest();
var imageBytes = image.GetBytes();
var blockBlob = blobContainer.GetBlockBlobReference(image.FileName);
blockBlob.Properties.ContentType = "messagesthreadimages/" + image.ImageFormat;
await blockBlob.UploadFromByteArrayAsync(imageBytes, 0, imageBytes.Length);
return Ok(blockBlob.Uri.ToString());
}
catch (Exception)
{
return BadRequest();
}
}
例子,希望有人能帮我解决这个问题。
我以前遇到过同样的问题。浏览器在不知道格式(文件类型)时下载文件。如果您使用桌面应用程序监视文件(不确定此选项在门户中的什么位置),您将找到文件类型。
这些文件类型是根据您设置的 blockBlob.Properties.ContentType
设置的。您需要检查并检查 image.ImageFormat
returns 到底是什么。如果此值设置为 "image/jpeg"
之类的值,则浏览器只会显示图像。但是,由于您使用的是 "messagesthreadimages/" + image.ImageFormat
,它可能会设置类似 "messagesthreadimages/image/jpeg"
.
的内容
正如 Neville 所说,当您像 blockBlob.Properties.ContentType 这样调用 SetProperties 方法时,您有一些误解。
Content-Type表示消息内容的媒体类型。
图像内容类型允许在消息中包含标准化图像文件。更详细的可以参考这个link.
image/g3fax [RFC1494]
image/gif [RFC1521]
image/ief (Image Exchange Format) [RFC1314]
image/jpeg [RFC1521]
image/tiff (Tag Image File Format) [RFC2301]
我看了这个article,看来你会自定义图片的ContentType。
因此,您可以更改代码如下:
blockBlob.Properties.ContentType = "image/" + image.ImageFormat;
我在通过网络 api 上传到 blob 存储中的 public 容器的图像时遇到了一些问题。问题是我需要我通过 api 上传的图片必须是可浏览的,我的意思是当把 public link 放在浏览器中时你可以在浏览器中看到图片,但是实际行为是,当我把 link 图像下载图像并且在浏览器中没有显示任何内容但是当我通过 azure 门户上传图像时,我可以看到我想要的图像.. ..我有我的容器public,我不知道还能做什么....我上传图片的代码是这样的:
private readonly CloudBlobContainer blobContainer;
public UploadBlobController()
{
var storageConnectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
var storageAccount = CloudStorageAccount.Parse(storageConnectionString);
var blobClient = storageAccount.CreateCloudBlobClient();
blobContainer = blobClient.GetContainerReference("messagesthredimages");
blobContainer.CreateIfNotExists();
blobContainer.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
}
[HttpPost]
[Route("UploadImagetoBlob")]
public async Task<IHttpActionResult> UploadImagetoBlob()//string imagePath
{
try
{
var image = WebImage.GetImageFromRequest();
var imageBytes = image.GetBytes();
var blockBlob = blobContainer.GetBlockBlobReference(image.FileName);
blockBlob.Properties.ContentType = "messagesthreadimages/" + image.ImageFormat;
await blockBlob.UploadFromByteArrayAsync(imageBytes, 0, imageBytes.Length);
return Ok(blockBlob.Uri.ToString());
}
catch (Exception)
{
return BadRequest();
}
}
例子,希望有人能帮我解决这个问题。
我以前遇到过同样的问题。浏览器在不知道格式(文件类型)时下载文件。如果您使用桌面应用程序监视文件(不确定此选项在门户中的什么位置),您将找到文件类型。
这些文件类型是根据您设置的 blockBlob.Properties.ContentType
设置的。您需要检查并检查 image.ImageFormat
returns 到底是什么。如果此值设置为 "image/jpeg"
之类的值,则浏览器只会显示图像。但是,由于您使用的是 "messagesthreadimages/" + image.ImageFormat
,它可能会设置类似 "messagesthreadimages/image/jpeg"
.
正如 Neville 所说,当您像 blockBlob.Properties.ContentType 这样调用 SetProperties 方法时,您有一些误解。
Content-Type表示消息内容的媒体类型。
图像内容类型允许在消息中包含标准化图像文件。更详细的可以参考这个link.
image/g3fax [RFC1494]
image/gif [RFC1521]
image/ief (Image Exchange Format) [RFC1314]
image/jpeg [RFC1521]
image/tiff (Tag Image File Format) [RFC2301]
我看了这个article,看来你会自定义图片的ContentType。 因此,您可以更改代码如下:
blockBlob.Properties.ContentType = "image/" + image.ImageFormat;