简体   繁体   English

在本地使用 Python pandas 从 Google Cloud 存储中读取 CSV 文件

[英]reading CSV file form Google Cloud storage using Python pandas locally

I am trying to read a CSV file in a Google Cloud bucket locally using Pandas.我正在尝试使用 Pandas 在本地读取 Google Cloud 存储桶中的 CSV 文件。 I have logged in using gcloud auth login and have configured the project.我已经使用gcloud auth login并配置了项目。 However, when I try to read the CSV file using df = pd.read_csv(f"gs://mybucket/myfolder/mycsv.csv") I get a 401 error:但是,当我尝试使用df = pd.read_csv(f"gs://mybucket/myfolder/mycsv.csv")读取 CSV 文件时,我收到 401 错误:

Anonymous caller does not have storage.objects.get access to the Google Cloud Storage object., 401

I was wondering what further steps should I take so I can directly read the csv file?我想知道我应该采取哪些进一步的步骤才能直接读取 csv 文件? I have checked the gcloud config and my account is listed there.我检查了gcloud config ,并且我的帐户列在那里。

The problem is that the credentials established by gcloud auth login will not be picked up by your code.问题是您的代码不会获取gcloud auth login建立的凭据。 Please, see this great SO question and related answer for an in-deep explatation.请参阅这个伟大的 SO 问题和相关答案以获得深入的解释。

As suggested in the above-mentioned question, you can use gcloud auth application-default login instead.正如上述问题中所建议的,您可以改用gcloud auth application-default login

As suggested in the SDK documentation , you can set the value of the GOOGLE_APPLICATION_CREDENTIALS environment variable as well.正如SDK 文档中所建议的那样,您也可以设置GOOGLE_APPLICATION_CREDENTIALS环境变量的值。

Finally, you can initialize your storage client by providing explicit credentials: please, see the relevant documentation here ;最后,您可以通过提供显式凭据来初始化存储客户端:请参阅 此处的相关文档 this documentation provides in addition a great summary of all the mentioned authentication options.本文档还提供了所有提到的身份验证选项的摘要。

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

相关问题 使用Python在Google Cloud Storage上读写JSON文件 - Reading & Writing JSON file on Google Cloud Storage using Python 使用 Python 将 CSV 文件上传到 Google Cloud Storage - Upload CSV file to Google Cloud Storage using Python 如何将 pandas 数据添加到 Google Cloud Storage 中现有的 csv 文件中? - How to add pandas data to an existing csv file in Google Cloud Storage? 使用pandas在Python中读取csv文件的块 - Reading chunks of csv file in Python using pandas 使用熊猫读取csv文件python 3.6 - reading csv file python 3.6 using pandas 使用XLRD从Google Cloud Storage读取文件(python) - Reading a file from Google Cloud Storage with XLRD (python) Heroku/Dash 应用程序 Python,读取 Google Cloud Storage 上的文件 - Heroku/Dash app Python, reading file on Google Cloud Storage 如何通过 Pandas 从 Google Cloud Function 中的 Google Cloud Storage 访问 csv 文件? - How to access csv file from Google Cloud Storage in a Google Cloud Function via Pandas? Google Storage // Cloud Function // Python 修改Bucket中的CSV文件 - Google Storage // Cloud Function // Python Modify CSV file in the Bucket 如何使用Python打开和处理存储在Google云端存储中的CSV文件 - How to open and process CSV file stored in Google Cloud Storage using Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM