我想创建客户端应用程序连接AWS Kinesis,也连接邮差/提琴手请求头。
额外补贴:
- ARN格式:ARN:aws: kineesis:region:account-id:stream/stream-name
- 默认的kinesis 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头输入是什么?
答案1:无Bug的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请求和body
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中仔细提取。