调用AnalyzeDocument操作时InvalidS3ObjectException:

2024-03-29 13:28:20 发布

您现在位置:Python中文网/ 问答频道 /正文

调用AnalyzeDocument操作时InvalidS3ObjectException:无法从S3获取对象元数据。检查对象密钥、区域和/或访问权限。“ 我一直收到这个错误。结束。和。结束。这个程序与我引入的测试用例一起工作,json带有一个{“body”:“imagename.jpg”}.但就在我试图利用JS带来的实际代码的那一刻,我发现了这个错误。让我困惑的是,我已经检查了这些区域,它们都很好。我进入我的帐户,创建了可以完全访问所有AWS和S3功能的用户,并使用了这些登录,我使用了我的根帐户,一切。我所要做的就是访问从我的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, value_map, block_map                    #Return the maps we need after they're filled.

以前有人告诉过我,这个代码很好,应该可以工作。那么,为什么我会出现这个错误呢


Tags: thekeyineventidmapgetif
2条回答

根据评论

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

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

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

尝试awscli查看是否可以访问s3中的映像:

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

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

相关问题 更多 >