如何使用python从google-cloud-platform下载数据?

艾伦

我将数据存储在Google Cloud Platform上,并且希望能够在本地下载它,这是我第一次尝试,最终我将在我的python代码中使用下载的数据。

我已经检查了文档,例如https://cloud.google.com/genomics/downloading-credentials-for-api-accesshttps://cloud.google.com/storage/docs/cloud-console,我都已成功获取我的第一个链接的Json文件,第二个是您在挣扎的地方,我使用的是python 3.5,并假设我的json文件名是data.json我添加了以下代码:

os.environ["file"] = "data.json"
urllib.request.urlopen('https://storage.googleapis.com/[bucket_name]/[filename]')

首先,我什至不知道我应该在环境附近调用什么值,所以我只是将其称为文件,不确定我应该如何填充它,并且第二行访问被拒绝,显然这不是如何下载我的文件,因为没有目标本地存储库或该命令中的任何内容,将不胜感激。

编辑:

   from google.cloud.storage import Blob

    os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "credentials/client_secret.json"
    storage_client = storage.Client.from_service_account_json('service_account.json')
    client = storage.Client(project='my-project')
    bucket = client.get_bucket('my-bucket')
    blob = Blob('path/to/my-object', bucket)
    download_to_filename('local/path/to/my-file')

我得到的尚未解决的参考storagedownload_to_filename和我应更换service_account.jsoncredentials/client_secret.json另外,我尝试打印os.environ["GOOGLE_APPLICATION_CREDENTIALS"]['installed']与任何Json一样的内容,但它只是说我应该给出数字,这意味着它仅以常规文本形式读取输入路径。

乔佛尔

您应该使用惯用的Google Cloud库在GCS中运行操作。

有了示例,并知道客户端库将获得应用程序默认凭据,首先我们必须使用以下命令设置应用程序默认凭据:

gcloud auth application-default login 

===编辑===

那是老方法。现在,您应该使用此链接中的说明

这意味着从控制台下载服务帐户密钥文件,并将环境变量设置GOOGLE_APPLICATION_CREDENTIALS为下载的JSON的路径。

另外,请确保该服务帐户对存储桶的项目具有适当的权限

或者,您可以使用显式凭据创建客户端。您需要全部下载密钥文件,但是在创建客户端时,请使用:

storage_client = storage.Client.from_service_account_json('service_account.json')

==========

然后,按照示例代码:

from  google.cloud import storage

client = storage.Client(project='project-id')
bucket = client.get_bucket('bucket-id')
blob = storage.Blob('bucket/file/path', bucket)
blob.download_to_filename('/path/to/local/save')

或者,如果这是一次性下载,则只需安装SDK并用于gsutil下载:

gsutil cp gs://bucket/file .

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Google Cloud Platform数据显示

需要帮助以从Google Cloud Platform批量下载日志-Cloud Logging

如何从Google Cloud Platform存储下载文件

使用 Python 的 Google Cloud Platform SDK

如何在Google Cloud Platform中配置特定的数据管道?

如何将 Kaggle 数据集导入 Google Cloud Platform

如何从Google Cloud Platform Java Flexible Environment下载应用程序

如何使用Go SDK for Google Cloud Platform获取项目元数据?

Google Cloud Platform:如何重命名Google Cloud Platform项目?

如何将数据从 Google Cloud Platform (BigQuery/Cloud SQL) 导入 R?

Google Cloud-将数据从存储桶下载到实例

Kafka到Google Cloud Platform的数据流提取

无法在 StreamSets Data Collector 中从 Google Cloud Platform 读取数据

如何使用curl从Google Cloud Storage下载文件

Google Cloud Messaging VS Google Cloud Platform

Google Cloud Platform AI Notebook - 如何确保使用 GPU?

如何使用Go SDK for Google Cloud Platform获取项目ID?

使用Google Cloud Platform时如何锁定状态文件?

从 Google Cloud Run 连接到外部(Google Cloud Platform 之外)PostgreSQL 数据库

如何使用 Django/Python 在 Google Cloud 上获取 Postgres 数据库列表

如何禁用Google Cloud Platform集成?

如何删除我的Google Cloud Platform帐户?

使用 Google Cloud Platform Buckets 的凭据无效

使用Java挂起Google Cloud Platform实例

如何通过 Cordova 上的移动应用程序的 API 从 Google Cloud Platform 获取数据?

如何在Google Cloud Platform中创建MariaDB数据库?

如何监控 Google Cloud Platform 上用户对 Firestore 数据库的访问

Google Cloud Storage Force下载

Google Cloud:以CSV格式下载