【问题标题】:Issue in Reading a file from gcs bucket through python script通过 python 脚本从 gcs 存储桶中读取文件的问题
【发布时间】:2017-08-18 19:50:06
【问题描述】:

我正在尝试打开 gcs 存储桶中的 csv 文件,以读取内容并在 BigQuery 中相应地处理数据。我能够列出存储桶中存在的 csv,但无法打开它。我的代码如下:

with cloudstorage.open(bucket_name/gs_file,"r+") as csvFile:
     reader = csv.reader(iter(csvFile.readline,''), delimiter = '|')
     csvfilearray = next(reader)
     print (csvfilearray)

出现以下错误:

NameError:名称“cloudstorage”未定义

导入云存储如下:

import cloudstorage as gcs

并再次尝试gcs.open,但仍然错误:

File "./GCS_Connection_Test.py", line 10, in <module>
    import cloudstorage as gcs
ImportError: No module named cloudstorage

谁能帮忙打开 GCS 上的文件进行处理。

【问题讨论】:

  • 你安装了google-cloud-storage 库吗?
  • 或者,如果你在 GAE 上,appengine-gcs-client 库会根据 Reading and Writing to Google Cloud Storage 匹配你的导入
  • 从 google vm 实例执行 python 代码,我能够获取特定存储桶下的对象列表。所以,似乎 google-cloud-storage lib 安装在那里。无法打开文件加工。我在“来自 google.cloud 导入存储”storage_client = storage.Client() 的代码中写了下面的导入
  • 尝试使用“pip install GoogleAppEngineCloudStorageClient”安装。仍然出现如下错误:import cloudstorage as gcs File“/usr/local/lib/python2.7/dist-packages/cloudstorage/__init__.py ",第 20 行,在 中从 .api_utils 导入 RetryParams 文件 "/usr/local/lib/python2.7/dist-packages/cloudstorage/api_utils.py",第 45 行,在 中来自 google.appengine。 api import app_identity ImportError: No module named appengine.api 请帮忙,需要做些什么来解决它
  • 这是否是 GAE 应用程序?如果是,您需要安装在您的应用程序中(而不是系统上)的第二个库,否则您需要安装在系统上的第一个库。不是相反。

标签: python google-app-engine google-cloud-storage


【解决方案1】:

根据您的 app.yaml 文件,您处于灵活环境中。

您使用了GoogleAppEngineCloudStorageClient,这是一个标准环境库(就像appengine-gcs-client),您需要使用google-cloud-storage 库,请参阅Using Cloud Storage 指南中的Edit project configuration and install dependencies 部分,这是您的需要关注。

您也按照标准环境说明安装库,需要使用弹性环境说明,见Using Python Libraries

一般说明:在文档页面 URL 中查找 docs/flexibledocs/standard 字符串以区分它们(或显示在文档左侧导航栏顶部附近的环境类型,您可能需要向上滚动看到它)

【讨论】:

  • 首先,非常感谢您回复我的问题。我是谷歌云的新手,有点迷失在文档中,现在也厌倦了寻找解决这个问题的方法。我按照你建议的链接进行操作。也创建了虚拟环境,但仍处于相同位置。您能否建议我使用的 cloudstorage 导入和 Cloudstorage.open() 函数在灵活的环境中是否有效(将工作)?有没有其他方法可以从 GCS 存储桶打开 csv 并根据要求处理文件。这种方法对我来说看起来很简单,但没有得到任何关于导入问题的建议。
  • 不,您现在使用的库将无法使用。休息一下,然后用正确的文档重新开始。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-05
  • 1970-01-01
  • 1970-01-01
  • 2022-01-19
  • 2023-03-06
  • 1970-01-01
  • 2020-04-06
相关资源
最近更新 更多