简体   繁体   English

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

[英]Validate all cloudformation files in a s3 folder

CloudFormation validate supports validating a cloudformation template in s3. CloudFormation 验证支持验证 s3 中的 cloudformation 模板。
How to validate all files in a s3 location.如何验证 s3 位置中的所有文件。 The files are located in a folder.这些文件位于一个文件夹中。

You can use the below python script to validate all cloudformation template in s3 bucket/folder您可以使用以下python脚本来验证 s3 存储桶/文件夹中的所有 cloudformation 模板
The below script generates Object Url / Public Url of all files in a s3 folder and then passes the url to validate_file function以下脚本生成Object Url / Public Url s3 文件夹中的所有文件,然后将 url 传递给validate_file function

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取s3文件夹中所有s3文件的Object URL - Get Object URL of s3 all s3 files in a folder cloudformation时如何在s3中动态创建文件夹 package - How to dynamically create a folder in s3 when doing cloudformation package 如何在脚本 elixir 中将文件夹的所有文件从另一个文件夹移动到同一个 S3 存储桶 - How to move all files of folder from another folder to same S3 bucket in script elixir 从 S3 存储桶中的文件夹中删除文件 - Delete files from folder in S3 bucket C# 列出 amazon S3 文件夹下所有带文件名的文件 - C# List all files with filename under an amazon S3 folder 如何通过 OPENROWSET (SQL Server) 列出 s3 存储桶文件夹中的所有镶木地板文件? - How to list all parquet files in s3 bucket folder via OPENROWSET (SQL Server)? S3 Bucket 的 aws cloudformation 片段应用 LifecycleConfiguration 删除所有现有版本 - aws cloudformation snippet for S3 Bucket to apply LifecycleConfiguration to delete all existing versions Cloudfront 的 Cloudformation S3 存储桶主体 - Cloudformation S3 bucket principal for Cloudfront 如何将文件从 S3 下载到本地文件夹 - How to download files from S3 to a local folder 如何使用 python 列出 S3 存储桶文件夹中的文件 - how to list files from a S3 bucket folder using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM