0

我發現這個文檔https://firebase.google.com/docs/storage/gcp-integration#apis火力地堡上傳圖像到火力存儲

下面是代碼從這個文檔

# Import gcloud 
from google.cloud import storage 

# Enable Storage 
client = storage.Client() 

# Reference an existing bucket. 
bucket = client.get_bucket('my-existing-bucket') 

# Upload a local file to a new file to be created in your bucket. 
zebraBlob = bucket.get_blob('zebra.jpg') 
zebraBlob.upload_from_filename(filename='/photos/zoo/zebra.jpg') 

# Download a file from your bucket. 
giraffeBlob = bucket.get_blob('giraffe.jpg') 
giraffeBlob.download_as_string() 

在行client = storage.Client()

說:

無法自動確定憑據。請設置GOOGLE_APPLICATION_CREDENTIALS或明確創建證書並重新運行該應用程序

在接下來的步驟我把

from oauth2client.client import GoogleCredentials 
GOOGLE_APPLICATION_CREDENTIALS = 'credentials.json' 
credentials = GoogleCredentials.get_application_default() 

說:

的應用默認憑據不可用。如果在Google Compute Engine中運行,則可以使用它們。

而我的最終問題是如何在Google Compute Engine中進行身份驗證。

回答

1

問題解決了。

1)您需要安裝https://cloud.google.com/sdk/

2)雲SDK中登錄您的Gmail

3)選擇您的火力點項目

4)在控制檯

gcloud auth application-default login

5.)你可以在這裏看到憑證

C:\ Users \ storks \ AppData \ Roaming \ gcloud \ application_default _credentials.json

6)欲瞭解更多信息請參閱應用程序默認憑據是如何工作的

https://developers.google.com/identity/protocols/application-default-credentials