Call rest api to push data to kinesis using python
I am a newbie in python and aws.I dont know much how to ask questions in stackoverflow.
Please do not block me.
I am trying to send a HTTP Post Request to put record into Amazon Kinesis Stream.
I have created a stream mystream in kinesis. I use method post.
I tried the following link to set up gateway api and it worked fine.
https://docs.aws.amazon.com/kinesis/latest/APIReference/API_CreateStream.html
I am trying to do it with python code using requests.
But i am getting the below mentioned error:
The following is my code:
import sys, os, base64, datetime, hashlib, hmac
import requests # pip install requests
# ************* REQUEST VALUES *************
method = 'POST'
service = 'kinesis'
host = 'kinesis.eu-west-1.amazonaws.com'
region = 'eu-west-1'
endpoint = 'https://kinesis.eu-west-1.amazonaws.com'
content_type = 'application/x-amz-json-1.1'
amz_target = 'Kinesis_20181114.PutRecord'
request_parameters = ''
request_parameters += '"StreamName": mystream,'
request_parameters += '"Data": + base64.b64encode(test) + ,'
request_parameters += '"PartitionKey": 1234 '
request_parameters += ''
# Key derivation functions. See:
# http://docs.aws.amazon.com/general/latest/gr/signature-v4-
examples.html#signature-v4-examples-python
def sign(key, msg):
return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest()
def getSignatureKey(key,datestamp,region,service):
kDate = sign(('AWS4' +key ).encode('utf-8'), datestamp)
kRegion = sign(kDate,region)
kService = sign(kRegion,service)
kSigning = sign(kService, 'aws4_request')
return kSigning
# Read AWS access key from env. variables or configuration file. Best
practice is NOT
# to embed credentials in code.
with open ('C:\Users\Connectm\Desktop\acesskeyid.txt') as f:
contents = f.read().split('n')
access_key = contents[0]
secret_key = contents[1]
if access_key is None or secret_key is None:
print('No access key is available.')
sys.exit()
# Create a date for headers and the credential string
t = datetime.datetime.utcnow()
amzdate = t.strftime('%Y%m%dT%H%M%SZ')
datestamp = t.strftime('%Y%m%d') # Date w/o time, used in credential scope
canonical_uri = '/'
canonical_querystring = ''
canonical_headers = 'content-type:' + content_type + 'n' + 'host:' + host +
'n' + 'x-amz-date:' + amzdate + 'n' + 'x-amz-target:' + amz_target + 'n'
signed_headers = 'content-type;host;x-amz-date;x-amz-target'
payload_hash = hashlib.sha256(request_parameters).hexdigest()
canonical_request = method + 'n' + canonical_uri + 'n' +
canonical_querystring + 'n' + canonical_headers + 'n' + signed_headers +
'n' + payload_hash
algorithm = 'AWS4-HMAC-SHA256'
credential_scope = datestamp + '/' + region + '/' + service + '/' +
'aws4_request'
string_to_sign = algorithm + 'n' + amzdate + 'n' + credential_scope +
'n' + hashlib.sha256(canonical_request).hexdigest()
signing_key = getSignatureKey(secret_key, datestamp, region, service)
signature = hmac.new(signing_key, (string_to_sign).encode('utf-8'),
hashlib.sha256).hexdigest()
authorization_header = algorithm + ' ' + 'Credential=' + access_key + '/' +
credential_scope + ', ' + 'SignedHeaders=' + signed_headers + ', ' +
'Signature=' + signature
print authorization_header;
headers = 'Content-Type':content_type,
'X-Amz-Date':amzdate,
'X-Amz-Target':amz_target,
'Authorization':authorization_header
# ************* SEND THE REQUEST *************
print 'nBEGIN REQUEST++++++++++++++++++++++++++++++++++++'
print 'Request URL = ' + endpoint
r = requests.post(endpoint, data=request_parameters, headers=headers)
print 'nRESPONSE++++++++++++++++++++++++++++++++++++'
print 'Response code: %dn' % r.status_code
print r.text
The following error i am getting
AWS4-HMAC-SHA256 Credential=AKIAI5C357A6YSKQFXEA/20181114/eu-west-
1/kinesis/aws4_request, SignedHeaders=content-type;host;x-amz-date;x-amz-
target,
Signature=1d7d463e77beaf86930806812188180db9cc7cff082663ad547f647a9c6d545a
BEGIN REQUEST++++++++++++++++++++++++++++++++++++
Request URL = https://kinesis.eu-west-1.amazonaws.com
RESPONSE++++++++++++++++++++++++++++++++++++
Response code: 400
"__type":"SerializationException"
Please can someone explain me how i can rectify the above error?
Is the code connecting to the stream?Is there a problem regarding
serialization of data?
python amazon-web-services
add a comment |
I am a newbie in python and aws.I dont know much how to ask questions in stackoverflow.
Please do not block me.
I am trying to send a HTTP Post Request to put record into Amazon Kinesis Stream.
I have created a stream mystream in kinesis. I use method post.
I tried the following link to set up gateway api and it worked fine.
https://docs.aws.amazon.com/kinesis/latest/APIReference/API_CreateStream.html
I am trying to do it with python code using requests.
But i am getting the below mentioned error:
The following is my code:
import sys, os, base64, datetime, hashlib, hmac
import requests # pip install requests
# ************* REQUEST VALUES *************
method = 'POST'
service = 'kinesis'
host = 'kinesis.eu-west-1.amazonaws.com'
region = 'eu-west-1'
endpoint = 'https://kinesis.eu-west-1.amazonaws.com'
content_type = 'application/x-amz-json-1.1'
amz_target = 'Kinesis_20181114.PutRecord'
request_parameters = ''
request_parameters += '"StreamName": mystream,'
request_parameters += '"Data": + base64.b64encode(test) + ,'
request_parameters += '"PartitionKey": 1234 '
request_parameters += ''
# Key derivation functions. See:
# http://docs.aws.amazon.com/general/latest/gr/signature-v4-
examples.html#signature-v4-examples-python
def sign(key, msg):
return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest()
def getSignatureKey(key,datestamp,region,service):
kDate = sign(('AWS4' +key ).encode('utf-8'), datestamp)
kRegion = sign(kDate,region)
kService = sign(kRegion,service)
kSigning = sign(kService, 'aws4_request')
return kSigning
# Read AWS access key from env. variables or configuration file. Best
practice is NOT
# to embed credentials in code.
with open ('C:\Users\Connectm\Desktop\acesskeyid.txt') as f:
contents = f.read().split('n')
access_key = contents[0]
secret_key = contents[1]
if access_key is None or secret_key is None:
print('No access key is available.')
sys.exit()
# Create a date for headers and the credential string
t = datetime.datetime.utcnow()
amzdate = t.strftime('%Y%m%dT%H%M%SZ')
datestamp = t.strftime('%Y%m%d') # Date w/o time, used in credential scope
canonical_uri = '/'
canonical_querystring = ''
canonical_headers = 'content-type:' + content_type + 'n' + 'host:' + host +
'n' + 'x-amz-date:' + amzdate + 'n' + 'x-amz-target:' + amz_target + 'n'
signed_headers = 'content-type;host;x-amz-date;x-amz-target'
payload_hash = hashlib.sha256(request_parameters).hexdigest()
canonical_request = method + 'n' + canonical_uri + 'n' +
canonical_querystring + 'n' + canonical_headers + 'n' + signed_headers +
'n' + payload_hash
algorithm = 'AWS4-HMAC-SHA256'
credential_scope = datestamp + '/' + region + '/' + service + '/' +
'aws4_request'
string_to_sign = algorithm + 'n' + amzdate + 'n' + credential_scope +
'n' + hashlib.sha256(canonical_request).hexdigest()
signing_key = getSignatureKey(secret_key, datestamp, region, service)
signature = hmac.new(signing_key, (string_to_sign).encode('utf-8'),
hashlib.sha256).hexdigest()
authorization_header = algorithm + ' ' + 'Credential=' + access_key + '/' +
credential_scope + ', ' + 'SignedHeaders=' + signed_headers + ', ' +
'Signature=' + signature
print authorization_header;
headers = 'Content-Type':content_type,
'X-Amz-Date':amzdate,
'X-Amz-Target':amz_target,
'Authorization':authorization_header
# ************* SEND THE REQUEST *************
print 'nBEGIN REQUEST++++++++++++++++++++++++++++++++++++'
print 'Request URL = ' + endpoint
r = requests.post(endpoint, data=request_parameters, headers=headers)
print 'nRESPONSE++++++++++++++++++++++++++++++++++++'
print 'Response code: %dn' % r.status_code
print r.text
The following error i am getting
AWS4-HMAC-SHA256 Credential=AKIAI5C357A6YSKQFXEA/20181114/eu-west-
1/kinesis/aws4_request, SignedHeaders=content-type;host;x-amz-date;x-amz-
target,
Signature=1d7d463e77beaf86930806812188180db9cc7cff082663ad547f647a9c6d545a
BEGIN REQUEST++++++++++++++++++++++++++++++++++++
Request URL = https://kinesis.eu-west-1.amazonaws.com
RESPONSE++++++++++++++++++++++++++++++++++++
Response code: 400
"__type":"SerializationException"
Please can someone explain me how i can rectify the above error?
Is the code connecting to the stream?Is there a problem regarding
serialization of data?
python amazon-web-services
I am using python 2.7.
– Himanish Bhattacharjee
Nov 14 '18 at 9:50
add a comment |
I am a newbie in python and aws.I dont know much how to ask questions in stackoverflow.
Please do not block me.
I am trying to send a HTTP Post Request to put record into Amazon Kinesis Stream.
I have created a stream mystream in kinesis. I use method post.
I tried the following link to set up gateway api and it worked fine.
https://docs.aws.amazon.com/kinesis/latest/APIReference/API_CreateStream.html
I am trying to do it with python code using requests.
But i am getting the below mentioned error:
The following is my code:
import sys, os, base64, datetime, hashlib, hmac
import requests # pip install requests
# ************* REQUEST VALUES *************
method = 'POST'
service = 'kinesis'
host = 'kinesis.eu-west-1.amazonaws.com'
region = 'eu-west-1'
endpoint = 'https://kinesis.eu-west-1.amazonaws.com'
content_type = 'application/x-amz-json-1.1'
amz_target = 'Kinesis_20181114.PutRecord'
request_parameters = ''
request_parameters += '"StreamName": mystream,'
request_parameters += '"Data": + base64.b64encode(test) + ,'
request_parameters += '"PartitionKey": 1234 '
request_parameters += ''
# Key derivation functions. See:
# http://docs.aws.amazon.com/general/latest/gr/signature-v4-
examples.html#signature-v4-examples-python
def sign(key, msg):
return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest()
def getSignatureKey(key,datestamp,region,service):
kDate = sign(('AWS4' +key ).encode('utf-8'), datestamp)
kRegion = sign(kDate,region)
kService = sign(kRegion,service)
kSigning = sign(kService, 'aws4_request')
return kSigning
# Read AWS access key from env. variables or configuration file. Best
practice is NOT
# to embed credentials in code.
with open ('C:\Users\Connectm\Desktop\acesskeyid.txt') as f:
contents = f.read().split('n')
access_key = contents[0]
secret_key = contents[1]
if access_key is None or secret_key is None:
print('No access key is available.')
sys.exit()
# Create a date for headers and the credential string
t = datetime.datetime.utcnow()
amzdate = t.strftime('%Y%m%dT%H%M%SZ')
datestamp = t.strftime('%Y%m%d') # Date w/o time, used in credential scope
canonical_uri = '/'
canonical_querystring = ''
canonical_headers = 'content-type:' + content_type + 'n' + 'host:' + host +
'n' + 'x-amz-date:' + amzdate + 'n' + 'x-amz-target:' + amz_target + 'n'
signed_headers = 'content-type;host;x-amz-date;x-amz-target'
payload_hash = hashlib.sha256(request_parameters).hexdigest()
canonical_request = method + 'n' + canonical_uri + 'n' +
canonical_querystring + 'n' + canonical_headers + 'n' + signed_headers +
'n' + payload_hash
algorithm = 'AWS4-HMAC-SHA256'
credential_scope = datestamp + '/' + region + '/' + service + '/' +
'aws4_request'
string_to_sign = algorithm + 'n' + amzdate + 'n' + credential_scope +
'n' + hashlib.sha256(canonical_request).hexdigest()
signing_key = getSignatureKey(secret_key, datestamp, region, service)
signature = hmac.new(signing_key, (string_to_sign).encode('utf-8'),
hashlib.sha256).hexdigest()
authorization_header = algorithm + ' ' + 'Credential=' + access_key + '/' +
credential_scope + ', ' + 'SignedHeaders=' + signed_headers + ', ' +
'Signature=' + signature
print authorization_header;
headers = 'Content-Type':content_type,
'X-Amz-Date':amzdate,
'X-Amz-Target':amz_target,
'Authorization':authorization_header
# ************* SEND THE REQUEST *************
print 'nBEGIN REQUEST++++++++++++++++++++++++++++++++++++'
print 'Request URL = ' + endpoint
r = requests.post(endpoint, data=request_parameters, headers=headers)
print 'nRESPONSE++++++++++++++++++++++++++++++++++++'
print 'Response code: %dn' % r.status_code
print r.text
The following error i am getting
AWS4-HMAC-SHA256 Credential=AKIAI5C357A6YSKQFXEA/20181114/eu-west-
1/kinesis/aws4_request, SignedHeaders=content-type;host;x-amz-date;x-amz-
target,
Signature=1d7d463e77beaf86930806812188180db9cc7cff082663ad547f647a9c6d545a
BEGIN REQUEST++++++++++++++++++++++++++++++++++++
Request URL = https://kinesis.eu-west-1.amazonaws.com
RESPONSE++++++++++++++++++++++++++++++++++++
Response code: 400
"__type":"SerializationException"
Please can someone explain me how i can rectify the above error?
Is the code connecting to the stream?Is there a problem regarding
serialization of data?
python amazon-web-services
I am a newbie in python and aws.I dont know much how to ask questions in stackoverflow.
Please do not block me.
I am trying to send a HTTP Post Request to put record into Amazon Kinesis Stream.
I have created a stream mystream in kinesis. I use method post.
I tried the following link to set up gateway api and it worked fine.
https://docs.aws.amazon.com/kinesis/latest/APIReference/API_CreateStream.html
I am trying to do it with python code using requests.
But i am getting the below mentioned error:
The following is my code:
import sys, os, base64, datetime, hashlib, hmac
import requests # pip install requests
# ************* REQUEST VALUES *************
method = 'POST'
service = 'kinesis'
host = 'kinesis.eu-west-1.amazonaws.com'
region = 'eu-west-1'
endpoint = 'https://kinesis.eu-west-1.amazonaws.com'
content_type = 'application/x-amz-json-1.1'
amz_target = 'Kinesis_20181114.PutRecord'
request_parameters = ''
request_parameters += '"StreamName": mystream,'
request_parameters += '"Data": + base64.b64encode(test) + ,'
request_parameters += '"PartitionKey": 1234 '
request_parameters += ''
# Key derivation functions. See:
# http://docs.aws.amazon.com/general/latest/gr/signature-v4-
examples.html#signature-v4-examples-python
def sign(key, msg):
return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest()
def getSignatureKey(key,datestamp,region,service):
kDate = sign(('AWS4' +key ).encode('utf-8'), datestamp)
kRegion = sign(kDate,region)
kService = sign(kRegion,service)
kSigning = sign(kService, 'aws4_request')
return kSigning
# Read AWS access key from env. variables or configuration file. Best
practice is NOT
# to embed credentials in code.
with open ('C:\Users\Connectm\Desktop\acesskeyid.txt') as f:
contents = f.read().split('n')
access_key = contents[0]
secret_key = contents[1]
if access_key is None or secret_key is None:
print('No access key is available.')
sys.exit()
# Create a date for headers and the credential string
t = datetime.datetime.utcnow()
amzdate = t.strftime('%Y%m%dT%H%M%SZ')
datestamp = t.strftime('%Y%m%d') # Date w/o time, used in credential scope
canonical_uri = '/'
canonical_querystring = ''
canonical_headers = 'content-type:' + content_type + 'n' + 'host:' + host +
'n' + 'x-amz-date:' + amzdate + 'n' + 'x-amz-target:' + amz_target + 'n'
signed_headers = 'content-type;host;x-amz-date;x-amz-target'
payload_hash = hashlib.sha256(request_parameters).hexdigest()
canonical_request = method + 'n' + canonical_uri + 'n' +
canonical_querystring + 'n' + canonical_headers + 'n' + signed_headers +
'n' + payload_hash
algorithm = 'AWS4-HMAC-SHA256'
credential_scope = datestamp + '/' + region + '/' + service + '/' +
'aws4_request'
string_to_sign = algorithm + 'n' + amzdate + 'n' + credential_scope +
'n' + hashlib.sha256(canonical_request).hexdigest()
signing_key = getSignatureKey(secret_key, datestamp, region, service)
signature = hmac.new(signing_key, (string_to_sign).encode('utf-8'),
hashlib.sha256).hexdigest()
authorization_header = algorithm + ' ' + 'Credential=' + access_key + '/' +
credential_scope + ', ' + 'SignedHeaders=' + signed_headers + ', ' +
'Signature=' + signature
print authorization_header;
headers = 'Content-Type':content_type,
'X-Amz-Date':amzdate,
'X-Amz-Target':amz_target,
'Authorization':authorization_header
# ************* SEND THE REQUEST *************
print 'nBEGIN REQUEST++++++++++++++++++++++++++++++++++++'
print 'Request URL = ' + endpoint
r = requests.post(endpoint, data=request_parameters, headers=headers)
print 'nRESPONSE++++++++++++++++++++++++++++++++++++'
print 'Response code: %dn' % r.status_code
print r.text
The following error i am getting
AWS4-HMAC-SHA256 Credential=AKIAI5C357A6YSKQFXEA/20181114/eu-west-
1/kinesis/aws4_request, SignedHeaders=content-type;host;x-amz-date;x-amz-
target,
Signature=1d7d463e77beaf86930806812188180db9cc7cff082663ad547f647a9c6d545a
BEGIN REQUEST++++++++++++++++++++++++++++++++++++
Request URL = https://kinesis.eu-west-1.amazonaws.com
RESPONSE++++++++++++++++++++++++++++++++++++
Response code: 400
"__type":"SerializationException"
Please can someone explain me how i can rectify the above error?
Is the code connecting to the stream?Is there a problem regarding
serialization of data?
python amazon-web-services
python amazon-web-services
asked Nov 14 '18 at 9:38
Himanish BhattacharjeeHimanish Bhattacharjee
2017
2017
I am using python 2.7.
– Himanish Bhattacharjee
Nov 14 '18 at 9:50
add a comment |
I am using python 2.7.
– Himanish Bhattacharjee
Nov 14 '18 at 9:50
I am using python 2.7.
– Himanish Bhattacharjee
Nov 14 '18 at 9:50
I am using python 2.7.
– Himanish Bhattacharjee
Nov 14 '18 at 9:50
add a comment |
1 Answer
1
active
oldest
votes
The fact that you're getting a SerializationException
means your code is working to talk to kinesis but the data you're giving in test
is likely not valid JSON.
That said:
I strongly recommend not doing the requests logic stuff yourself but use the software development kit (SDK) for AWS, called boto3.
import json
import boto3
kinesis = boto3.client("kinesis")
response = kinesis.put_record(
StreamName="my-fancy-kinesis-stream",
Data=json.dumps(
'example': 'payload',
'yay': 'data',
'hello': 'world'
),
PartitionKey="AdjustAsNeeded"
)
print response
This will instantiate a kinesis client using the credentials on your machine (either via instance metadata or ~/.aws/config) or environment variables.
Then it takes a simple dictionary and dumps it into a JSON string for the data.
Lots to say on partition keys that you can find out here.
Also, check out boto3!
Thank you for the help.I am following the following link docs.aws.amazon.com/general/latest/gr/…
– Himanish Bhattacharjee
Nov 14 '18 at 10:02
Yup I saw - don't do that unless you really have to. That logic already exists in a library that Amazon maintains called boto3.
– Randall Hunt
Nov 14 '18 at 10:03
Actually i wanted to create a call rest api for signature aws 4?
– Himanish Bhattacharjee
Nov 14 '18 at 10:05
You don't want to see that. "I am trying to send a HTTP Post Request to put record into Amazon Kinesis Stream." <-- boto3 is the way to accomplish your stated goal it will do all of the signature stuff and request logic for you. I'd avoid dealing with the signature logic yourself because it is very easy to get it wrong.
– Randall Hunt
Nov 14 '18 at 10:07
Ok.Can postman do the job for me?
– Himanish Bhattacharjee
Nov 14 '18 at 10:09
|
show 6 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297024%2fcall-rest-api-to-push-data-to-kinesis-using-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The fact that you're getting a SerializationException
means your code is working to talk to kinesis but the data you're giving in test
is likely not valid JSON.
That said:
I strongly recommend not doing the requests logic stuff yourself but use the software development kit (SDK) for AWS, called boto3.
import json
import boto3
kinesis = boto3.client("kinesis")
response = kinesis.put_record(
StreamName="my-fancy-kinesis-stream",
Data=json.dumps(
'example': 'payload',
'yay': 'data',
'hello': 'world'
),
PartitionKey="AdjustAsNeeded"
)
print response
This will instantiate a kinesis client using the credentials on your machine (either via instance metadata or ~/.aws/config) or environment variables.
Then it takes a simple dictionary and dumps it into a JSON string for the data.
Lots to say on partition keys that you can find out here.
Also, check out boto3!
Thank you for the help.I am following the following link docs.aws.amazon.com/general/latest/gr/…
– Himanish Bhattacharjee
Nov 14 '18 at 10:02
Yup I saw - don't do that unless you really have to. That logic already exists in a library that Amazon maintains called boto3.
– Randall Hunt
Nov 14 '18 at 10:03
Actually i wanted to create a call rest api for signature aws 4?
– Himanish Bhattacharjee
Nov 14 '18 at 10:05
You don't want to see that. "I am trying to send a HTTP Post Request to put record into Amazon Kinesis Stream." <-- boto3 is the way to accomplish your stated goal it will do all of the signature stuff and request logic for you. I'd avoid dealing with the signature logic yourself because it is very easy to get it wrong.
– Randall Hunt
Nov 14 '18 at 10:07
Ok.Can postman do the job for me?
– Himanish Bhattacharjee
Nov 14 '18 at 10:09
|
show 6 more comments
The fact that you're getting a SerializationException
means your code is working to talk to kinesis but the data you're giving in test
is likely not valid JSON.
That said:
I strongly recommend not doing the requests logic stuff yourself but use the software development kit (SDK) for AWS, called boto3.
import json
import boto3
kinesis = boto3.client("kinesis")
response = kinesis.put_record(
StreamName="my-fancy-kinesis-stream",
Data=json.dumps(
'example': 'payload',
'yay': 'data',
'hello': 'world'
),
PartitionKey="AdjustAsNeeded"
)
print response
This will instantiate a kinesis client using the credentials on your machine (either via instance metadata or ~/.aws/config) or environment variables.
Then it takes a simple dictionary and dumps it into a JSON string for the data.
Lots to say on partition keys that you can find out here.
Also, check out boto3!
Thank you for the help.I am following the following link docs.aws.amazon.com/general/latest/gr/…
– Himanish Bhattacharjee
Nov 14 '18 at 10:02
Yup I saw - don't do that unless you really have to. That logic already exists in a library that Amazon maintains called boto3.
– Randall Hunt
Nov 14 '18 at 10:03
Actually i wanted to create a call rest api for signature aws 4?
– Himanish Bhattacharjee
Nov 14 '18 at 10:05
You don't want to see that. "I am trying to send a HTTP Post Request to put record into Amazon Kinesis Stream." <-- boto3 is the way to accomplish your stated goal it will do all of the signature stuff and request logic for you. I'd avoid dealing with the signature logic yourself because it is very easy to get it wrong.
– Randall Hunt
Nov 14 '18 at 10:07
Ok.Can postman do the job for me?
– Himanish Bhattacharjee
Nov 14 '18 at 10:09
|
show 6 more comments
The fact that you're getting a SerializationException
means your code is working to talk to kinesis but the data you're giving in test
is likely not valid JSON.
That said:
I strongly recommend not doing the requests logic stuff yourself but use the software development kit (SDK) for AWS, called boto3.
import json
import boto3
kinesis = boto3.client("kinesis")
response = kinesis.put_record(
StreamName="my-fancy-kinesis-stream",
Data=json.dumps(
'example': 'payload',
'yay': 'data',
'hello': 'world'
),
PartitionKey="AdjustAsNeeded"
)
print response
This will instantiate a kinesis client using the credentials on your machine (either via instance metadata or ~/.aws/config) or environment variables.
Then it takes a simple dictionary and dumps it into a JSON string for the data.
Lots to say on partition keys that you can find out here.
Also, check out boto3!
The fact that you're getting a SerializationException
means your code is working to talk to kinesis but the data you're giving in test
is likely not valid JSON.
That said:
I strongly recommend not doing the requests logic stuff yourself but use the software development kit (SDK) for AWS, called boto3.
import json
import boto3
kinesis = boto3.client("kinesis")
response = kinesis.put_record(
StreamName="my-fancy-kinesis-stream",
Data=json.dumps(
'example': 'payload',
'yay': 'data',
'hello': 'world'
),
PartitionKey="AdjustAsNeeded"
)
print response
This will instantiate a kinesis client using the credentials on your machine (either via instance metadata or ~/.aws/config) or environment variables.
Then it takes a simple dictionary and dumps it into a JSON string for the data.
Lots to say on partition keys that you can find out here.
Also, check out boto3!
edited Nov 14 '18 at 10:56
answered Nov 14 '18 at 9:53
Randall HuntRandall Hunt
7,25442436
7,25442436
Thank you for the help.I am following the following link docs.aws.amazon.com/general/latest/gr/…
– Himanish Bhattacharjee
Nov 14 '18 at 10:02
Yup I saw - don't do that unless you really have to. That logic already exists in a library that Amazon maintains called boto3.
– Randall Hunt
Nov 14 '18 at 10:03
Actually i wanted to create a call rest api for signature aws 4?
– Himanish Bhattacharjee
Nov 14 '18 at 10:05
You don't want to see that. "I am trying to send a HTTP Post Request to put record into Amazon Kinesis Stream." <-- boto3 is the way to accomplish your stated goal it will do all of the signature stuff and request logic for you. I'd avoid dealing with the signature logic yourself because it is very easy to get it wrong.
– Randall Hunt
Nov 14 '18 at 10:07
Ok.Can postman do the job for me?
– Himanish Bhattacharjee
Nov 14 '18 at 10:09
|
show 6 more comments
Thank you for the help.I am following the following link docs.aws.amazon.com/general/latest/gr/…
– Himanish Bhattacharjee
Nov 14 '18 at 10:02
Yup I saw - don't do that unless you really have to. That logic already exists in a library that Amazon maintains called boto3.
– Randall Hunt
Nov 14 '18 at 10:03
Actually i wanted to create a call rest api for signature aws 4?
– Himanish Bhattacharjee
Nov 14 '18 at 10:05
You don't want to see that. "I am trying to send a HTTP Post Request to put record into Amazon Kinesis Stream." <-- boto3 is the way to accomplish your stated goal it will do all of the signature stuff and request logic for you. I'd avoid dealing with the signature logic yourself because it is very easy to get it wrong.
– Randall Hunt
Nov 14 '18 at 10:07
Ok.Can postman do the job for me?
– Himanish Bhattacharjee
Nov 14 '18 at 10:09
Thank you for the help.I am following the following link docs.aws.amazon.com/general/latest/gr/…
– Himanish Bhattacharjee
Nov 14 '18 at 10:02
Thank you for the help.I am following the following link docs.aws.amazon.com/general/latest/gr/…
– Himanish Bhattacharjee
Nov 14 '18 at 10:02
Yup I saw - don't do that unless you really have to. That logic already exists in a library that Amazon maintains called boto3.
– Randall Hunt
Nov 14 '18 at 10:03
Yup I saw - don't do that unless you really have to. That logic already exists in a library that Amazon maintains called boto3.
– Randall Hunt
Nov 14 '18 at 10:03
Actually i wanted to create a call rest api for signature aws 4?
– Himanish Bhattacharjee
Nov 14 '18 at 10:05
Actually i wanted to create a call rest api for signature aws 4?
– Himanish Bhattacharjee
Nov 14 '18 at 10:05
You don't want to see that. "I am trying to send a HTTP Post Request to put record into Amazon Kinesis Stream." <-- boto3 is the way to accomplish your stated goal it will do all of the signature stuff and request logic for you. I'd avoid dealing with the signature logic yourself because it is very easy to get it wrong.
– Randall Hunt
Nov 14 '18 at 10:07
You don't want to see that. "I am trying to send a HTTP Post Request to put record into Amazon Kinesis Stream." <-- boto3 is the way to accomplish your stated goal it will do all of the signature stuff and request logic for you. I'd avoid dealing with the signature logic yourself because it is very easy to get it wrong.
– Randall Hunt
Nov 14 '18 at 10:07
Ok.Can postman do the job for me?
– Himanish Bhattacharjee
Nov 14 '18 at 10:09
Ok.Can postman do the job for me?
– Himanish Bhattacharjee
Nov 14 '18 at 10:09
|
show 6 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297024%2fcall-rest-api-to-push-data-to-kinesis-using-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I am using python 2.7.
– Himanish Bhattacharjee
Nov 14 '18 at 9:50