<legend id='9rTbL'><style id='9rTbL'><dir id='9rTbL'><q id='9rTbL'></q></dir></style></legend>

      <i id='9rTbL'><tr id='9rTbL'><dt id='9rTbL'><q id='9rTbL'><span id='9rTbL'><b id='9rTbL'><form id='9rTbL'><ins id='9rTbL'></ins><ul id='9rTbL'></ul><sub id='9rTbL'></sub></form><legend id='9rTbL'></legend><bdo id='9rTbL'><pre id='9rTbL'><center id='9rTbL'></center></pre></bdo></b><th id='9rTbL'></th></span></q></dt></tr></i><div id='9rTbL'><tfoot id='9rTbL'></tfoot><dl id='9rTbL'><fieldset id='9rTbL'></fieldset></dl></div>
        <bdo id='9rTbL'></bdo><ul id='9rTbL'></ul>
        <tfoot id='9rTbL'></tfoot>

        <small id='9rTbL'></small><noframes id='9rTbL'>

      1. 通过云功能在 Google Cloud Storage 中创建新的 csv 文件

        Create new csv file in Google Cloud Storage from cloud function(通过云功能在 Google Cloud Storage 中创建新的 csv 文件)

        <small id='v0Ql2'></small><noframes id='v0Ql2'>

          • <bdo id='v0Ql2'></bdo><ul id='v0Ql2'></ul>
            <i id='v0Ql2'><tr id='v0Ql2'><dt id='v0Ql2'><q id='v0Ql2'><span id='v0Ql2'><b id='v0Ql2'><form id='v0Ql2'><ins id='v0Ql2'></ins><ul id='v0Ql2'></ul><sub id='v0Ql2'></sub></form><legend id='v0Ql2'></legend><bdo id='v0Ql2'><pre id='v0Ql2'><center id='v0Ql2'></center></pre></bdo></b><th id='v0Ql2'></th></span></q></dt></tr></i><div id='v0Ql2'><tfoot id='v0Ql2'></tfoot><dl id='v0Ql2'><fieldset id='v0Ql2'></fieldset></dl></div>

              <tfoot id='v0Ql2'></tfoot>
                  <tbody id='v0Ql2'></tbody>
              • <legend id='v0Ql2'><style id='v0Ql2'><dir id='v0Ql2'><q id='v0Ql2'></q></dir></style></legend>

                  本文介绍了通过云功能在 Google Cloud Storage 中创建新的 csv 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  第一次使用 Google Cloud Storage.下面我有一个云功能,只要将 csv 文件上传到我的存储桶内的 my-folder 就会触发该功能.我的目标是在同一文件夹中创建一个新的 csv 文件,读取上传的 csv 的内容并将每一行转换为将进入新创建的 csv 的 URL.问题是我一开始就创建新的 csv 时遇到了麻烦,更不用说实际写入它了.

                  First time working with Google Cloud Storage. Below I have a cloud function which is triggered whenever a csv file gets uploaded to my-folder inside my bucket. My goal is to create a new csv file in the same folder, read the contents of the uploaded csv and convert each line to a URL that will go into the newly created csv. Problem is I'm having trouble just creating the new csv in the first place, let alone actually writing to it.

                  我的代码:

                  import os.path
                  import csv
                  import sys
                  import json
                  from csv import reader, DictReader, DictWriter
                  from google.cloud import storage
                  from io import StringIO
                  
                  def generate_urls(data, context):
                      if context.event_type == 'google.storage.object.finalize':
                          storage_client = storage.Client()
                          bucket_name = data['bucket']
                          bucket = storage_client.get_bucket(bucket_name)
                          folder_name = 'my-folder'
                          file_name = data['name']
                  
                          if not file_name.endswith('.csv'):
                              return
                  

                  接下来的几行来自 GCP 的 GitHub 存储库中的示例.这是我希望创建新 csv 的时候,但没有任何反应.

                  These next few lines came from an example in GCP's GitHub repo. This is when I would expect the new csv to be created, but nothing happens.

                          # Prepend 'URL_' to the uploaded file name for the name of the new csv
                          destination = bucket.blob(bucket_name + '/' + file_name[:14] + 'URL_' + file_name[14:])
                          destination.content_type = 'text/csv'
                          sources = [bucket.get_blob(file_name)]
                          destination.compose(sources)
                          output = bucket_name + '/' + file_name[:14] + 'URL_' + file_name[14:]
                  
                  
                          # Transform uploaded csv to string - this was recommended on a similar SO post, not sure if this works or is the right approach...
                          blob = bucket.blob(file_name)
                          blob = blob.download_as_string()
                          blob = blob.decode('utf-8')
                          blob = StringIO(blob)
                  
                          input_csv = csv.reader(blob)
                  

                  在下一行出现错误:No such file or directory: 'myProjectId/my-folder/URL_my_file.csv'

                          with open(output, 'w') as output_csv:
                              csv_dict_reader = csv.DictReader(input_csv, )
                              csv_writer = csv.DictWriter(output_csv, fieldnames=['URL'], delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
                              csv_writer.writeheader()
                              line_count = 0
                              for row in csv_dict_reader:
                                  line_count += 1
                                  url = ''
                                  ...
                                  # code that converts each line
                                  ...
                                  csv_writer.writerow({'URL': url})
                              print(f'Total rows: {line_count}')
                  

                  如果有人对我如何获得它来创建新的 csv 然后写入它有任何建议,那将是一个巨大的帮助.谢谢!

                  If anyone has any suggestions on how I could get this to create the new csv and then write to it, it would be a huge help. Thank you!

                  推荐答案

                  可能我会说我对代码和解决方案的设计有几个问题:

                  Probably I would say that I have a few questions about the code and the design of the solution:

                  1. 据我了解 - 一方面,云功能由 finalise 事件触发 Google Cloud Storage 触发器,而不是他希望将新创建的文件保存到同一个存储桶中.成功后,该存储桶中新对象的出现将触发您的云函数的另一个实例.这是预期的行为吗?你的云功能准备好了吗?

                  1. As I understand - on one hand the cloud function is triggered by a finalise event Google Cloud Storage Triggers, not he other hand you would like to save a newly created file into the same bucket. Upon success, an appearance of a new object in that bucket is to trigger another instance of your cloud function. Is that the intended behaviour? You cloud function is ready for that?

                  在本体上没有文件夹这样的东西.因此在这段代码中:

                  Ontologically there is no such thing as folder. Thus in this code:

                          folder_name = 'my-folder'
                          file_name = data['name']
                  

                  第一行有点多余,除非您想将该变量和值用于其他用途...并且 file_name 获取包含所有前缀的对象名称(您可以将它们视为文件夹".

                  the first line is a bit redundant, unless you would like to use that variable and value for something else... and the file_name gets the object name including all prefixes (you may consider them as "folders".

                  1. 您引用的示例 - storage_compose_file.py - 是关于如何将 GCS 中的几个对象组合成一个.我不确定该示例是否与您的案例相关,除非您有一些额外的要求.

                  1. The example you refer - storage_compose_file.py - is about how a few objects in the GCS can be composed into one. I am not sure if that example is relevant for your case, unless you have some additional requirements.

                  现在,让我们看一下这段代码:

                  Now, let's have a look at this snippet:

                          destination = bucket.blob(bucket_name + '/' + file_name[:14] + 'URL_' + file_name[14:])
                          destination.content_type = 'text/csv'
                          sources = [bucket.get_blob(file_name)]
                          destination.compose(sources)
                  

                  一个.bucket.blob - 是工厂构造函数 - 请参阅 API 存储桶说明.我不确定您是否真的想使用 bucket_name 作为其参数的元素...

                  a. bucket.blob - is a factory constructor - see API buckets description. I am not sure if you really would like to use a bucket_name as an element of its argument...

                  b.sources - 变成一个只有一个元素的列表 - 对 GCS 存储桶中现有对象的引用.

                  b. sources - becomes a list with only one element - a reference to the existing object in the GCS bucket.

                  c.destination.compose(sources) - 是否试图复制现有对象?如果成功 - 它可能会触发您的云函数的另一个实例.

                  c. destination.compose(sources) - is it an attempt to make a copy of the existing object? If successful - it may trigger another instance of your cloud function.

                  1. 关于类型更改

                          blob = bucket.blob(file_name)
                          blob = blob.download_as_string()
                  

                  在第一行之后,blob 变量的类型为 google.cloud.storage.blob.Blob.在第二个 - bytes 之后.我认为 Python 允许这样的事情......但你真的喜欢它吗?顺便说一句,download_as_string 方法已弃用 - 请参阅 Blobs/Objects API

                  After the first line the blob variable has the type google.cloud.storage.blob.Blob. After the second - bytes. I think Python allows such things... but would you really like it? BTW, the download_as_string method is deprecated - see Blobs / Objects API

                  1. 关于输出:

                     output = bucket_name + '/' + file_name[:14] + 'URL_' + file_name[14:]
                      
                     with open(output, 'w') as output_csv:
                  

                  请记住 - 所有这些都发生在云函数的内存中.与 GCS 的 blob 桶无关.如果您想在云函数中使用临时文件 - 您将在 /tmp 目录中使用它们 - 从谷歌云函数写入临时文件我猜你会因为这个问题而收到错误.

                  Bear in mind - all of that happens inside the memory of the cloud function. Nothing to do with the GCS buckets of blobs. If you would like to use temporary files within cloud functions - you are to use them in the /tmp directory - Write temporary files from Google Cloud Function I would guess that you get the error because of this issue.

                  =>提出一些建议.

                  => Coming to some suggestions.

                  您可能希望将对象下载到云函数内存中(进入 /tmp 目录).然后您想处理源文件并将结果保存在源附近.然后您想将结果上传到 另一个(不是源)存储桶.如果我的假设是正确的,我会建议逐步实施这些事情,并检查您是否在每一步都获得了预期的结果.

                  You probably would like to download the object into the cloud function memory (into the /tmp directory). Then you would like to process the source file and save the result near the source. Then you would like to upload the result to another (not the source) bucket. If my assumptions are correct, I would suggest to implement those things step by step, and check that you get the desired result on each step.

                  这篇关于通过云功能在 Google Cloud Storage 中创建新的 csv 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                  Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                  Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                  Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                  Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                  Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)
                  • <tfoot id='hTMtI'></tfoot>
                      • <bdo id='hTMtI'></bdo><ul id='hTMtI'></ul>

                          <i id='hTMtI'><tr id='hTMtI'><dt id='hTMtI'><q id='hTMtI'><span id='hTMtI'><b id='hTMtI'><form id='hTMtI'><ins id='hTMtI'></ins><ul id='hTMtI'></ul><sub id='hTMtI'></sub></form><legend id='hTMtI'></legend><bdo id='hTMtI'><pre id='hTMtI'><center id='hTMtI'></center></pre></bdo></b><th id='hTMtI'></th></span></q></dt></tr></i><div id='hTMtI'><tfoot id='hTMtI'></tfoot><dl id='hTMtI'><fieldset id='hTMtI'></fieldset></dl></div>

                          <small id='hTMtI'></small><noframes id='hTMtI'>

                          <legend id='hTMtI'><style id='hTMtI'><dir id='hTMtI'><q id='hTMtI'></q></dir></style></legend>
                            <tbody id='hTMtI'></tbody>