uploading video with vimeo and django

Viewed 876

i am creating a function to let users submit video's of their product on an e commerce project. The users upload the video through a django form with filefield, I then use django to take the video and send it to Vimeo's API for upload. This is currently working however i have noticed it is really slow. When the user presses submit on the form, with a video of 1 min length (120mb), their waiting over 1 min just to have the form successfully submited.

What would be the best solution to have the upload complete, however still let the user continue browsing our site while it runs in the background?

The video passed to this function is the TemporaryUploadedFile from the form.

def video_upload(video, product_pk):

    #Initial sending, get an upload link

    url = 'https://api.vimeo.com/me/videos'
    headers = {
    'Authorization': 'bearer %s' % settings.VIMEO_TOKEN, 'Content-Type' : 'application/json', 'Accept' : 'application/vnd.vimeo.*+json;version=3.4'}

    data = {'upload' :{ 'approach' : 'tus', 'size' : video.size}, 'name' : str(product_pk)}

    r = requests.post(url, headers=headers, data=json.dumps(data))



    if r.status_code == 200:


        uri = r.json()['uri'] # link to final video
        upload_link = r.json()['upload']['upload_link']
        approach = r.json()['upload']['approach']#should be tus


        if approach == 'tus':

            #first attempt to upload video

            headers = {'Tus-Resumable': '1.0.0', 'Upload-Offset' : '0', 'Content-Type' : 'application/offset+octet-stream', 'Accept' : 'application/vnd.vimeo.*+json;version=3.4'}
            video_rb = video.open('rb')
            r = requests.patch(upload_link, video_rb, headers=headers)


            #Second attempt to upload video



            #Verify upload is complete

            headers = {'Tus-Resumable': '1.0.0',  'Accept' : 'application/vnd.vimeo.*+json;version=3.4'}
            r = requests.head(upload_link, headers=headers)


            return uri

Please note the function is not yet finished as i need to add a bit more logic for second attempt on video upload etc.

Any advice on a more efficient and scaleable way to do this will be much appreciated.

1 Answers

I did something similar to this by setting up an async function with Celery and Redis. That way I could enable to user to move on instantly while content uploaded in the background. Once the content uploaded, I saved the URL to the database somewhere and pushed info to the user the upload completed (or failed). Hope that provides any viewers with potential ways to solve this.

Related