调用AnalyzeDocument操作时,InvalidS3ObjectException:

问题描述

InvalidS3ObjectException,当调用AnalyzeDocument操作时:无法从S3获取对象元数据。检查对象密钥,区域和/或访问权限。” 我不断收到此错误。过度。和。过度。该程序处理了我要引入的测试用例,即带有{“ body”:“ imagename.jpg”}的json。但是,当我尝试利用JS引入的实际代码的那一刻,就会出现此错误。让我感到困惑的是,我已经检查了区域,它们很好。我进入我的帐户,创建了对所有AWS和S3功能具有完全访问权限的用户,并利用这些登录名,使用了我的root帐户,所有内容。我要做的就是从s3存储桶访问图像。为什么不起作用?下面是我的代码。如果我使用上面提供的测试用例,它会起作用,但是当我尝试使用它所连接的网站时,它就无法工作。

def main(event,context):
    key_map,value_map,block_map = get_kv_map(event)   #Take map variables in to get the key and value map we need.

转到此功能...

def get_kv_map(event): 
    filePath = event
    fileExt = filePath.get('body')
    s3 = boto3.resource('s3',region_name='us-east-1')

    bucket = s3.Bucket('react-images-ex')
    obj = bucket.Object(bucket)

    client = boto3.client('textract') #We utilize boto3's textract lib 
    response = client.analyze_document(Document={'S3Object': {'Bucket': 'react-images-ex','Name': fileExt}},FeatureTypes=['FORMS'])
# Get the text blocks
    blocks=response['Blocks']     #We make a blocks variable that will be the blocks we find in the document
# get key and value maps
    key_map = {}
    value_map = {}
    block_map = {}
    for block in blocks:        #Traverse the blocks found in the document
        block_id = block['Id']          #Set variable for blockId to the Id's found on that block location
        block_map[block_id] = block                 #Make the block map at that ID be the block variable
        if block['BlockType'] == "KEY_VALUE_SET":       #if we see that the type of block we're on is a key and value set pair,we check if it's a key or not. If it's not a key,we kNow it's a value. We send it to the respective map. 
            if 'KEY' in block['EntityTypes']:
                key_map[block_id] = block
            else:
                value_map[block_id] = block
    return key_map,block_map                    #Return the maps we need after they're filled.

在此代码正常之前,有人告诉我它应该起作用。那么,为什么我会收到此错误

解决方法

基于评论。

body的问题在于它是json字符串,而不是实际的json对象。

解决方案是将字符串解析为json:

fileExt = json.loads(filePath.get('body'))
,

尝试awscli来查看是否可以在s3中访问图像:

aws s3 ls s3://react-images-ex/<some-fileExt>

或者您错误地解析了fileExt,或者您没有S3权限来访问文件。 awscli命令将有助于验证这一点。