Unbreakable
Unbreakable

Reputation: 8084

How to connect to Amazon Kinesis Firehose in ASP.Net Core 2.2 Web Api Application

I am a beginner in AWS and I want to send a sample data to s3 bucket using Amazon Kinesis from ASP.Net Core 2.2 Web Api Application. But I am unable to send data. Below is what I have tried. Steps I did:

  1. Created an AWS account and then created one s3 bucket.

  2. Created a Kinesis account and linked the s3 bucket to it.

3. In Main

  public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
        Amazon.Util.ProfileManager.RegisterProfile("demo-aws-profile", "MyAccessKeyId", "MySecretKey");
    }

Question 1: What am I suppose to pass in place of "demo-aws-profile"? Can it be any random name?

Question 2: Is there anything else is needed to connect to AWS?

Code Snippet

        [HttpGet]
        public ActionResult<IEnumerable<string>> Get()
        {
            var o = new
            {
                Message = "Hello World"
            };
            byte[] oByte = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(o));
            AmazonKinesisConfig config = new AmazonKinesisConfig();
            config.RegionEndpoint = Amazon.RegionEndpoint.USEast1;
           // QUESTION 3: DO I NEED TO SET ANY OTHER PROPERTY IN CONFIG??
            var client = new AmazonKinesisClient(config);
            try
            {
                using (MemoryStream ms = new MemoryStream(oByte))
                {
                    PutRecordRequest requestRecord = new PutRecordRequest();
                    // QUESTION 4: What is this stream name??? 
                    requestRecord.StreamName = "test-stream";
                    requestRecord.Data = ms;
                    var response = client.PutRecordAsync(requestRecord);
                    response.Wait();
                    return Ok(new
                    {
                        seq = response.Result.SequenceNumber
                    });
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
            return new string[] { "value1", "value2" };
        }

I am getting an exception as System.Threading.Tasks.TaskCanceledException: A task was canceled.

P.S: I am a beginner and I could have done any basic mistake, so please let me know if I should provide any further details. I still think that I am not able to communicate with Kinesis and hence to my s3 bucket. Am I doing something wrong or missing some set up here.

Upvotes: 0

Views: 2971

Answers (1)

programmerj
programmerj

Reputation: 1684

Try passing in your AccessKeyId, SecretAccessKey, and Region directly to the constructor as a test (you don't ever want to hard code these in a real release). Make sure the user associated with these credentials has a policy configured to allow access to Kinesis.

Also use async/await.

    [HttpGet]
    public async Task ActionResult<IEnumerable<string>> Get()
    {
        var o = new
        {
            Message = "Hello World"
        };
        byte[] oByte = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(o));

        var client = new AmazonKinesisClient(<AccessKeyId>, <SecretAccessKey>, <Region>);
        try
        {
            using (MemoryStream ms = new MemoryStream(oByte))
            {
                PutRecordRequest requestRecord = new PutRecordRequest();
                // QUESTION 4: What is this stream name??? 
                requestRecord.StreamName = <your Kinesis stream name>;
                requestRecord.Data = ms;
                var response = await client.PutRecordAsync(requestRecord);
                return Ok(new
                {
                    seq = response.Result.SequenceNumber
                });
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
        return new string[] { "value1", "value2" };
    }

Upvotes: 3

Related Questions