如何在 python 中使用 boto3 更新 AWS Gamelift 脚本?
How do I update an AWS Gamelift script with boto3 in python?
我 运行 在尝试使用 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
我尝试发送 base64 编码版本的 zip 文件。但是,那没有用。我将以下代码放在脚本的 client_update 部分之前,并使用 b64EncodedZip 作为 ZipFile 参数。
with open("RealtimeServer.zip", "rb") as f:
bytes = f.read()
b64EncodedZip = base64.b64encode(bytes)
我让脚本正常工作,但我通过避免使用 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 脚本,请告诉我。
在 https://github.com/boto/boto3/issues/2646 的 boto3 维护者的帮助下,我能够让它工作
(感谢@swetashre)
这是代码,它最多只能工作 5mb,如果您想上传任何大于 5mb 的 zip 文件,则需要使用 s3 存储桶。
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])
我 运行 在尝试使用 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
我尝试发送 base64 编码版本的 zip 文件。但是,那没有用。我将以下代码放在脚本的 client_update 部分之前,并使用 b64EncodedZip 作为 ZipFile 参数。
with open("RealtimeServer.zip", "rb") as f:
bytes = f.read()
b64EncodedZip = base64.b64encode(bytes)
我让脚本正常工作,但我通过避免使用 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 脚本,请告诉我。
在 https://github.com/boto/boto3/issues/2646 的 boto3 维护者的帮助下,我能够让它工作 (感谢@swetashre)
这是代码,它最多只能工作 5mb,如果您想上传任何大于 5mb 的 zip 文件,则需要使用 s3 存储桶。
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])