Dannick Montanede
Dannick Montanede

Reputation: 119

how to upload a video to google cloud storage in python

I used an AppEngine application and store all my files in Cloud storage.

I tried to store video too but I can't.

I don't understand how Resumable upload works and how to set it up.

Upvotes: 0

Views: 859

Answers (1)

GAEfan
GAEfan

Reputation: 11370

Something like this should work:

import cloudstorage as gcs

DEFAULT_GCS_BUCKET = {your bucket}
id ={some id}

video = request.files['video']

# to see what these do: https://cloud.google.com/appengine/docs/python/googlecloudstorageclient/retryparams_class
write_retry_params = gcs.RetryParams(   initial_delay =     0.2,
                                            backoff_factor =    2,
                                            max_delay =         5.0,
                                            min_retries =       1,
                                            max_retries =       3,
                                            max_retry_period =  15,
                                            urlfetch_timeout =  10
                                        )


video_bytes = video.read()
content_type = video.content_type

with gcs.open(
        filename =      '{}/{}'.format(DEFAULT_GCS_BUCKET', id), 
        mode =          'w', 
        content_type =  content_type, 
        options=        {  
                            'x-goog-acl': 'public-read', 
                            'Cache-Control': 'private, max-age=0, no-transform'
                        },
        retry_params =  write_retry_params
    ) as f:
    f.write(video_bytes)

Upvotes: 1

Related Questions