简体   繁体   English

将映像上传到Azure Blob存储-本地工作,部署后失败

[英]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- 您可以尝试以下步骤-

  • Go to your resources under your subscription and select the app service under which you deployed your app. 转到订阅下的资源,然后选择用于部署应用程序的应用程序服务。 在此处输入图片说明
  • Under Development Tools, select Advanced Tools and click Go. 在“开发工具”下,选择“高级工具”,然后单击“执行”。 This will take you to the pseudo hard drive where your app is deployed. 这将带您到已部署应用程序的伪硬盘驱动器。

在此处输入图片说明

  • Under Debug console, select CMD. 在调试控制台下,选择CMD。

在此处输入图片说明

  • It will look something like below. 如下所示。 Create/put your file in the appropriate path and use that path. 在适当的路径中创建/放置文件,然后使用该路径。

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 简单的WebClient在本地工作,但在部署到Azure时无法工作 - Simple WebClient works locally, but not when deployed to Azure 具有约束的属性路由在本地工作,但在部署时会失败 - Attribute Route with constraint works locally but fails when deployed Blob 触发的 Azure 函数在部署后不起作用,但在本地运行,并且没有任何错误迹象 - Blob triggered Azure function doesn't work deployed but works locally, and no sign of any error 在Azure Blob存储中获取图像 - Getting images in the Azure Blob Storage C# Azure ServiceBus / Azure SQL - 在本地工作,部署到 Azure 时似乎不起作用 - C# Azure ServiceBus / Azure SQL - works locally, does not seem to work when deployed to Azure JavaScript在本地运行,但在部署到Web服务器时无法运行 - JavaScript works locally but not when deployed to webserver 使用 UseCookiesAuthentication 来授权 MVC Controller 操作,它在本地工作,当应用程序部署在 azure 中时会出现问题 - using UseCookiesAuthentication to authorize the MVC Controller Action, it works locally, gives issue when the application is deployed in azure 使用 Azure blob 存储时应该在本地保存什么? - What should I be saving locally when I use Azure blob storage? 从Azure Blob存储下载图像 - Downloading images from Azure Blob Storage Database First DBContext在本地工作,但在部署时无法工作 - Database First DBContext works locally but not when deployed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM