import json
import boto3
import uuid
import time
import os
import mimetypes

# basically returns a list of a all files in the branch
def get_blob_list(codecommit, repository, branch):
    response = codecommit.get_differences(
        repositoryName=repository,
        afterCommitSpecifier=branch,
    )
    blob_list = [difference['afterBlob'] for difference in response['differences']]
    while 'nextToken' in response:
        response = codecommit.get_differences(
            repositoryName=repository,
            afterCommitSpecifier=branch,
            nextToken=response['nextToken']
        )
        blob_list += [difference['afterBlob'] for difference in response['differences']]
    return blob_list

def codecommit_to_s3():
    # target bucket
    bucket = boto3.resource('s3').Bucket('code-bucket')
    # source codecommit
    codecommit = boto3.client('codecommit', region_name='us-east-1')
    repository_name = 'repo-Name'
    # reads each file in the branch and uploads it to the s3 bucket
    for blob in get_blob_list(codecommit, repository_name, 'master'):
        path = blob['path']
        content = (codecommit.get_blob(repositoryName=repository_name, blobId=blob['blobId']))['content']
        # we have to guess the mime content-type of the files and provide it to S3 since S3 cannot do this on its own.
        content_type = mimetypes.guess_type(path)[0]
        print(blob)
        if content_type is not None:
            bucket.put_object(Body=(content), Key=path, ContentType=content_type)
        else:
            bucket.put_object(Body=(content), Key=path)


最后修改:2021 年 12 月 20 日 03 : 17 PM
如果觉得我的文章对你有用,请随意赞赏