简体   繁体   English

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

[英]can't download objects from google cloud storage using python

I tried to download objects from google cloud storage using python instead of using google cloud SDK.我尝试使用 python 而不是使用谷歌云 SDK 从谷歌云存储下载对象。 Here is my code:这是我的代码:

#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))

the problem is after I run it, there is nothing happened and no results.问题是我运行后,什么也没发生,也没有结果。 Did I do something wrong here?我在这里做错了吗? Really appreciate any help!真的很感激任何帮助!

TL;DR - You have defined a function in python but have not invoked it. TL;DR - 您已在 python 中定义了一个函数,但尚未调用它。 Calling the function should actually execute the code to pull the blob from your Google Cloud Storage bucket and copy it to your local destination directory.调用该函数实际上应该执行代码以从您的 Google Cloud Storage 存储桶中提取 blob 并将其复制到您的本地目标目录。

Also, you're taking in arguments in your function but are not using them and instead using hard-coded values for blob name, GCS bucket name, destination path.此外,您在函数中接受参数但没有使用它们,而是使用 blob 名称、GCS 存储桶名称、目标路径的硬编码值。 Although this will work, it does defeat the purpose of defining a function in the first place.虽然这会奏效,但它首先违背了定义函数的目的。

Working example工作示例

Here is a working example which uses the arguments in the function to make the call to 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM