从S3通过lambda加载a .mov视频

发布于 2025-02-09 01:58:51 字数 1061 浏览 2 评论 0原文

我正在尝试将.MOV视频文件加载到lambda Python脚本中进行处理。

import json
import urllib.parse
import boto3
import os

print('Loading function')

s3 = boto3.client('s3')


def lambda_handler(event, context):

    # Get the object from the event and show its content type
    bucket = os.environ['SOURCE_BUCKET_NAME']
    key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')

    try:
        video = s3.get_object(Bucket=bucket, Key=key)
        print(video)
        return event
        
    except Exception as e:
        print(e)
        print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
        raise e

“视频”对象内编码是一个字典,包含一个带有值'< botocore.response.streamingbody对象的“正文”键,位于0x7f0c0d4ec310'。我已经尝试了多种读取此内容的方法,包括:

video = video ['body']。read()

返回字节。当我尝试读取带有打开(...)或字节的字节时,我会发现错误:“ Endswith首先必须是字节或字节元组,而不是str”。

我还尝试了

video ['hody']。

video = 。

I am trying to load a .mov video file into a Lambda python script for processing.

import json
import urllib.parse
import boto3
import os

print('Loading function')

s3 = boto3.client('s3')


def lambda_handler(event, context):

    # Get the object from the event and show its content type
    bucket = os.environ['SOURCE_BUCKET_NAME']
    key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')

    try:
        video = s3.get_object(Bucket=bucket, Key=key)
        print(video)
        return event
        
    except Exception as e:
        print(e)
        print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
        raise e

The 'video' object in-code is a dictionary, containing a 'Body' key with the value '<botocore.response.StreamingBody object at 0x7f0c0d4ec310'. I have tried a number of approaches to read this, including:

video = video['Body'].read()

which returns Bytes. When I try to read the bytes with open(...) or BytesIO, I get the error: "endswith first arg must be bytes or a tuple of bytes, not str".

I have also tried

video = video['Body'].read().decode()

which returns error: 'utf-8' codec can't decode byte 0x8e in position 29: invalid start byte.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文