[英]Uplod images to azure blob storage - Works locally, fails when deployed
I am using ASPNetBoilerplate as a base framework, using the .Net Core template, and I am trying to upload photos to a blob storage. 我使用ASPNetBoilerplate作为基本框架,使用.Net Core模板,并且试图将照片上传到Blob存储。 The site is hosted in an Azure Web App, and the Blob storage is in the same subscription.
该站点托管在Azure Web App中,并且Blob存储位于同一订阅中。
I am using the below code, When I run it locally in debug mode, it works, but as soon as I deploy it to Azure, I get an error of unable to locate the file D:\\public\\images\\ 我正在使用下面的代码,当我在调试模式下本地运行时,它可以工作,但是一旦将其部署到Azure,就会出现无法找到文件D:\\ public \\ images \\的错误。
Can anyone see what I have done incorrectly for the azure deployment, or why it would work locally but not in Azure. 谁能看到我为azure部署所做的错误操作,或者为什么它会在本地运行但不能在Azure中运行。
I am trying to generate the uploaded file name from a guid, upload the image(s) to azure, and store the generated filename into an sql database. 我正在尝试从guid生成上传的文件名,将图像上传到azure,然后将生成的文件名存储到sql数据库中。
Any help or suggestions will be greatly appreciated. 任何帮助或建议,将不胜感激。
public async Task<ICollection<BlogImage>> UploadFileAsync(ICollection<IFormFile> files, string RecordType, int id) {
ICollection<BlogImage> _blogImages = new Collection<BlogImage>();
string imageFullPath = null;
if (files == null || files.Count == 0)
{
await Task.FromException(new Exception("No File Supplied"));
}
try
{
//Connect To Azure
CloudStorageAccount cloudStorageAccount = GetAuth();
//Create a refernce to blob client
CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
//Create a reference to the container
CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference("images");
if (await cloudBlobContainer.CreateIfNotExistsAsync())
{
await cloudBlobContainer.SetPermissionsAsync(
new BlobContainerPermissions
{
PublicAccess = BlobContainerPublicAccessType.Blob
}
);
}
foreach (var file in files)
{
string imageName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(RecordType + "/" +id.ToString() + "/" + imageName);
cloudBlockBlob.Properties.ContentType = file.ContentType;
// Stream fileStream = File.OpenRead(file)
Logger.Info("file name is: " + file.FileName + " Name is: " + file.Name + " Generated Name is: " + imageName);
using (Stream fileStream = File.OpenRead(file.FileName))
{
Logger.Info("File Opened;");
// Upload the file
await cloudBlockBlob.UploadFromStreamAsync(fileStream);
System.Drawing.Image image = System.Drawing.Image.FromStream(fileStream);
var im = new BlogImage { ImageName = imageName, Size = file.Length, ContentType = file.ContentType, Height = image.Height, Width = image.Width };
fileStream.Dispose();
_Images.Add(im);
}
imageFullPath = cloudBlockBlob.Uri.ToString();
}
}
catch (Exception ex)
{
Logger.Error("Exception triggered: " + ex.Message);
_telemetryClient.TrackException(ex);
}
return _Images;
}
You use the File.OpenRead
method to read the file stream which takes a path (which probably don't exists - depends on the Azure Service you are using to host the App). 您使用
File.OpenRead
方法读取带有路径的文件流(该路径可能不存在-取决于您用来承载App的Azure服务)。 However, you should probably use the OpenReadStream()
for the passed IFormFile
to get the stream instead. 但是,您可能应该对传递的
IFormFile
使用OpenReadStream()
来获取流。
That's because your app is now deployed on cloud and the path is not configured properly. 这是因为您的应用程序现在已部署在云上,并且路径配置不正确。 You can try below steps-
您可以尝试以下步骤-
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.