我在尝试使用 python 命令更新 AWS Gamelift 脚本时遇到问题,该命令压缩目录并将其所有内容作为较新版本上传到 AWS Gamelift。
from zipfile import ZipFile
import os
from os.path import basename
import boto3
import sys, getopt
def main(argv):
versInput = sys.argv[1]
#initializes client for updating script in aws gamelift
client = boto3.client('gamelift')
#Where is the directory relative to the script directory. In this case, one folder dir lower and the contents of the RealtimeServer dir
dirName = '../RealtimeServer'
# create a ZipFile object
with ZipFile('RealtimeServer.zip', 'w') as zipObj:
# Iterate over all the files in directory
for folderName, subfolders, filenames in os.walk(dirName):
rootlen = len(dirName) + 1
for filename in filenames:
#create complete filepath of file in directory
filePath = os.path.join(folderName, filename)
# Add file to zip
zipObj.write(filePath, filePath[rootlen:])
response = client.update_script(
ScriptId=SCRIPT_ID_GOES_HERE,
Version=sys.argv[1],
ZipFile=b'--zip-file \"fileb://RealtimeServer.zip\"'
)
if __name__ == "__main__":
main(sys.argv[1])
我计划在每次进行更改时给它一个新的版本号来使用它:
python updateScript.py "0.1.1"
这是为了帮助加快开发速度。但是,我对 client.update_script() 的 ZipFile 参数做错了
对于上下文,我可以直接从命令行使用 AWS CLI,并通过以下方式毫无问题地更新脚本:
aws gamelift update-script --script-id SCRIPT_STRING_ID_HERE --script-version "0.4.5" --zip-file fileb://RealtimeServer.zip
但是,我不确定发生了什么,因为当我尝试它时它无法解压缩文件:
botocore.errorfactory.InvalidRequestException: An error occurred (InvalidRequestException) when calling the UpdateScript operation: Failed to unzip the zipped file.
更新:
阅读有关 ZipFile 参数的更多文档后:
https://docs.aws.amazon.com/gamelift/latest/apireference/API_UpdateScript.html
我尝试发送 zip 文件的 base64 编码版本。然而,这并没有奏效。我将以下代码放在脚本的 client_update 部分之前,并使用 b64EncodedZip 作为 ZipFile 参数。
with open("RealtimeServer.zip", "rb") as f:
bytes = f.read()
b64EncodedZip = base64.b64encode(bytes)