简体   繁体   English

使用 Python 的 Amazon Kinesis Video PutMedia

[英]Amazon Kinesis Video PutMedia Using Python

I'm getting following response when using putMedia api using python.使用 python 使用 putMedia api 时,我收到以下响应。

Response code: 200响应代码:200

{"Output":{"__type":"com.amazon.coral.service#SerializationException","Message":null},"Version":"1.0"} {"输出":{"__type":"com.amazon.coral.service#SerializationException","Message":null},"Version":"1.0"}

I have saved video frames locally and trying to post first frame on the amazon video stream.我已在本地保存视频帧并尝试在亚马逊视频流上发布第一帧。 I have signed using Signing AWS Requests with Signature Version 4 instructions.我已使用Signing AWS Requests with Signature Version 4说明进行签名

Below is my code -下面是我的代码 -

import sys, os, base64, datetime, hashlib, hmac, time 
import requests # pip install requests

# ************* REQUEST VALUES *************
method = 'POST'
service = 'kinesisvideo'
host = 's-1110bf70.kinesisvideo.us-west-2.amazonaws.com'
region = 'us-west-2'
endpoint = 'https://s-1110bf70.kinesisvideo.us-west-2.amazonaws.com'
# POST requests use a content type header. For DynamoDB,
# the content is JSON.
content_type = 'application/json'
start_tmstp = repr(time.time())
localfile = 'mkv-0.jpg';
with open(localfile,'rb') as image:
      request_parameters = image.read()

# 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, date_stamp, regionName, serviceName):
    kDate = sign(('AWS4' + key).encode('utf-8'), date_stamp)
    kRegion = sign(kDate, regionName)
    kService = sign(kRegion, serviceName)
    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.
access_key = '***********'
secret_key = '*************'
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()
amz_date = t.strftime('%Y%m%dT%H%M%SZ')
date_stamp = t.strftime('%Y%m%d') # Date w/o time, used in credential scope


# ************* TASK 1: CREATE A CANONICAL REQUEST *************
# http://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html

# Step 1 is to define the verb (GET, POST, etc.)--already done.

# Step 2: Create canonical URI--the part of the URI from domain to query 
# string (use '/' if no path)
canonical_uri = '/putMedia'

## Step 3: Create the canonical query string. In this example, request
# parameters are passed in the body of the request and the query string
# is blank.
canonical_querystring = ''

# Step 4: Create the canonical headers. Header names must be trimmed
# and lowercase, and sorted in code point order from low to high.
# Note that there is a trailing \n.
canonical_headers = 'content-type:' + content_type + '\n' + 'host:' + host + '\n' + 'x-amz-content-sha256:' + 'UNSIGNED-PAYLOAD' + '\n' + 'x-amz-date:' + amz_date + '\n' + 'x-amz-target:' + amz_target + '\n' + 'x-amzn-fragment-timecode-type:' + 'ABSOLUTE' + '\n' + 'x-amzn-producer-start-timestamp:' + start_tmstp + '\n' + 'x-amzn-stream-name:' + 'ExampleStream' + '\n'

# Step 5: Create the list of signed headers. This lists the headers
# in the canonical_headers list, delimited with ";" and in alpha order.
# Note: The request can include any headers; canonical_headers and
# signed_headers include those that you want to be included in the
# hash of the request. "Host" and "x-amz-date" are always required.
# For DynamoDB, content-type and x-amz-target are also required.
signed_headers = 'content-type;host;x-amz-content-sha256;x-amz-date;x-amz-target;x-amzn-fragment-timecode-type;x-amzn-producer-start-timestamp;x-amzn-stream-name'

# Step 6: Create payload hash. In this example, the payload (body of
# the request) contains the request parameters.

# Step 7: Combine elements to create canonical request
canonical_request = method + '\n' + canonical_uri + '\n' + canonical_querystring + '\n' + canonical_headers + '\n' + signed_headers


# ************* TASK 2: CREATE THE STRING TO SIGN*************
# Match the algorithm to the hashing algorithm you use, either SHA-1 or
# SHA-256 (recommended)
algorithm = 'AWS4-HMAC-SHA256'
credential_scope = date_stamp + '/' + region + '/' + service + '/' + 'aws4_request'
string_to_sign = algorithm + '\n' +  amz_date + '\n' +  credential_scope + '\n' +  hashlib.sha256(canonical_request.encode('utf-8')).hexdigest()

# ************* TASK 3: CALCULATE THE SIGNATURE *************
# Create the signing key using the function defined above.
signing_key = getSignatureKey(secret_key, date_stamp, region, service)

# Sign the string_to_sign using the signing_key
signature = hmac.new(signing_key, (string_to_sign).encode('utf-8'), hashlib.sha256).hexdigest()


# ************* TASK 4: ADD SIGNING INFORMATION TO THE REQUEST *************
# Put the signature information in a header named Authorization.
authorization_header = algorithm + ' ' + 'Credential=' + access_key + '/' + credential_scope + ', ' +  'SignedHeaders=' + signed_headers + ', ' + 'Signature=' + signature

# # Python note: The 'host' header is added automatically by the Python 'requests' library.
headers = {
       'Content-Type':content_type,
           'x-amzn-fragment-timecode-type': 'ABSOLUTE',
           'x-amzn-producer-start-timestamp': start_tmstp,
           'x-amzn-stream-name': 'ExampleStream', 
           'X-Amz-Target':amz_target,
           'x-amz-content-sha256':'UNSIGNED-PAYLOAD',
           '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: %d\n' % r.status_code)
print(r.text)

I have spent around a week on this issue.我在这个问题上花了大约一周的时间。 Please help me.请帮我。

The input of PutMedia is not frames, you will need to provide MKV format input to it. PutMedia 的输入不是帧,您需要为其提供 MKV 格式的输入。 You can refer to a similar example for PutMedia in java here: https://docs.aws.amazon.com/kinesisvideostreams/latest/dg/examples-putmedia.html您可以在此处参考 Java 中 PutMedia 的类似示例: https ://docs.aws.amazon.com/kinesisvideostreams/latest/dg/examples-putmedia.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Amazon Kinesis视频GetMedia / PutMedia - Amazon Kinesis Video GetMedia/PutMedia 如何使用PYTHON实现Amazon Kinesis PutMedia方法 - How to Implement Amazon Kinesis PutMedia Method using PYTHON 亚马逊 AWS Kinesis Video Boto GetMedia/PutMedia - Amazon AWS Kinesis Video Boto GetMedia/PutMedia 如何将从 django 中的 opencv 捕获的实时视频发送到 aws kinesis-video-streams putmedia 端点? - How to send live video captured from opencv in django to aws kinesis-video-streams putmedia endpoint? 如何有效地提取 Amazon Kinesis Video Stream 以进行自定义 Python 处理? - How can you effectively pull an Amazon Kinesis Video Stream for custom python processing? 如何验证Amazon Kinesis Python客户端是否正常工作 - How to verify that Amazon Kinesis Python client is working 使用 Python 解析和渲染 Kinesis Video Streams 并获取输入帧的图像表示 - Using Python to parse and render Kinesis Video Streams and get an image representation of the input frame "从 Python 中的 Amazon Kinesis 输出读取字节图像" - Read a Bytes image from Amazon Kinesis output in python 使用Django和python将视频上传到Amazon S3 - Upload video to amazon s3 using django and python 使用Sagemaker的AWS Kinesis实时视频处理 - AWS Kinesis real time video processing using sagemaker
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM