Python boto3 multipart upload video to aws s3

感情迁移 提交于 2020-05-17 08:49:45

问题


I want to upload a video in multiparts, I have a function that reads a video do some processing on the frame and now instead of writing it to local disk i want to send it to aws s3. I have figured out way to upload in parts using boto3 client

import cv2
import boto3


multipart_upload = MultipartUpload(<AWS_BUCKET_NAME>, <AWS_KEY>)
multipart_upload.open()
video_path = "video.mp4"
cap = cv2.VideoCapture(video_path)
ok = cap.isOpened()
while ok:
    ok,frame = cap.read()

    # do some processing on the frame

    """ then some code to convert these frames into video file writable bytes and collect bytes upto 5MB (specified in aws docs and pass to upload method)"""

    byte_data = frames_to_video_files(frame)  # i am not able to figure out how this part will work

    multipart_upload.upload(byte_data)

multipart_upload.close()


class MultipartUpload:

   def __init__(self,bucket,key):
       self.bucket = bucket
       self.key = key
       self.s3 = boto3.client("s3")
       self.parts = []

   def open(self):
       response = s3.create_multipart_upload(ACL="public-read",Bucket=self.bucket,ContentType="video/mp4",Key=self.key)
       self.upload_id = response["UploadId"]

   def upload(self,bytes_data,part_number):
       response = self.s3.upload_part(Bucket=self.bucket,Key=self.key,Body=bytes_data,PartNumber=part_number,UploadId=self.upload_id)
       self.parts.append({"ETag":response["ETag"],"PartNumber":part_number})

   def close(self):
       response = self.s3.complete_multipart_upload(Bucket=self.bucket,Key=self.key,UploadId=self.upload_id,MultipartUpload={"Parts":self.parts})
       print(response)



来源:https://stackoverflow.com/questions/61696155/python-boto3-multipart-upload-video-to-aws-s3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!