簡體   English   中英

無法使用 python 從谷歌雲存儲下載對象

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

我嘗試使用 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))

問題是我運行后,什么也沒發生,也沒有結果。 我在這里做錯了嗎? 真的很感激任何幫助!

TL;DR - 您已在 python 中定義了一個函數,但尚未調用它。 調用該函數實際上應該執行代碼以從您的 Google Cloud Storage 存儲桶中提取 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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM