Uploading a video file to an AWS S3 Bucket

Hi,

I am trying to upload a video stored on a users local machine to an AWS S3 bucket using a mixture of boto3 and the st.file_uploader component however whenever I try to do this it cannot load the file.

I’ve put the code extracts below (removing security details). If anyone could help point me in the right direction I would REALLY appreciate it.

Thanks in advance!

def uploadMP4ToS3(file, bucket, s3_file):
    s3 = boto3.client('s3',
                      region_name='[REDACTED]',
                      aws_access_key_id='[REDACTED]',
                      aws_secret_access_key='[REDACTED]')
    
    try:
        s3.upload_file(file, bucket, s3_file)
        st.success('File Successfully Uploaded')
        return True
    except FileNotFoundError:
        time.sleep(9)
        st.error('File not found.')
        return False     

c1, c2 = st.columns(2)
c1.subheader("Upload MP4 Video/Audio File")
        uploaded_mp4 = c1.file_uploader("Select an MP4 file")
        
        if uploaded_mp4 is not None:
            
            if uploaded_mp4.type != "video/mp4":
                c1.error('Only MP4 videos are supported. Please upload a different file')
                
            else:
                c1.success(uploaded_mp4.name + ' Selected')
                bytes_data = uploaded_mp4.getvalue()
                if c1.button('Upload'):
                    with st.spinner('Uploading...'):
                        uploadMP4ToS3(uploaded_mp4.name,'[REDACTED]',uploaded_mp4.name)

You might be able to use s3.upload_fileobj, which accepts a file-like object, which is what file_uploader returns. Then you can just pass the uploaded_mp4 object itself, not its name

Thank you so much, this works perfectly! Much appreciated.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.