import boto3
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 lambda_handler(event, context):
# target bucket
bucket = boto3.resource('s3').Bucket('gm-rawdata')
# source codecommit
codecommit = boto3.client('codecommit', region_name='us-east-1')
repository_name = 'repo-GmUcceMl-337058716437'
# 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)
# Test lambda_handler(1,1)
最后修改:2021 年 12 月 02 日 05 : 50 PM
© 允许规范转载