【问题标题】:How to connect AWS Kinesis with C# and postman/fiddler如何将 AWS Kinesis 与 C# 和 postman/fiddler 连接起来
【发布时间】:2021-09-28 00:55:01
【问题描述】:

我想创建客户端应用程序以连接 AWS Kinesis 并连接 postman/fiddler 请求标头。

优惠:

  1. ARN 格式:arn:aws:kinesis:region:account-id:stream/stream-name
  2. 所有的默认 kinesis url:https://kinesis.us-east-1.amazonaws.com

我们必须有 ARN 以及 Key 和 Secret Id。

Kinesis ARN:arn:aws:kinesis:us-east-1:6333775056331:stream/template-v1-kinesis-##00056###-64-sandbox-customerstream###-Vfyubo###

AWS 密钥 ID:##AZG6B52DCC## AWS 密钥:##NG6e#p/##YGuRVEs5jqjwB4i3GLpwaewt##

所以 1> 什么是 C# 代码? 2> HTTP 标头输入是什么?

【问题讨论】:

    标签: amazon-kinesis


    【解决方案1】:

    答案 1:无错误的 C# 代码 安装所需的包

    using Amazon;
    using Amazon.Kinesis;
    using Amazon.Kinesis.Model;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    
         class ConsumerApp
        {
            static readonly string AWS_Key_ID = "AKIAZG6B##S7QZ##5";
            static readonly string AWS_Key_secret = "6iN###/OuYGuRVEs5jqjwB4i3GLpw###";
    
            static readonly AmazonKinesisConfig amza = new AmazonKinesisConfig();
    
            private static readonly AmazonKinesisClient kinesisClient =
                new AmazonKinesisClient(AWS_Key_ID, AWS_Key_secret, RegionEndpoint.USEast1);
    
    private static string GetStringFromMemoryStream(MemoryStream ms)
        {
            ms.Position = 0;
            using (StreamReader sr = new StreamReader(ms))
            {
                return sr.ReadToEnd();
            }
        }
            public static void Main(string[] args)
            {
                const string myStreamName = "template-v1-kinesis-####-64-sandbox-customerstream14D5928D-####";
    
                try
                {
    
                    DescribeStreamRequest describeStreamReq = new DescribeStreamRequest();
                    describeStreamReq.StreamName = myStreamName;
    
                    var describeResult = kinesisClient.DescribeStreamAsync(describeStreamReq).Result;
                    string streamStatus = describeResult.StreamDescription.StreamStatus;
                    Console.Error.WriteLine("  - current state: " + streamStatus);
                    if (streamStatus == StreamStatus.ACTIVE)
                    {
                         GetShardIteratorRequest sR = new GetShardIteratorRequest();
                    foreach (var shardid in describeResult.StreamDescription.Shards)
                    {                    
    
                    sR.ShardId = shardid.ShardId;
                    sR.ShardIteratorType = ShardIteratorType.TRIM_HORIZON;
                    sR.StreamName = myStreamName;
                    GetShardIteratorResponse sresp = kinesisClient.GetShardIterator(sR);
                    GetRecordsRequest req = new GetRecordsRequest();
                    req.ShardIterator = sresp.ShardIterator;
                    //req.Limit = 100;
                    
                   
                    GetRecordsResponse res = kinesisClient.GetRecords(req);
    
                    foreach (var item in res.Records)
                    {
                        Console.WriteLine(item.PartitionKey + " : " + GetStringFromMemoryStream(item.Data));
    
                    }
                    }
    
                    Console.ReadLine();
                    }
                }
                catch (ResourceInUseException)
                {
                    Console.Error.WriteLine("Producer is quitting without creating stream " + myStreamName +
                        " to put records into as a stream of the same name already exists.");
                    Environment.Exit(1);
                }
    
    
            }
        }
    

    2> Fiddler 请求和正文

    POST https://kinesis.us-east-1.amazonaws.com/ HTTP/1.1
    X-Amz-Target: Kinesis_20131202.DescribeStream
    User-Agent: aws-sdk-dotnet-45/2.3.55.2 .NET Runtime/3.-1 .NET Framework/Unknown OS/6.2.9200.0 ClientAsync
    Host: kinesis.us-east-1.amazonaws.com
    X-Amz-Date: 20210925T050415Z
    X-Amz-Content-SHA256: 3fdc5c4caaa0d1cae4451b629c2fe2a56ab4d120a88197####73c62a1d2b88e
    Authorization: AWS4-HMAC-SHA256 Credential=##G6B52DCCPJS7 key id##/20210925/us-east-1/kinesis/aws4_request, SignedHeaders=content-type;host;user-agent;x-amz-content-sha256;x-amz-date;x-amz-target, Signature=##9fb1076aeb342bd4b5ff9bcd131##ba02dcd0316cef4dcb0db882c##
    Accept: application/json
    Connection: Keep-Alive
    Content-Type: application/x-amz-json-1.1
    Content-Length: 95
    Request body
    {"StreamName":"template-v1-kinesis-##-64-sandbox-customerstream1##28D-VfyuboNK##p0"}
        
    

    注意:#需要替换正确的值,您可以从ARN中仔细提取。

    【讨论】:

    • 如果您在提取时遇到问题 # 遵循 3 个步骤 1. 安装并运行 fiddler 以观看 http 请求 2. 运行 c# 程序 3. 您将在 fiddler 中获得 http 请求标头 4. 使用标头和正文和从提琴手跑
    • 上述代码可能不会返回所有数据,因为您需要循环通过分片迭代器。执行GetRecordsResponse res = await kinesisClient.GetRecordsAsync(req);后可以调用res.NextShardIterator获取下一个位置。
    猜你喜欢
    • 2020-11-13
    • 1970-01-01
    • 2010-11-17
    • 2017-10-21
    • 2018-02-09
    • 1970-01-01
    • 2015-10-07
    • 2019-02-26
    • 1970-01-01
    相关资源
    最近更新 更多