简体   繁体   中英

how to upload local video to aws s3 using boto3 flask

I am new in flask. I want to upload video in s3 aws using flask. Also, filename changes every time when a new file upload. And there is subfolder in bucket like bucketname/videos I want to upload in videos

def uploadvideo():
finalResult = 'abc.mp4'
s3_bucket_video_url = 'media-storage-beta' + '/videos/' + 'video'
s3 = boto3.client('s3')
response = s3.upload_file(
    finalResult, 'media-storage-beta', 'video')
return 'success'

You need to install boto3 in your virtualenv. Also depends where you are running Flask app. If you trying to upload from local. You need AWS access keys in the ENV variables. You can define a route like this in flask. A form with videotitle and upload file in form is used here.

@app.route('/uploadvideo', methods=['POST'])
def uploadvideo():
    if request.method == 'POST':
        print(request.form['videotitle'])
        f_video = request.files['videofile']
        print(f_video.filename)
        # create video_url
        s3_bucket_video_url = <Main_Folder> + '/videos/' + video_file_name
        s3_client = boto3.client('s3')
        response = s3_client.upload_file(
            f.filename, <BUCKET_NAME>,s3_bucket_video_url)
        return 'Success'

Thanks

Ashish

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM