Upload file to cloud storage from request without saving it locally

I am trying to upload a file to google cloud storage using the API which I build on Flask (python) but I don’t want to save the file locally. Directly take the file from request and upload it to cloud storage.

Upload to GCS method

def upload_to_bucket(blob_name, path_to_file):
    """ Upload data to a bucket"""
    storage_client = storage.Client()

    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(blob_name)
    blob.upload_from_filename(path_to_file)

    #returns a public url
    return blob.public_url

Get the file object from request body

file = request.files[‘file’]

This is how I am calling the method

url = upload_to_bucket(file.name, file)

But here it returns with Attributer error. I tried file.value, file.file but that didn’t work.

Source: Python Questions

LEAVE A COMMENT