我在尝试用python命令更新AWS Gamelift脚本时遇到了一个问题,该命令压缩目录并将其及其所有内容作为新版本上传到AWS Gameliff。
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
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/gamelift.html#GameLift.Client.update_script
我尝试发送一个base64编码版本的zip文件。然而,这并没有奏效。我将以下代码放在脚本的client_update部分之前,并使用b64EncodedZip作为ZipFile参数。
with open("RealtimeServer.zip", "rb") as f:
bytes = f.read()
b64EncodedZip = base64.b64encode(bytes)
我得到了boto3维护人员的帮助https://github.com/boto/boto3/issues/2646(谢谢@swetashre(
这是代码,它只能工作到5mb,如果你想上传一个大于5mb的zip文件,就需要使用s3 bucket。
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:])
with open('RealtimeServer.zip','rb') as f:
contents = f.read()
response = client.update_script(
ScriptId="SCRIPT_ID_GOES_HERE",
Version=sys.argv[1],
ZipFile=contents
)
if __name__ == "__main__":
main(sys.argv[1])
我让脚本正常工作,但我避免使用boto3。我不喜欢它,但它很管用。
os.system("aws gamelift update-script --script-id "SCRIPT_ID_GOES_HERE" --script-version " + sys.argv[1] + " --zip-file fileb://RealtimeServer.zip")
如果有人知道如何让boto3用于更新AWS Gamelift脚本,请告诉我。