如何将 AWS Kinesis 与 C# 和 postman/fiddler 连接起来
How to connect AWS Kinesis with C# and postman/fiddler
我想创建客户端应用程序以连接 AWS Kinesis 并连接 postman/fiddler 请求 header。
条件:
- ARN 格式:arn:aws:kinesis:region:account-id:stream/stream-name
- 所有人的默认运动url:https://kinesis.us-east-1.amazonaws.com
我们必须有 ARN 和密钥以及秘密 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 header 输入是什么?
答案 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中仔细提取。
我想创建客户端应用程序以连接 AWS Kinesis 并连接 postman/fiddler 请求 header。
条件:
- ARN 格式:arn:aws:kinesis:region:account-id:stream/stream-name
- 所有人的默认运动url:https://kinesis.us-east-1.amazonaws.com
我们必须有 ARN 和密钥以及秘密 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 header 输入是什么?
答案 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中仔细提取。