本文介绍了通过boto3同步两个存储桶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用boto3在两个不同的存储桶(源存储桶和目标存储桶)中循环存储桶内容,并且如果在源中找到与目标存储桶不匹配的任何密钥,则会将其上载到目标存储桶.请注意,我不想使用aws s3同步.我目前正在使用以下代码来完成这项工作:

Is there any way to use boto3 to loop the bucket contents in two different buckets (source and target) and if it finds any key in source that does not match with target, it uploads it to the target bucket. please note I do not want to use aws s3 sync. I am currently using the following code for doing this job:

import boto3

s3 = boto3.resource('s3')
src = s3.Bucket('sourcenabcap')
dst = s3.Bucket('destinationnabcap')
objs = list(dst.objects.all())
for k in src.objects.all():
 if (k.key !=objs[0].key):
  # copy the k.key to target

推荐答案

如果您只希望按Key进行比较(忽略对象之间的差异),则可以使用以下类似方法:

If you only wish to compare by Key (ignoring differences within objects), you could use something like:

s3 = boto3.resource('s3')
source_bucket = s3.Bucket('source')
destination_bucket = s3.Bucket('destination')
destination_keys = [object.key for object in destination_bucket.objects.all()]
for object in source_bucket.objects.all():
  if (object.key not in destination_keys):
    # copy object.key to destination

这篇关于通过boto3同步两个存储桶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 14:25