Amazon CodeCommit に push されたファイルを S3 に自動保存したい

0

下記、サイトを参考し、Lambda による、S3 への自動保存を実施したいと思っております。 https://github.com/rioner/CodeCommitToS3/blob/master/lambda_function.py

私のCodeCommit には、数千のファイルが存在するのですが、上記のスクリプトを使用する と 300 ファイルまでしか、s3 に保存されませんでした。

どのようにしたら、リポジトリの全容を S3 へ保存出来るのでしょうか。

tk
gefragt vor 6 Monaten434 Aufrufe
4 Antworten
0

ファイル数が多い場合、get_differences()のレスポンスにNextTokenというものが含まれていると思います。
このNextTokenを使用してget_differences()をもう一度実行すると続きからファイルを取得できます。
なので、NextTokenがレスポンスから無くなるまでwhileでループするようにコードを書き換えればよいと思います。
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/codecommit/client/get_differences.html

profile picture
EXPERTE
beantwortet vor 6 Monaten
  • 以下のブログが参考になると思います。 https://www.packetswitch.co.uk/how-to-use-nexttoken-in-boto3-aws-api-calls/
    コードにすると以下のような感じだと思います。

    # coding:utf-8
    import boto3
    
    def lambda_handler(event, context):
        commit_id = event['Records'][0]['codecommit']['references'][0]['commit']
        rep = event['Records'][0]['eventSourceARN'].split(":")[5]
    
        s3 = boto3.client('s3')
        codecommit = boto3.client('codecommit')
    
        next_token = None
        all_files = []
    
        while True:
            if next_token:
                response = codecommit.get_differences(repositoryName=rep, afterCommitSpecifier=commit_id, NextToken=next_token)
                files = response['differences']
            else:
                response = codecommit.get_differences(repositoryName=rep, afterCommitSpecifier=commit_id)
                files = response['differences']
    
            all_files.extend(files)
    
            next_token = response['NextToken']
            if 'NextToken' not in response:
                break
    
        # ファイルごとの操作
        for file in all_files:
            path = rep + '/' + file['afterBlob']['path']
            blobid = file['afterBlob']['blobId']
            content = codecommit.get_blob(repositoryName=rep, blobId=blobid)['content']
            s3.put_object(Bucket='バケット名', Key=path, Body=content)
    
0

早速のご回答ありがとうございます。

下記も試してみたのですが、結果は変わらず、300 ファイルとなっていました。 https://stackoverflow.com/questions/43418653/how-can-i-push-aws-codecommit-to-s3-using-lambda

他に有用なサンプル等をご存知でしたら、教えて頂けないでしょうか。

tk
beantwortet vor 6 Monaten
0

コードのご提供、ありがとうございます。 早速試してみますので、後ほど結果を提示させて頂きます。

tk
beantwortet vor 6 Monaten
0

確認出来ました。全ファイル(8262)が対象となることを確認出来たのですが、s3 アップロードでタイムアウト(15分)と なってしまいました。 こちらで、zip 圧縮等を検討したいと思います。

大変、助かりました、ありがとうございます。

tk
beantwortet vor 6 Monaten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen