以编程方式仅将较新的文件上传到 Google Storage

问题描述

如何仅以编程方式(不使用 gsutil)将较新的文件(具有不同的 mtime 或大小)上传到 Google Storage 存储分区?

我正在寻找本质上 the same as gsutil rsync, 但以编程方式。

documentation 提供了一种上传各种文件的编程方式,如下所示:

# Python example
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(destination_blob_name)

blob.upload_from_filename(source_file_name)

但是,我找不到有关仅对较新文件执行此操作的任何信息。

我在 StackOverflow 上找到的所有答案都建议使用 gsutil在这种情况下我不能使用。 documentation on blob.upload_from_filename 似乎也没有提供答案 - 尽管坦率地说我不太明白它的各种参数的作用。

解决方法

如果您想模仿 gsutil,您可以将文件/对象 mtime 存储在对象的自定义元数据中。执行此操作后,您可以实现 gsutil 用于检测较新文件的算法 - 在 https://cloud.google.com/storage/docs/gsutil/commands/rsync#change-detection-algorithm 中进行了描述。