Question: I want to know how a file is read if sent to an end point.
After reading multiple articles and doing some research I am able to send a file to Amazon S3 bucket. Below is the working code
but I don't understand how a file is sent to my API via Postman and how processing happens, how this file is being read in the code. Can someone please help me decode this code.
I have added line numbers to the code I want to understand.
Line
Number
1 [httppost]
2 public async Task<bool> Upload()
{
try
{
3 var filesReadToProvider = await Request.Content.ReadAsMultipartAsync();
4 foreach (var content in filesReadToProvider.Contents)
{
5 var stream = await content.ReadAsStreamAsync();
6 using (StreamReader sr = new StreamReader(stream))
{
string line = "";
7 while ((line = sr.ReadLine()) != null)
{
8 using (MemoryStream outputStream = new MemoryStream())
9 using (StreamWriter sw = new StreamWriter(outputStream))
{
sw.WriteLine(line);
10 sw.Flush();
PutRecordRequest putRecord = new PutRecordRequest();
putRecord.DeliveryStreamName = myStreamName;
Record record = new Record();
11 outputStream.Position = 0;
record.Data = outputStream;
putRecord.Record = record;
try
{
await kinesisClient.PutRecordAsync(putRecord);
}
catch (Exception ex)
{
Console.WriteLine("Failed to send record to Kinesis. Exception: {0}", ex.Message);
}
}
}
}
}
}
catch (Exception e)
{
}
return true;
}
P.S: I am sending a file via Postman for testing the working of this end point.
My undertanding of above code.
Line Number 1:
It's a HTTPPOST request, so that means the content I am sending will be in the body of the request and not in the headers or the URL. Since I am sending a file, the file content will be sent in the body of the request.
Line Number 2:
asyn so it will be run on a separate thread so my UI (if any) will be responsive.
Line Number 3:
I am guessing Request is basically a handle to read the body content for any API Requests, so I am reading the content using Request.Content
,
Question: What is the meaning of ReadAsMultipartAsync
: it says: Reads all body parts within a MIME multipart message and produces a set of System.Net.Http.HttpContent instances as a result.
? What this means? Does this mean the if my file has 10 lines, it is reading all 10 lines and kind of storing it in the memory (in layman terms)?
Line Number 4:
I am guessing filesReadToProvider.Contents
is again a handle for all those 10 lines that I have in file. Does that mean if I have 10 lines in my file this will loop will run for 10 times (I think I am wrong here).
Line Number 5:
ReadAsStreamAsync
it says: Serialize the HTTP content and return a stream that represents the content as an asynchronous operation.
. Does this mean that it is reading 1 line at a time and serializing it? But why it is serializing the lines?
LineNumber 6:
Since I have streams to deal with I am creating a streamreader instance and passing the stream to it. (in layman terms: I am passing one line of file that I am reading)?
Beyond this I am lost. I am totally confused after this. What is a stream? Is it one line in my file?
Question: Please someone help me in understading what is happening in above code. I don't think I can directly link a line of file with a stream. Please guide me.