如何将映像保存到 Azure Blob 并使用特定字段命名,并返回 Blob URL 并将其保存到 .net core



ios 应用程序调用 .net core Web API(post 方法(来发送一些详细信息以保存在 SQL 数据库中。我必须将此图像保存在 blob 中并将其命名为 EmpID_PunchInDate_PunchInTime.png

我有以下代码,但我无法弄清楚如何在 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." });
}

传入的请求以 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"
}

因此,图像应另存为 1000_10232019_1323.png 在 Azure Blob 中,应返回 Blob URL。我怎样才能做到这一点?

首先添加对 Azure 存储客户端 nuget 包的引用。然后创建一个新的 blob 容器(如果不存在((根据您的要求(,然后获取带有文件名的 blob 引用。

您需要通过选择所需的字段并将它们连接成字符串,从您获得的输入请求中创建文件名。然后,使用该文件名创建 Blob 容器引用。

然后使用上传异步方法将文件流上传到该 Blob。blob 客户端具有可以返回的 uri 属性。

另请注意,如果这是一个专用 Blob,并且你要将此 URL 共享给最终用户,则必须创建 Blob URI,并将 SAS(共享访问签名(令牌追加到 URL,以便他们可以使用该 URL 直接下载文件。 SAS 令牌将基于你定义的对该容器的访问策略。

将文件上传到 Blob 存储。

专用 blob SAS 令牌。

最新更新