【发布时间】:2020-10-14 15:03:08
【问题描述】:
一旦文件上传到存储中,就会触发云功能, 我的文件名:PubSubMessage。 内文:嗨,这是第一条消息
from google.cloud import storage
storage_client = storage.Client()
def hello_gcs(event, context):
file = event
bucket = storage_client.get_bucket(file['bucket'])
blob = bucket.blob(file['name'])
contents = blob.download_as_string()
print('contents: {}'.format(contents))
decodedstring = contents.decode(encoding="utf-8", errors="ignore")
print('decodedstring: \n{}'.format(decodedstring))
print('decodedstring: \n{}'.format(decodedstring))
------WebKitFormBoundaryAWAKqDaYZB3fJBhx
Content-Disposition: form-data; name="file"; filename="PubSubMessage.txt"
Content-Type: text/plain
Hi, this this the first line.
Hi ,this is the second line.
hi this is the space after.
------WebKitFormBoundaryAWAKqDaYZB3fJBhx--
我的Requirements.txt 文件
google-cloud-storage
requests==2.20.0
requests-toolbelt==0.9.1
如何获取文件“嗨,我是第一条消息.....”中的实际字符串?
从文件中获取文本的最佳方法是什么? TIA
【问题讨论】:
-
我看到您已经编辑了您的帖子,以便在您阅读文件中的字符串后包含您想要做的更多事情,但我认为split them into one or more separate questions 会更好。
-
@RafaelAlmeida 我没有尝试这 2 件事,因为我一直在获取文本部分。
-
@RafaelAlmeida 我尝试了该代码,但失败了。我已经用代码更新了我的问题。请帮忙。不确定它不起作用
-
快速查看,它出现了缩进问题,在最后两行的
print之前缺少三个空格。如果这不能解决问题,请附上您收到的错误消息。 -
@RafaelAlmeida 如果是缩进问题,云函数会引发编译错误。更大的问题是日志中没有正确的错误。它只是说没有其他信息就崩溃了,但我确信这不是缩进问题
标签: python google-cloud-platform google-cloud-functions google-cloud-storage