我需要同步不同帐户中的s3存储桶,为此我创建了具有存储桶策略的2个存储桶,并使用iam策略和s3事件触发器创建了Lambda函数。
我通过CLI进行了尝试。任何人都可以帮助编写bot脚本来同步存储桶
我正在尝试下面的lambda代码-
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def sync_command(command):
command_list = command.split(' ')
try:
logger.info("Running shell command: \"{}\"".format(command))
result = subprocess.run(command_list, stdout=subprocess.PIPE);
logger.info("Command output:\n---\n{}\n---".format(result.stdout.decode('UTF-8')))
except Exception as e:
logger.error("Exception: {}".format(e))
return False
return True
def lambda_handler(event, context):
logger.info(event);
SOURCE_BUCKET = os.environ['source']
print('SOURCE_BUCKET:', SOURCE_BUCKET)
TARGET_BUCKET = os.environ['target']
print('TARGET_BUCKET:', TARGET_BUCKET)
sync_command("aws s3 sync s3://source-bucket/ s3://destination-bucket/")
但这显示错误异常: [Errno 2]没有这样的文件或目录:'aws':'aws
答案 0 :(得分:0)
我已经使用bob脚本做到了,这是代码
import subprocess
import logging
import boto3
def check(bucket, key):
s3client = boto3.client("s3")
s3 = boto3.resource('s3')
copy_source = {
'Bucket': bucket,
'Key': key
}
print("key",key)
bucket = s3.Bucket('target_bucket')
bucket.copy(copy_source,key)
def lambda_handler(event, context):
ev = event["Records"]
bucketName = ev[0]["s3"]["bucket"]["name"]
keyName = ev[0]["s3"]["object"]["key"]
check(bucketName,keyName)
在lambda中工作正常。