AWS lambda克隆git存储库并使zip无法在s3中上传

时间:2018-05-27 04:22:45

标签: python git amazon-web-services amazon-s3 aws-lambda

我想尝试克隆我在私有github中的git存储库。我可以看到添加webhooks并按照here所述设置云形成。

它正在向我的lambda发送请求。我能够克服存储库的方式,但无法创建存储库的zip并将其上传到s3,就像在Lambda中一样,我无法找到存储存储库的位置,以便将其压缩并从该路径上传。

import boto3
from botocore.vendored import requests
import logging
import base64
import os
import shutil
from zipfile import ZipFile
from cStringIO import StringIO

# Set to False to allow self-signed/invalid ssl certificates
verify = False

logger = logging.getLogger()
logger.setLevel(logging.INFO)


s3_client = boto3.client('s3')


def lambda_handler(event, context):

   path        =   "/gitpull" 
   clone       =   "git clone https://username:pwd@site.com/scm/awsdemos/testrepo.git" 

# os.system("sshpass -p your_password ssh user_name@your_localhost")
  os.chdir(path) 

  os.system(clone) # Cloning
#   folder = "/gitpull"
#   logger.info(os.listdir(folder))

#   shutil.make_archive('Gitpull', 'zip', '/tmp')
   s3_archive_file = "Gitpull.zip" 

    # Create zip from /tmp dir without any common preffixes
   shutil.make_archive('Gitpull', 'zip', os.getcwd())
   logger.info("Uploading zip to S3://%s/%s" % ('gitpulls3', s3_archive_file))
   s3_client.upload_file(os.getcwd(), 'gitpulls3', s3_archive_file)
   logger.info('Upload Complete')

有什么好方法可以做到吗?

1 个答案:

答案 0 :(得分:0)

将s3_client.upload_file更改为以下代码:

AppRegistry.registerHeadlessTask('TimeTracking', () =>   TimeTracking);

检查我在Lambda函数上尝试的以下代码:

s3 = boto3.resource('s3')
s3.meta.client.upload_file(os.getcwd() + "/" + s3_archive_file_name, 'mybucket', s3_archive_file_name)