Shrey
Shrey

Reputation: 156

How to download a file from Google Cloud Platform storage

I was reading the python documentation for google cloud storage and was successfully able to create a method that uploads files, however, I am not able to find a way to download files using a blob's URL. I was able to download the file using the filename, but that's not practical since the user could upload files with the same name. The blob is private. I have access to the blob's URL, so I was wondering if there is a way to download files using this link.

This is my upload code which works perfectly:

def upload_blob(bucket_name, filename, file_obj):
    if filename and file_obj:
        storage_client = storage.Client()
        bucket = storage_client.bucket('example-storage-bucket')
        blob = bucket.blob(filename)
        blob.upload_from_file(file_obj) # binary file data
        form_logger.info('File {} uploaded'.format(filename))
        return blob

This code downloads the file, but I could only figure it out with the blob name, not URL:

def download_blob(bucket_name, url):
    if url:
        storage_client = storage.Client()
        bucket = storage_client.bucket('example-storage-bucket')
        blob = bucket.blob(url)
        blob.download_to_filename("example.pdf")

Any suggestions or thoughts on how to download the file using the blob's media link URL?

Upvotes: 6

Views: 10083

Answers (2)

Gautam Savaliya
Gautam Savaliya

Reputation: 1437

For example, bucket example-storage-bucket has file folder/example.pdf and its

Link URL is https://storage.cloud.google.com/example-storage-bucket/folder/example.pdf and URI is gs://example-storage-bucket/folder/example.pdf

Use below function to download blob using GCS link URL(if you are using Python 3.x):

import os
from urllib.parse import urlparse

def decode_gcs_url(url):
    p = urlparse(url)
    path = p.path[1:].split('/', 1)
    bucket, file_path = path[0], path[1] 
    return bucket, file_path

def download_blob(url):
    if url:
        storage_client = storage.Client()
        bucket, file_path = decode_gcs_url(url)
        bucket = storage_client.bucket(bucket)
        blob = bucket.blob(file_path)
        blob.download_to_filename(os.path.basename(file_path))

Upvotes: 6

Mike Schwartz
Mike Schwartz

Reputation: 12155

I think what you're saying is that you want to download the blob to a file whose name is based on the blob name, correct? If so, you can find the blob name in the blob.metadata, and then pick a filename based on that blob name.

Upvotes: 1

Related Questions