我使用“上传 .zip”在 AWS (Python) 中创建了一个 lambda 函数我丢失了这些文件,我需要进行一些更改,有没有办法下载那个 .zip?
是的!
导航到您的 lambda 函数设置,在右上角您将有一个名为“Actions
”的按钮。在下拉菜单中选择“export
”,然后在弹出窗口中单击“下载部署包”,该函数将下载到 .zip
文件中。
右上角的操作按钮
https://i.stack.imgur.com/z9S8P.png
来自上方 CTA 的弹出窗口(在此处点击“下载部署包”)
https://i.stack.imgur.com/60kbf.png
更新:添加了 sambhaji-sawant 的脚本链接。修正了错别字,改进了基于评论的答案和脚本!
您可以使用 aws-cli 下载任何 lambda 的 zip。
首先,您需要获取 lambda zip $ aws lambda get-function --function-name $functionName --query 'Code.Location'
的 URL
然后你需要使用 wget/curl 从 URL 下载 zip。 $ wget -O myfunction.zip URL_from_step_1
此外,您可以使用列出您的 AWS 账户上的所有功能
$ aws lambda list-functions
我制作了一个简单的 bash 脚本,从您的 AWS 账户并行下载所有 lambda 函数。你可以看到它here :)
注意:在使用上述命令(或任何 aws-cli 命令)之前,您需要使用 aws configure
设置 aws-cli
aws lambda get-function --function-name $functionName --query 'Code.Location'
返回用双引号括起来的 url,在使用 wget
之前需要将它们删除。要删除引号,您可以添加选项 --output text
。
如果您想下载给定区域中的所有功能,这是我的解决方法。我创建了一个简单的节点脚本来下载功能。在运行脚本之前,安装所有必需的 npm 包并将您的 AWS CLI 设置为您想要的区域。
let download = require('download-file');
let extract = require('extract-zip');
let cmd = require('node-cmd');
let downloadFile = async function (dir, filename, url) {
let options = {
directory: dir,
filename: filename
}
return new Promise((success, failure) => {
download(url, options, function (err) {
if (err) {
failure(err)
} else {
success('done');
}
})
})
}
let extractZip = async function (source, target) {
return new Promise((success, failure) => {
extract(source, { dir: target }, function (err) {
if (err) {
failure(err)
} else {
success('done');
}
})
})
}
let getAllFunctionList = async function () {
return new Promise((success, failure) => {
cmd.get(
'aws lambda list-functions',
function (err, data, stderr) {
if (err || stderr) {
failure(err || stderr)
} else {
success(data)
}
}
);
})
}
let getFunctionDescription = async function (name) {
return new Promise((success, failure) => {
cmd.get(
`aws lambda get-function --function-name ${name}`,
function (err, data, stderr) {
if (err || stderr) {
failure(err || stderr)
} else {
success(data)
}
}
);
})
}
let init = async function () {
try {
let { Functions: getAllFunctionListResult } = JSON.parse(await getAllFunctionList());
let getFunctionDescriptionResult, downloadFileResult, extractZipResult;
getAllFunctionListResult.map(async (f) => {
var { Code: { Location: getFunctionDescriptionResult } } = JSON.parse(await getFunctionDescription(f.FunctionName));
downloadFileResult = await downloadFile('./functions', `${f.FunctionName}.zip`, getFunctionDescriptionResult)
extractZipResult = await extractZip(`./functions/${f.FunctionName}.zip`, `/Users/malhar/Desktop/get-lambda-functions/final/${f.FunctionName}`)
console.log('done', f.FunctionName);
})
} catch (e) {
console.log('error', e);
}
}
init()
let { Functions: getAllFunctionListResult } = JSON.parse(await getAllFunctionList());
中的 Functions 做了什么
您可以找到一个 python 脚本来下载所有 lambda 函数 here。
import os
import sys
from urllib.request import urlopen
import zipfile
from io import BytesIO
import boto3
def get_lambda_functions_code_url():
client = boto3.client("lambda")
lambda_functions = [n["FunctionName"] for n in client.list_functions()["Functions"]]
functions_code_url = []
for fn_name in lambda_functions:
fn_code = client.get_function(FunctionName=fn_name)["Code"]
fn_code["FunctionName"] = fn_name
functions_code_url.append(fn_code)
return functions_code_url
def download_lambda_function_code(fn_name, fn_code_link, dir_path):
function_path = os.path.join(dir_path, fn_name)
if not os.path.exists(function_path):
os.mkdir(function_path)
with urlopen(fn_code_link) as lambda_extract:
with zipfile.ZipFile(BytesIO(lambda_extract.read())) as zfile:
zfile.extractall(function_path)
if __name__ == "__main__":
inp = sys.argv[1:]
print("Destination folder {}".format(inp))
if inp and os.path.exists(inp[0]):
dest = os.path.abspath(inp[0])
fc = get_lambda_functions_code_url()
print("There are {} lambda functions".format(len(fc)))
for i, f in enumerate(fc):
print("Downloading Lambda function {} {}".format(i+1, f["FunctionName"]))
download_lambda_function_code(f["FunctionName"], f["Location"], dest)
else:
print("Destination folder doesn't exist")
这是我使用的一个 bash 脚本,它下载了默认区域中的所有函数:
download_code () {
local OUTPUT=$1
OUTPUT=`sed -e 's/,$//' -e 's/^"//' -e 's/"$//g' <<<"$OUTPUT"`
url=$(aws lambda get-function --function-name get-marvel-movies-from-opensearch --query 'Code.Location' )
wget $url -O $OUTPUT.zip
}
FUNCTION_LIST=$(aws lambda list-functions --query Functions[*].FunctionName)
for run in $FUNCTION_LIST
do
download_code $run
done
echo "Finished!!!!"
grep
并下载具有通用模式的函数。FUNCTION_LIST=$(aws lambda list-functions --query Functions[*].FunctionName | grep <pattern>)
code
,您应该会找到location
。这是一个预签名的 URL,您可以使用它来下载该函数。该 URL 的有效期为 10 分钟。.zip
扩展名,因此在 Windows 中只是一个普通文件。解决方法是下载后手动给文件名添加扩展名。