如何将codecommit文件自动上传到s3?

时间:2018-05-31 11:16:19

标签: amazon-web-services amazon-s3

下面的代码只给出了更改但不是整个文件的字节。来到s3部分,zip文件正在创建,但大小为零字节,而实际大小将为70-80字节。我需要一种方法,当发生推送事件时,存储库中的所有文件都将被推送到s3。 至少告诉我如何在推送甚至发生时从存储库中获取所有文件。

提前致谢!

import boto3
import pathlib
import tarfile
import io
import sys
import os


codecommit = boto3.client("codecommit")

def get_differences(repository_name, branch="master"):
    response = codecommit.get_differences(
        repositoryName=repository_name,
        afterCommitSpecifier='master',
    )
    differences = []
    while "nextToken" in response:
        response = codecommit.get_differences(
            repositoryName=repository_name,
            afterCommitSpecifier='master',
            nextToken=response["nextToken"]
        )
        differences += response.get("differences", [])
    else:
        differences += response["differences"]
    return differences

def main(event,context):
    repository_name = event['Records'][0]['eventSourceARN'].split(':')[5]
    repository_path = pathlib.Path(repository_name)
    buf = io.BytesIO()
    with tarfile.open(None, "w:gz", fileobj=buf) as tar:
    for difference in get_differences(repository_name):
        blobid = difference["afterBlob"]["blobId"]
        path = difference["afterBlob"]["path"]
        mode = difference["afterBlob"]["mode"] # noqa
        blob = codecommit.get_blob(
            repositoryName=repository_name, blobId=blobid)
        tarinfo = tarfile.TarInfo(str(repository_path / path))
        tarinfo.size = len(blob["content"])
        tar.addfile(tarinfo, io.BytesIO(blob["content"]))
        tarobject = buf.getvalue()
# save to s3
s3 = boto3.client('s3')
s3.filename = str(repository_name)
s3.bucket = 'csharp-codes-codecommit' 
response = s3.put_object(Bucket=s3.bucket,Key= '/tmp/'+str(repository_path))

2 个答案:

答案 0 :(得分:1)

从您的代码中看,您的 let data = [ {"results": [ {result: "Passed"}, {result: "Failed"}, {} ] }, {"results": [ {result: "Passed"}, {result: "Failed"}, {result: "Passed"}, ] }, {"results": [ {result: "Passed"}, {result: "Failed"}, {} ] }, {"results": [ {result: "Passed"}, {result: "Failed"}, {} ] }, ]; let count = {"Passed": 0, "Failed": 0}; for (let i = 0; i < data.length; i++){ if (!data[i].results) continue; for (let j = 0; j < data[i].results.length; j++){ let el = data[i].results[j].result; if (el) count[el]++; } } console.log(count);调用似乎附加了您的tar文件。

另一个解决方案是位于here的CodeCommit的无服务器示例。每次推送后last one都会创建一个zip存档。

答案 1 :(得分:0)

请阅读boto3的S3客户端op的文档。

您尚未提供Body(因此您的S3对象将使用0字节创建)并且您错误地提供了Key(密钥应包括路径和文件名,而不仅仅是路径正如你所写的那样。)