/
PutRecordv2.go
62 lines (51 loc) · 1.87 KB
/
PutRecordv2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX - License - Identifier: Apache - 2.0
// snippet-start:[kinesis.go-v2.PutRecord]
package main
import (
"context"
"flag"
"fmt"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/kinesis"
)
// KinesisPutRecordAPI defines the interface for the PutRecord function.
// We use this interface to test the function using a mocked service.
type KinesisPutRecordAPI interface {
PutRecord(ctx context.Context,
params *kinesis.PutRecordInput,
optFns ...func(*kinesis.Options)) (*kinesis.PutRecordOutput, error)
}
// MakePutRecord creates an Amazon Kinesis (Amazon Kinesis) stream record.
// Inputs:
// c is the context of the method call, which includes the AWS Region.
// api is the interface that defines the method call.
// input defines the input arguments to the service call.
// Output:
// If success, a PutRecordOutput object containing the result of the service call and nil.
// Otherwise, nil and an error from the call to PutRecordOutput.
func MakePutRecord(c context.Context, api KinesisPutRecordAPI, input *kinesis.PutRecordInput) (*kinesis.PutRecordOutput, error) {
return api.PutRecord(c, input)
}
func main() {
stream := flag.String("s", "", "The name of the stream")
partition := flag.String("k", "", "The identifier of the partition key")
payload := flag.String("p", "", "The payload")
flag.Parse()
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
panic("configuration error, " + err.Error())
}
client := kinesis.NewFromConfig(cfg)
input := &kinesis.PutRecordInput{
Data: []byte(*payload),
PartitionKey: partition,
StreamName: stream,
}
results, err := MakePutRecord(context.TODO(), client, input)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(results.SequenceNumber)
}
// snippet-end:[kinesis.go-v2.PutRecord]