簡體   English   中英

從 Google AI Platform / Vertex AI 中的本地 Jupyter 和筆記本訪問 Google Cloud Storage

[英]Accessing Google Cloud Storage from local Jupyter and Notebooks in Google AI Platform / Vertex AI

問題陳述:讓谷歌雲存儲和一些桶。 需要將此類存儲桶中的數據導入到:

  • 在我的本地計算機上運行的本地 Jupyter 實例
  • 谷歌 Colab 筆記本
  • Vertex AI(和/或 AI 平台)中的 JupyterLab 筆記本

任何能夠處理這些情況的參考代碼將不勝感激。 親切的問候

本地 Jupyter 實例:首先使用gcloud auth login驗證您的本地環境,然后使用gsutil將內容復制到本地環境。

# Authenticate with your account
!gcloud auth login --no-browser

# Copy from your bucket to local path (note -r is for recursive call)
!gsutil cp -r gs://BUCKET/DIR_PATH ./TARGET_DIR

Colab :首先驗證您的 Colab session 以訪問雲 API。然后您可以使用gsutil將內容復制到本地環境。

# Authenticate with your account
from google.colab import auth as google_auth
google_auth.authenticate_user()

# Copy from your bucket to local path (note -r is for recursive call)
!gsutil cp -r gs://BUCKET/DIR_PATH ./TARGET_DIR

Vertex AI 中的 JupyterLab 筆記本:您的環境已經過身份驗證。 使用 gsutil 將內容復制到本地環境。

# Copy from your bucket to local path (note -r is for recursive call)
!gsutil cp -r gs://BUCKET/DIR_PATH ./TARGET_DIR

您還可以使用Cloud Storage 客戶端庫通過 Python 直接訪問 Google Cloud Storage 中的文件。 如上所述,您需要首先對您的環境進行身份驗證。

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

# Instantiates a client
storage_client = storage.Client()

# The name for the new bucket
bucket_name = "my-new-bucket"

# Creates the new bucket
bucket = storage_client.create_bucket(bucket_name)

print(f"Bucket {bucket.name} created.")

暫無
暫無

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

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