无法使用python从谷歌云存储中下载对象

时间:2017-07-23 15:32:14

标签: python anaconda google-cloud-storage

我尝试使用python而不是使用谷歌云SDK从谷歌云存储下载对象。这是我的代码:

#Imports the Google Cloud client library
from google.cloud import storage
from google.cloud.storage import Blob

# Downloads a blob from the bucket
def download_blob(bucket_name, source_blob_name, destination_file_name):
    storage_client = storage.Client()
    bucket = storage_client.get_bucket('sora_mue')
    blob = bucket.blob('01N3P*.ubx')
    blob.download_to_filename('C:\\Users\\USER\\Desktop\\Cloud')

    print ('Blob {} downloaded to {}.'.format(source_blob_name,
                                             destination_file_name))
问题是在我运行之后,没有任何事情发生也没有结果。我在这里做错了吗?非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

TL; DR - 您已在python中定义了一个函数但尚未调用它。调用该函数应该实际执行代码以从您的Google云端存储桶中提取blob并将其复制到本地目标目录。

此外,您正在接受函数中的参数但不使用它们,而是使用blob名称,GCS存储桶名称,目标路径的硬编码值。虽然这样可行,但它确实首先破坏了定义函数的目的。

工作示例

这是一个工作示例,它使用函数中的参数来调用GCS。

from google.cloud import storage

# Define a function to download the blob from GCS to local destination
def download_blob(bucket_name, source_blob_name, destination_file_name):
  storage_client = storage.Client()
  bucket = storage_client.get_bucket(bucket_name)
  blob = bucket.blob(source_blob_name)
  blob.download_to_filename(destination_file_name)
  print ('Blob {} downloaded to {}.'.format(source_blob_name, destination_file_name))

# Call the function to download blob '01N3P*.ubx' from GCS bucket
# 'sora_mue' to local destination path 'C:\\Users\\USER\\Desktop\\Cloud'
download_blob('sora_mue', '01N3P*.ubx', 'C:\\Users\\USER\\Desktop\\Cloud')
# Will print
# Blob 01N3P*.ubx downloaded to C:\Users\USER\Desktop\Cloud.