简体   繁体   English

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

[英]how can i download my data from google-cloud-platform using python?

I have my data on google cloud platform and i want to be able to be able to download it locally, this is my first time trying that and eventually i'll use the downloaded data with my python code.我在谷歌云平台上有我的数据,我希望能够在本地下载它,这是我第一次尝试这样做,最终我会将下载的数据与我的 python 代码一起使用。

I have checked the docs, like https://cloud.google.com/genomics/downloading-credentials-for-api-access and https://cloud.google.com/storage/docs/cloud-console i have successfully got the Json file for my first link, the second one is where u'm struggling, i'm using python 3.5 and assuming my json files name is data.json i have added the following code:我已经检查了文档,例如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]')

first of all i don't even know what i should call the value near environ so i just called it file, not sure how i'm supposed to fill it and i got access denied on the second line, obviously it's not how to download my file as there is no destination local repository or anything in that command any guidance will be appreciated.首先,我什至不知道我应该在环境附近调用什么值,所以我只是将它称为文件,不知道我应该如何填充它并且我在第二行拒绝访问,显然这不是如何下载我的文件,因为没有目标本地存储库或该命令中的任何内容,我们将不胜感激。

Edit:编辑:

   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')

I'm getting unresolved reference for storage and download_to_filename and should i replace service_account.json with credentials/client_secret.json .我得到了关于storagedownload_to_filename未解析参考,我应该用credentials/client_secret.json替换service_account.json Plus i tried to print the content of os.environ["GOOGLE_APPLICATION_CREDENTIALS"]['installed'] like i'd do with any Json but it just said i should give numbers meaning it read the input path as regular text only.另外,我尝试打印os.environ["GOOGLE_APPLICATION_CREDENTIALS"]['installed']就像我对任何 Json 所做的一样,但它只是说我应该给出数字,这意味着它仅将输入路径作为常规文本读取。

You should use the idiomatic Google Cloud library to run operations in GCS.您应该使用惯用的 Google Cloud 库在 GCS 中运行操作。

With the example there, and knowing that the client library will get the application default credentials, first we have to set the applicaiton default credentials with有了这个例子,并且知道客户端库将获得应用程序默认凭据,首先我们必须设置应用程序默认凭据

gcloud auth application-default login 

===EDIT=== ===编辑===

That was the old way.那是旧的方式。 Now you should use the instructions in this link .现在您应该使用此链接中的说明。

This means downloading a service account key file from the console , and setting the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of the downloaded JSON.这意味着从控制台下载服务帐户密钥文件,并将环境变量GOOGLE_APPLICATION_CREDENTIALS设置为下载的 JSON 的路径。

Also, make sure that this service account has the proper permissions on the project of the bucket.此外,请确保此服务帐户对存储桶的项目具有适当的权限

Or you can create the client with explicit credentials.或者您可以使用显式凭据创建客户端。 You'll need to download the key file all the same, but when creating the client, use:您同样需要下载密钥文件,但在创建客户端时,请使用:

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

========== ==========

And then, following the example code:然后,按照示例代码:

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')

Or, if this is a one-off download, just install the SDK and use gsutil to download:或者,如果这是一次性下载,只需安装SDK并使用gsutil下载:

gsutil cp gs://bucket/file .

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用python下载谷歌云平台上文件夹内的文件? - How can i download the files inside a folder on google cloud platform using python? 如何在 Google 云平台 gcp 上托管 python 脚本? - How can I host a python script on Google cloud platform gcp? 无法使用 python 从谷歌云存储下载对象 - can't download objects from google cloud storage using python Google Cloud Platform:我如何获得使用Python将对象放入Google Cloud Store的签名URL - Google Cloud Platform: How can I get a signed URL for putting an object to Google Cloud Store with Python 如何从 Google Cloud AI Jupyter Notebook(Python) 连接到 Google Cloud Platform Data Storage? - How to connect to Google Cloud Platform Data Storage from Google Cloud AI Jupyter Notebook(Python)? 如何从谷歌云控制台下载我的项目源代码 - How can I download my project source code from google cloud console 如何从 Google Cloud Platform 存储下载文件 - How to download a file from Google Cloud Platform storage 如何在 python 程序中定义我的 Google Cloud Platform 服务帐户的密钥? - How do I define my Google Cloud Platform service account's key in a python program? 我可以在Google Cloud Platform的Python应用程序中运行Webpack吗? - Can I run Webpack in a Python app on Google Cloud Platform? 如何在Google Cloud Platform上使用Python进行尴尬的并行任务以最大化吞吐量? - How can I maximize throughput for an embarrassingly-parallel task in Python on Google Cloud Platform?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM