gpt4 book ai didi

python - 验证 s3 文件夹中的所有 cloudformation 文件

转载 作者:行者123 更新时间:2023-12-03 07:39:53 25 4
gpt4 key购买 nike

CloudFormation validate 支持验证 s3 中的 cloudformation 模板。
如何验证 s3 位置中的所有文件。这些文件位于一个文件夹中。

最佳答案

您可以使用下面的python脚本来验证s3存储桶/文件夹中的所有cloudformation模板
以下脚本生成 s3 文件夹中所有文件的 Object Url/Public Url,然后将该 url 传递给 validate_file 函数

import boto3

s3_uri="s3://BUCKET_NAME/FOLDER_1/FOLDER2/" # S3 URI of the folder you want to recursively scan, Replace this with your own S3 URI

# Split the s3 uri to extract bucket name and the file prefix
# Splitting S# 3 URI will generate an array
# Combine the appropirate elements of the array to extraxt BUCKET_NAME and PREFIX
arr=s3_uri.split('/')
bucket =arr[2]
prefix=""
for i in range(3,len(arr)-1):
prefix=prefix+arr[i]+"/"

s3_client = boto3.client("s3")

def validate_file(object_url): # function to validate cloudformation template
cloudformation_client = boto3.client('cloudformation')

response = cloudformation_client.validate_template(
TemplateURL=object_url
)
print(response) # print the response


def get_all_s3_files(bucket,prefix,s3_client): # generate object url of all files in the folder and pass it to validate function

response = s3_client.list_objects_v2(Bucket=bucket, Prefix=prefix) # Featch Meta-data of all the files in the folder
files = response.get("Contents")
for file in files: # Iterate through each files
file_path=file['Key']
object_url="https://"+bucket+".s3.amazonaws.com/"+file_path #create Object URL Manually
print("Object Url = "+object_url)
if object_url.endswith(".yml"):
validate_file(object_url=object_url) # validate all files

get_all_s3_files(bucket=bucket,prefix=prefix,s3_client=s3_client)

关于python - 验证 s3 文件夹中的所有 cloudformation 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71827336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com