Download multiple file from Google cloud storage using Python

后端 未结 2 992
日久生厌
日久生厌 2021-01-12 20:02

I am trying to download multiple files from the Google cloud storage folder. I am able to download the single file but unable to download multiple files. I took this referen

2条回答
  •  时光取名叫无心
    2021-01-12 20:27

    It looks like you may simply have the wrong level of indentation in your python code. The block beginning with # Retrieve all blobs with a prefix matching the folder is within the scope of the if above so it's never executed if the folder already exists.

    Try this:

    # [download multiple files]
    bucket_name = 'bigquery-hive-load'
    # The "folder" where the files you want to download are
    folder="/projects/bigquery/download/shakespeare/"
    
    # Create this folder locally
    if not os.path.exists(folder):
        os.makedirs(folder)
    
    # Retrieve all blobs with a prefix matching the folder
    bucket=storage_client.get_bucket(bucket_name)
    print(bucket)
    blobs=list(bucket.list_blobs(prefix=folder))
    print(blobs)
    for blob in blobs:
        if(not blob.name.endswith("/")):
            blob.download_to_filename(blob.name)
    
    # [End download to multiple files]
    

提交回复
热议问题