【问题标题】:GCP Python Cloud Function : Reading a Plain text file from Cloud StorageGCP Python云函数:从云存储中读取纯文本文件
【发布时间】: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


【解决方案1】:

您从 Google 存储中读取的字符串是 multipart form 的字符串表示形式。它不仅包含上传的文件内容,还包含一些元数据。同一种请求可用于表示多个文件和/或表单字段以及一个文件。

要访问您想要的文件内容,您可以使用支持该功能的库,例如requests-toolbelt。以this SO answer 为例。您将需要包含边界的 Content-Type 标头,或者仅从内容中手动解析边界,如果您绝对必须的话。

编辑:从您的回答来看,似乎 Content-Type 标头在 Google Storage 的存储元数据中可用,这是一种常见情况。对于此答案的未来读者,从何处读取此标题的具体细节将取决于您的具体情况。

由于此库存在于 PyPI (the Python Package Index) 中,因此您甚至可以在 Cloud Functions by specifying it as a dependency in the requirements.txt file 中使用它。

【讨论】:

  • 我想通过云功能来实现它,所以我不确定这个工具带是否受支持或者我怎样才能让它在那里工作。有没有其他方法可以访问文件数据并在同一个函数中处理它?
  • 您可以在 Cloud Functions 中使用它,我在答案中添加了一个编辑,并附有解释过程的链接。
  • 我试过了,它不起作用。 from_response 方法需要一个响应对象来访问 response.content 但我们的只是一个字符串。错误说 str 对象没有属性内容。这是我正在寻找的链接,但它的 AWS Lambda。他们在某些部分对其进行硬编码,或者从不属于 gcp 函数的 aws lamba 上下文对象中获取内容:-------stackoverflow.com/questions/50925083/…
【解决方案2】:

下面的代码将打印文件中的实际文本。

from requests_toolbelt.multipart import decoder
from google.cloud import storage
storage_client = storage.Client()

def hello_gcs(event, context):
    file = event
    
    bucket = storage_client.bucket(file['bucket'])
    #print('Bucket Name :  {}'.format(file['bucket']))
    #print('Object Name :  {}'.format(file['name']))
    #print('Bucket Object :  {}'.format(bucket))
    
    blob = bucket.get_blob(file['name'])
    #print('Blob Object :  {}'.format(blob))
    
    contentType = blob.content_type
    print('Blob ContentType: {}'.format(contentType))

    #To download the file as byte object
    content = blob.download_as_string()
    print('content: {}'.format(content))

    for part in decoder.MultipartDecoder(content, contentType).parts:
         print(part.text)

【讨论】:

    猜你喜欢
    • 2018-08-18
    • 1970-01-01
    • 2019-04-20
    • 2022-10-20
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 2017-08-18
    相关资源
    最近更新 更多