【问题标题】:I am trying to read a file in in Google Cloud Storage bucket with a python code but getting the error我正在尝试使用 python 代码读取 Google Cloud Storage 存储桶中的文件,但出现错误
【发布时间】:2019-09-18 04:22:36
【问题描述】:

我正在尝试读取存储在 Google Cloud Storage 存储桶 python 中的文件:

textfile = open("${gcs_bucket}mdm/OFF-B/test.txt", 'r') 
times = textfile.read().splitlines() 
textfile.close() 
print(getcwd()) 
print(times)

该文件存在于该位置,但我收到以下错误:

File "/var/cache/tomcat/temp/interpreter-9196592956267519250.tmp", line 3, in <module>
  textfile = open("gs://tp-bi-datalake-mft-landing-dev/mdm/OFF-B/test.txt", 'r')
IOError: [Errno 2] No such file or directory: 'gs://tp-bi-datalake-mft-landing-dev/mdm/OFF-B/test.txt'

【问题讨论】:

  • textfile = open("${gcs_bucket}mdm/OFF-B/test.txt", 'r') times = textfile.read().splitlines() textfile.close() print( getcwd()) print(times) 是我的代码
  • 不要将此放在评论中。把它放在你的问题中。

标签: python-3.x google-cloud-platform jython matillion


【解决方案1】:

那是因为您试图将其作为本地文件读取。

要从 Cloud Storage 读取,您需要导入库并使用客户端。

检查这个类似的Stackoverflow Question

在你的情况下,它会是这样的:

from google.cloud import storage

# Instantiates a client
client = storage.Client()

bucket_name = 'tp-bi-datalake-mft-landing-dev'

bucket = client.get_bucket(bucket_name)

blob = bucket.get_blob('mdm/OFF-B/test.txt')

downloaded_blob = blob.download_as_string()

print(downloaded_blob)

您还需要install the library,只需运行即可:

pip install google-cloud-storage 在运行代码之前。

您还可以在这里找到更多Google Cloud Storage Python Samples

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    • 2020-07-05
    • 1970-01-01
    • 2015-09-26
    • 1970-01-01
    • 2015-02-15
    相关资源
    最近更新 更多