【问题标题】:copy last modified files from one bucket into an other bucket using gsutil使用 gsutil 将最后修改的文件从一个存储桶复制到另一个存储桶
【发布时间】:2019-04-29 05:14:01
【问题描述】:

我需要将上次修改的文件从一个 GCS 存储桶复制到另一个。 假设输入桶是:

gs://input-bucket/object 

目标桶是:

gs://target-bucket/object

我想复制今天的最后一个文件: 我写了

gsutil ls -l gs://renault-ftt-vll-dfp/complex-files/PAN/TRM    | sort -k2n | tail -n5   | sort -k2n | tail -n5 

但这并不完整。我的目标是将今天最后修改的文件从输入存储桶复制到目标存储桶。 请问有什么帮助吗? 非常感谢

【问题讨论】:

    标签: google-cloud-platform google-cloud-storage


    【解决方案1】:

    使用 gsutil 无法做到这一点,但我在 python 中为你做了这个漂亮的脚本:

    import subprocess
    import re
    import datetime
    
    child = subprocess.Popen('gsutil ls -l gs://<YOUR_BUCKET> | sort -k2n',shell=True,stdout=subprocess.PIPE)
    output = child.communicate()[0]
    
    datepattern = re.compile("\d{4}-\d{2}-\d{2}")
    matcher = datepattern.search(output)
    
    for line in output.splitlines():
        datepattern = re.compile("\d{4}-\d{2}-\d{2}")
        matcher = datepattern.search(line)
        if matcher:
            if matcher.group(0) == datetime.datetime.today().strftime('%Y-%m-%d'):
    
                filebucket = line[line.index("gs://") + len("gs://"):]
                child = subprocess.Popen("gsutil cp gs://"+filebucket+" gs://<YOUR_DESTINATION_BUCKET>",shell=True,stdout=subprocess.PIPE)
                outputCopy=child.communicate()[0]
                print outputCopy
    

    只需编辑“”和“”字段并正常运行,它应该会将今天已修改的所有文件复制到您的目标存储桶。

    【讨论】:

      【解决方案2】:

      目前在 gsutil 中无法轻松做到这一点,但使用终端是可行的。

      gsutil -m ls -l gs://input-bucket | grep $(date -I) | sed 's/.*\(gs:\/\/\)/\1/''| gsutil cp -I gs://target-bucket/

      分解:

      gsutil -m ls -l gs://input-bucket - 这将列出输入桶中的所有对象

      示例行:29 2018-11-27T15:43:24Z gs://input-bucket/README.md

      grep $(date -I) - 查找包含今天日期的所有行。 (查找今天修改的所有对象)

      sed 's/.*\(gs:\/\/\)/\1/'' - 这将删除直到gs:// 开始的所有内容,因此它将把行从29 2018-11-27T15:43:24Z gs://input-bucket/README.md 更改为gs://input-bucket/README.md

      gsutil cp -I gs://target-bucket/ - 将其复制到目标存储桶,-I 选项允许我们输入要从标准输入复制的文件列表。

      【讨论】:

        猜你喜欢
        • 2013-11-01
        • 2017-03-17
        • 2018-11-22
        • 2023-02-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多