【问题标题】:How to combine multiple files in GCS bucket with Cloud Function trigger如何将 GCS 存储桶中的多个文件与 Cloud Function 触发器合并
【发布时间】:2021-02-17 08:29:56
【问题描述】:

每个名称的每个日期都有 3 个文件,格式如下: 'nameXX_date',这是一个例子: '名称XX_01-01-20' '名称XY_01-01-20' 'nameXZ_01-01-20'

其中“名称”可以是任何内容,日期是文件上传的日期(几乎每天)。

我需要编写一个云函数,每当有新文件进入存储桶时触发,它将 3 个 XX、XY、XZ 文件合并到一个文件名 = "name_date" 的文件中。

这是我目前所得到的:


bucket_id = 'bucketname'
client = gcs.Client()
bucket = client.get_bucket(bucket_id)

name = 
date =
outfile = f'bucketname/{name}_{date}.CSV'

blobs = []
for shard in ('XX', 'XY', 'XZ'):
    sfile = f'{name}{shard}_{date}'
    blob = bucket.blob(sfile)
    if not blob.exists():
        # this causes a retry in 60s
        raise ValueError(f'branch {sfile} not present')
    blobs.append(blob)
bucket.blob(outfile).compose(blobs)
logging.info(f'Successfullt created {outfile}')
for blob in blobs:
    blob.delete()
logging.info('Deleted {} blobs'.format(len(blobs)))

我面临的问题是我不确定如何获取落入存储桶中的新文件的名称和日期,以便我可以找到其他 2 个匹配的文件并将它们组合起来

顺便说一句,我从这篇文章中得到了这段代码,我正在尝试在这里实现它:https://medium.com/google-cloud/how-to-write-to-a-single-shard-on-google-cloud-storage-efficiently-using-cloud-dataflow-and-cloud-3aeef1732325

【问题讨论】:

    标签: python google-cloud-functions google-cloud-storage


    【解决方案1】:

    据我了解,云功能是由特定 GCS 存储桶中的对象上的 google.storage.object.finalize 事件触发的。

    在这种情况下,您的云功能“签名”看起来像(取自您提到的“中等”文章):

    def compose_shards(data, context):
    

    data 是一本字典,其中包含有关 finalized 的对象(文件)的大量详细信息。在此处查看一些详细信息:Google Cloud Storage Triggers

    例如,data["name"] - 是正在讨论的对象的名称。

    如果您知道命名这些对象/分片所依据的模式/模板/规则,则可以从对象/分片名称中提取相关元素,并使用它来组成目标对象/文件名。

    【讨论】:

      猜你喜欢
      • 2021-06-13
      • 2020-04-11
      • 1970-01-01
      • 2020-08-21
      • 2017-01-27
      • 2021-06-30
      • 2021-02-04
      • 2020-09-17
      • 2020-06-28
      相关资源
      最近更新 更多