这里我使用的是本地 Azure 存储模拟器。我能够将图像存储在 Blob 存储中。我将 Azure 存储模拟器连接到本地数据库,因此我能够在数据库中看到图像的本地路径。这里我需要在代码中获取路径,并需要保存在另一个表中。这是代码。我需要返回本地图像路径。
public static CloudBlockBlob getAzureStorage(string imageName)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("images");
container.CreateIfNotExists();
CloudBlockBlob blockBlob = container.GetBlockBlobReference(imageName);
return blockBlob;
}
public static string uploadToAzure(Image img, CloudBlockBlob blockBlob)
{
MemoryStream memoryStream = new MemoryStream();
img.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
memoryStream.Position = 0;
blockBlob.UploadFromStream(memoryStream);
return String.Format("http://{0}{1}", blockBlob.Uri.DnsSafeHost, blockBlob.Uri.AbsolutePath);
}
答案1
我得到了问题的答案。将权限设置为公共。T
container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Container });
container.CreateIfNotExists();