1

An ios app calls a .net core web API(post method) to send a few details to save in SQL Database. I have to save this image in a blob and name it as 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

/// <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)

{
"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. How can i accomplish this?

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
Rahul Dev
  • 141
  • 2
  • 17

1 Answers1

1

First add reference to the azure storage client nuget package. Then create a new blob container if not exists( as per your requirement) then get a blob reference with the filename.

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.

And then use the Uploadasync method to upload the filestream to that blob. THe blobclient has the uri property which you can return.

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. the SAS token will be based on the access policy defined by you to that container.

uploading files to blob storage.

Private blobs SAS token.

Aravind
  • 4,125
  • 1
  • 28
  • 39