简体   繁体   English

如何将图像保存到 Azure Blob 并使用特定字段命名并返回 Blob Url 并将其保存到 .net core 2.2 中的数据库

[英]How can i save an image to a Azure Blob and name it with specific fields and return the Blob Url and save it to database in .net core 2.2

An ios app calls a .net core web API(post method) to send a few details to save in SQL Database. iOS 应用程序调用 .net 核心 Web API(post 方法)发送一些详细信息以保存在 SQL 数据库中。 I have to save this image in a blob and name it as EmpID_PunchInDate_PunchInTime.png我必须将此图像保存在一个 blob 中并将其命名为 EmpID_PunchInDate_PunchInTime.png

I have the below code but I'm not able to figure out how to call post method inside a post method我有下面的代码,但我无法弄清楚如何在 post 方法中调用 post 方法

/// <summary>
        /// The PostPunchInDetailsToAzure
        /// </summary>
        /// <param name="item">The item<see cref="Punch_In"/></param>
        /// <returns>The <see cref="Task{IActionResult}"/></returns>
        [HttpPost]
        [Route("PostPunchInDetailsToAzure")]
        // POST: api/PunchIn/PostPunchInDetailsToAzure
        public async Task<IActionResult> PostPunchInDetailsToAzure([FromBody] Punch_In item)
        {

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var srtBase64Encoded = item.ClockInNotesForSelfie;
            string fullOutputPath = "E:\\temp\\img.png";
            byte[] imageBytes = Convert.FromBase64String(srtBase64Encoded);
            MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
            ms.Write(imageBytes, 0, imageBytes.Length);
            ms.Position = 0;
            System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
            System.Drawing.Image img = Base64ToImage(srtBase64Encoded);
            img.Save(fullOutputPath, System.Drawing.Imaging.ImageFormat.Png);
            var stream = img.ToStream(ImageFormat.Png);
            _context.Punch_Ins.Add(item);


            await _context.SaveChangesAsync();
            return Ok(new { result = "Data stored succesfully." });
        }

The incoming request comes in the form of JSON as below (for example)传入请求以 JSON 的形式出现,如下所示(例如)

{
"ClockInEmpId":"1000",
"clockInDate":"10/23/2019",
"ClockInTime":"13:23",
"clockInLatitude":"12.919456",
"clockInLongitude":"77.649802",
"clockInLaborAccountName":"/@H///@H//620108",
"ClockInHQTravelTime":"00:00",
"clockInPerDiem":"00.00",
"clockInNotes":"Hello",
"ClockInNotesForSelfie":"A Base 64 string"
}

So the image should be saved as 1000_10232019_1323.png In Azure Blob and Blob URL should be returned.因此图像应在 Azure Blob 中保存为 1000_10232019_1323.png 并且应返回 Blob URL。 How can i accomplish this?我怎样才能做到这一点?

First add reference to the azure storage client nuget package.首先添加对 azure 存储客户端 nuget 包的引用。 Then create a new blob container if not exists( as per your requirement) then get a blob reference with the filename.然后创建一个新的 blob 容器(如果不存在)(根据您的要求),然后获取带有文件名的 blob 引用。

You will need to create the file name from the input request you are getting by picking the fields you need and concatenating them to a string.您需要通过选择您需要的字段并将它们连接到一个字符串来从您获得的输入请求中创建文件名。 Then you create a blob container reference with that filename.然后使用该文件名创建一个 blob 容器引用。

And then use the Uploadasync method to upload the filestream to that blob.然后使用 Uploadasync 方法将文件流上传到该 blob。 THe blobclient has the uri property which you can return. blobclient 具有可以返回的 uri 属性。

Also note that if this is a private blob and you are sharing this url to the end user you would have to create the blob URI with a SAS( shared access signature) token appended to the url so that they can download the file directly with that url.另请注意,如果这是一个私有 blob,并且您要将此 url 共享给最终用户,则必须使用附加到 url 的 SAS(共享访问签名)令牌创建 blob URI,以便他们可以直接下载文件网址。 the SAS token will be based on the access policy defined by you to that container. SAS 令牌将基于您为该容器定义的访问策略。

uploading files to blob storage. 将文件上传到 blob 存储。

Private blobs SAS token. 私有 blob SAS 令牌。

暂无
暂无

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

相关问题 如何将调整大小的图像保存到 ASP.NET 核心应用程序中的 Azure Blob 存储? - How do I save my resized image to my Azure Blob Storage in my ASP.NET Core Application? 如何从 ASP.NET Core 中的私有 Azure Blob Stroage 帐户将图像返回到视图? - How do I return an image to a view from a private Azure Blob Stroage account in ASP.NET Core? Asp.Net Core:使用 ImageSharp 压缩图像并使用 Blob 保存到数据库 - Asp.Net Core : Compress the image using ImageSharp and save to database using Blob 如何从 ASP.NET 核心应用程序将图像上传到 Azure blob 存储中的特定目录? - How do I upload an image to a specific directory in my Azure blob storage from my ASP.NET Core application? 如何将图像的BLOB保存到sql数据库而不先将其保存到文件系统? - How do I save the BLOB of an image into a sql database without saving it to the filesystem first? 如何在 ASP.NET Core 应用程序中显示来自 Azure Blob 存储帐户的图像? - How to I display an image from my Azure Blob Storage Account in my ASP.NET Core Application? 如何使用 Object c# .NET Core 在 blob 存储 azure 上创建 csv 文件? - How can I create a csv file on blob storage azure with Object c# .NET Core? 如何使用 .NET Core 和 EF 将图像文件保存到 SQL 数据库 - How can I save Image file to SQL database with .NET Core and EF ASP.NET Core从Azure和数据库中删除图像Blob - ASP.NET Core delete image blob from azure & from database Azure Blob容器名称可以使用多长时间? - How long can an Azure Blob Container Name be?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM