在谷歌AI平台/Vertex AI中从本地Jupyter和笔记本电脑访问谷歌云存储



问题陈述:使用带有一些Bucket的Google云存储。需要将数据从这样的桶导入:

  • 在本地计算机上运行的本地Jupyter实例
  • 谷歌Colab笔记本
  • Vertex AI(和/或AI平台(中的JupyterLab笔记本

任何能够在这些情况下使用的参考代码都将不胜感激。向致以诚挚的问候

本地Jupyter实例:首先使用gcloud auth登录验证本地env,然后使用gsutil将内容复制到本地env。

# 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会话以访问云API。然后您可以使用gsutil将内容复制到本地env。

# 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笔记本:您的env已通过身份验证。使用gsutil将内容复制到本地env。

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

您还可以使用云存储客户端库,通过Python直接访问Google云存储中的文件。如上所述,您需要首先验证您的环境。

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

最新更新