gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 14:21:54 26 4
gpt4 key购买 nike

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

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

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 prefixes
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')

有什么好的方法可以做到这一点吗?

最佳答案

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

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

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

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

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

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

logging.info("hello")
s3_client = boto3.client('s3')
s3 = boto3.resource('s3')


def lambda_handler(event, context):

path = "/tmp"
clone = "git clone https://github.com/sirajpathan/test.git"

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

os.system(clone) # Cloning
#os.chdir(os.getcwd())
folder = os.getcwd() + "/tmp"
logger.info(os.listdir(os.getcwd()))
logger.info(os.listdir("/tmp"))

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

# Create zip from /tmp dir without any common prefixes
shutil.make_archive('test', 'zip', os.getcwd())
logger.info(os.listdir(os.getcwd()))
logger.info("Uploading zip to S3://%s/%s" % ('testsiraj1', s3_archive_file))
#used s3 meta client below
s3.meta.client.upload_file(os.getcwd() + "/" + s3_archive_file, 'testsiraj1', s3_archive_file)
##s3_client.upload_file(os.getcwd(), 'bucketname', s3_archive_file)
logger.info('Upload Complete')

关于python - AWS lambda 克隆 git 存储库并使 zip 无法在 s3 中上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50548998/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com