【问题标题】:Update Text File Using Lambda使用 Lambda 更新文本文件
【发布时间】:2019-01-26 15:53:56
【问题描述】:

我希望能够在将图像上传到 s3 存储桶时更新文本文件。此文本文件的每一行都将包含 Amazon Rekognition 的结果。但是,我写的代码不能正常工作

bucket_name = "update-my-text-file"
rekognition = boto3.client('rekognition')
s3 = boto3.resource('s3')
bucket = s3.Bucket(bucket_name)

def handle_image(key):
    response = rekognition.detect_labels(
        Image={
            'S3Object': {
                'Bucket': bucket_name,
                'Name': key
            }
        }
    )
    return response


def lambda_handler(event, context):

    file_name = 'results.txt'
    object = s3.Object(bucket_name, 'tmp/results.txt')

    cli = boto3.client('s3')
    response = cli.get_object(Bucket=bucket_name, Key='tmp/results.txt')
    data = response['Body'].read()
    print('the data is ' + data)

    key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key'].encode('utf8'))
    response = handle_image(key)
    print('the response is: ' + response)

    object.put(Body=data + '/n' + response)

【问题讨论】:

  • 这太模糊了。究竟会发生什么,这与您的预期有何不同?

标签: python amazon-web-services amazon-s3 aws-lambda


【解决方案1】:

您可能会发现这样下载文件更容易:

import boto3
s3_client = boto3.client('s3')
s3_client.download_file('mybucket', 'hello.txt', '/tmp/hello.txt')

然后您可以根据需要读/写本地文件。然后,再次上传:

s3_client.upload_file('/tmp/hello.txt', 'mybucket', 'hello.txt')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 2013-07-18
    • 1970-01-01
    • 2022-01-06
    相关资源
    最近更新 更多