我正在尝试使用C#将图像文件从本地系统上传到Azure Blob存储。下面是我正在使用的代码:
string storageConnectionString = "<connection_string>";
CloudStorageAccount storageacc = CloudStorageAccount.Parse(storageConnectionString);
CloudBlobClient blobClient = storageacc.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("myblob");
container.CreateIfNotExists();
CloudBlockBlob blockBlob = container.GetBlockBlobReference("images");
using (var filestream = System.IO.File.OpenRead(@"C:\\Users\\John\\Desktop\\11173.jpg"))
{
blockBlob.UploadFromStream(filestream);
}
我正在尝试创建名称为myblob
的容器,该容器可以看到。在其中,我创建了一个名称为images
的Blob,然后在其中上传了图像文件。但是我看不到图像文件。上面的代码有什么问题。如何将图像文件上传到Blob存储中。请帮忙。谢谢
答案 0 :(得分:1)
我认为您有两件事错了。
应该是这样的:
string storageConnectionString = "<connection_string>";
CloudStorageAccount storageacc = CloudStorageAccount.Parse(storageConnectionString);
CloudBlobClient blobClient = storageacc.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("images");
container.CreateIfNotExists();
CloudBlockBlob blockBlob = container.GetBlockBlobReference("11173.jpg");
using (var filestream = System.IO.File.OpenRead(@"C:\\Users\\John\\Desktop\\11173.jpg"))
{
blockBlob.UploadFromStream(filestream);
}
答案 1 :(得分:1)
请尝试以下操作:
string storageConnectionString = "<connection_string>";
CloudStorageAccount storageacc = CloudStorageAccount.Parse(storageConnectionString);
CloudBlobClient blobClient = storageacc.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("myblob");
container.CreateIfNotExists();
CloudBlockBlob blockBlob = container.GetBlockBlobReference("images/11173.jpg");
blockBlob.Properties.ContentType = "image/jpg";
using (var filestream = System.IO.File.OpenRead(@"C:\\Users\\John\\Desktop\\11173.jpg"))
{
blockBlob.UploadFromStream(filestream);
}
基本上,以上代码将11173.jpg
保存为images/11173.jpg
。 Blob名称实际上是images/11173.jpg
。
或者您可以尝试以下方法:
string storageConnectionString = "<connection_string>";
CloudStorageAccount storageacc = CloudStorageAccount.Parse(storageConnectionString);
CloudBlobClient blobClient = storageacc.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("myblob");
container.CreateIfNotExists();
CloudBlockBlob blockBlob = container.GetBlockBlobReference("11173.jpg");
blockBlob.Properties.ContentType = "image/jpg";
using (var filestream = System.IO.File.OpenRead(@"C:\\Users\\John\\Desktop\\11173.jpg"))
{
blockBlob.UploadFromStream(filestream);
}
答案 2 :(得分:0)
在安装推荐的nuget pkg“ Azure.Storage.Blobs”(https://www.nuget.org/packages/Azure.Storage.Blobs/)之后,以下代码在.NET Core中工作
public void PersistPhoto(IFormFile fileToPersist, string saveAsFileName)
{
string connectionString= "Azure Storage Connection String";
string containerName= "Azure Storage Container Name";
BlobContainerClient container = new BlobContainerClient(connectionString, containerName);
try
{
// Get a reference to a blob
BlobClient blob = container.GetBlobClient(saveAsFileName);
// Open the file and upload its data
using (Stream file = fileToPersist.OpenReadStream())
{
blob.Upload(file);
}
uri = blob.Uri.AbsoluteUri;
}
catch
{
// log error
}
}
根据Microsoft的规定,NuGet pkg“ Microsoft.Azure.Storage.Blob”现已弃用。