如何在 python lambda 函数中将文件从 S3 存储桶提交到代码提交存储库

How to commit files from a S3 bucket to codecommit repository in a python lambda function

我需要使用 python lambda 函数以编程方式将文件从 S3 中的存储桶提交并推送到 codecommit 存储库。

我正在使用 boto3 库,首先我从存储桶中获取并解压缩 zip 文件,最后我循环每个文件并制作一个 put_file.

问题是 put_file 生成与存储库中文件一样多的提交,但我只需要一次提交,因为我必须向 codebuild 发送一个通知。

我的 lambda 代码:


 file_key = event['Records'][0]['s3']['object']['key']
    
  
    
    obj = s3.get_object(Bucket=bucket_name, Key=file_key)
    body_dec = base64.b64decode(obj['Body'].read())
    memory_file = io.BytesIO(body_dec)
    
    with zipfile.ZipFile(memory_file, 'r') as zf:
        files = zf.namelist()
        for individualFile in files:
            data = zf.read(individualFile)
            
            #get parentCommitId for nuew push
            parentCommitId=""
            
            try:
                response = client.get_branch(
                    repositoryName='test-codecommit',
                    branchName='master'
                )
                parentCommitId= response['branch']['commitId']
            except botocore.exceptions.ClientError as error:
                print(error.response['Error'])
                
                
            try:  
             
            
                if not parentCommitId:
                    #parentCommitId= None
                    response = client.put_file(
                        repositoryName='test-codecommit',
                        branchName='master',
                        fileContent=data,
                        filePath=individualFile,
                        commitMessage='tag1',
                        name='Javier',
                        email='jramirezneira@gmail.com'
                    )
                else:
                    response = client.put_file(
                        repositoryName='test-codecommit',
                        branchName='master',
                        fileContent=data,
                        filePath=individualFile,
                        #fileMode='EXECUTABLE'|'NORMAL'|'SYMLINK',
                        parentCommitId=parentCommitId,
                        commitMessage='tag1',
                        name='Javier',
                        email='jramirezneira@gmail.com'
                    )
                result.append({'file': individualFile, 'Message': 'Added to Codecommit'})
            except botocore.exceptions.ClientError as error:
                print(error.response['Error'])
                result.append({'file': individualFile, 'Message': error.response['Error']['Message']})


我将感谢您的帮助或建议

您可以使用 create_commit 而不是使用 put_file,它在其 putFiles 参数中包含多个文件。我能够使用此代码做到这一点-

def create_codecommit_repo_commit(repo_name, branch_name, code_folder):
    client = boto3.client('codecommit')
    parent_folder = os.path.join(code_folder, repo_name)
    putFilesList = []
    for (root, folders, files) in os.walk(parent_folder):
        for file in files:
            file_path = os.path.join(root, file)
            with open(file_path, mode='r+b') as file_obj:
                file_content = file_obj.read()
            putFileEntry = {'filePath': str(file_path).replace(parent_folder, ''),
                            'fileContent': file_content}
            putFilesList.append(putFileEntry)

    response = client.create_commit(repositoryName=repo_name, branchName=branch_name, putFiles=putFilesList)
    return response