<UnknownOperationException/> from aws-textract service endpoint

Viewed 12

I have been working with textract for this past month using boto3, when I tried using the QUERIES feature this would throw an error saying that queries config is not a valid parameter so I decided to go with the amazon service API, but I get as a response;here my code, I;m following the docs to get the signature.

import requests
import boto3
import sys, os, base64, datetime, hashlib, hmac 
import json

session = boto3.Session()
credentials = session.get_credentials()
credentials = credentials.get_frozen_credentials()
access_key = credentials.access_key
secret_key = credentials.secret_key
print()
method = 'POST'
service = 'textract'
amz_target   = 'Textract.StartDocumentAnalysis'
host = 'textract.us-east-1.amazonaws.com'
region = 'us-east-1'
endpoint = 'https://textract.us-east-1.amazonaws.com'
canonical_uri = '/'
content_type = 'application/x-amz-json-1.1'
# request_parameters = 'Action=DescribeRegions&Version=2013-10-15'
def sign(key, msg):
    return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest()

def getSignatureKey(key, dateStamp, regionName, serviceName):
    kDate = sign(('AWS4' + key).encode('utf-8'), dateStamp)
    kRegion = sign(kDate, regionName)
    kService = sign(kRegion, serviceName)
    kSigning = sign(kService, 'aws4_request')
    return kSigning

# 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
amz_target = 'Textract.'
canonical_headers = 'content-type:' + content_type + '\n' + 'host:' + host + '\n' + 'x-amz-date:' + amz_date + '\n' + 'x-amz-target:' + amz_target + '\n'

signed_headers = 'content-type;host;x-amz-date;x-amz-target'
canonical_querystring = ''
payload = {
    
   "DocumentLocation": { 
      "S3Object": { 
         "Bucket": "",
         "Name": "",
      }
   },
   "FeatureTypes": [ "FORMS" ],
   "JobTag": "123",
   "NotificationChannel": { 
      "RoleArn": "",
      "SNSTopicArn": ""
   },
    "QueriesConfig": { 
       "Queries": [ 
          { 
             "Alias": "string",
             "Pages": [ "1-4" ],
             "Text": "what is the policy number ?"
         }
      ]
   }
}
payload_hash = hashlib.sha256(json.dumps(payload).encode('utf-8')).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 = 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()

signing_key = getSignatureKey(secret_key, date_stamp, 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

headers = {'Content-Type':content_type,
           'X-Amz-Date':amz_date,
           'X-Amz-Target':amz_target,
           'Authorization':authorization_header}
print('Request URL = ' + endpoint)


r = requests.post("https://textract.us-east-1.amazonaws.com", data=json.dumps(payload), headers=headers)

print(r.text)
0 Answers
Related