Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 684
Shrey Avatar asked Jul 05 '20 20:07

Shrey


People also ask

Can you download directly to cloud storage?

It is possible to download directly to cloud drive storage, they have to make use of third-party software to complete the task. That is to say, Google Drive itself can't download files directly to itself from websites or other clouds. This application is called MultCloud.

How do I download files from Google Cloud SSH?

In the Google Cloud console, go to the VM instances page. In the list of virtual machine instances, click SSH in the row of the instance that you want to connect to. After the connection is established, click the download icon download.


2 Answers

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))
like image 131
Gautam Savaliya Avatar answered Oct 04 '22 03:10

Gautam Savaliya


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.

like image 26
Mike Schwartz Avatar answered Oct 04 '22 04:10

Mike Schwartz