【问题标题】:How do I update an AWS Gamelift script with boto3 in python?如何在 python 中使用 boto3 更新 AWS Gamelift 脚本?
【发布时间】:2020-12-13 13:05:59
【问题描述】:

我在尝试使用 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

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/gamelift.html#GameLift.Client.update_script

我尝试发送 zip 文件的 base64 编码版本。然而,这并没有奏效。我将以下代码放在脚本的 client_update 部分之前,并使用 b64EncodedZip 作为 ZipFile 参数。

with open("RealtimeServer.zip", "rb") as f:
        bytes = f.read()
        b64EncodedZip = base64.b64encode(bytes)

【问题讨论】:

    标签: python-3.x amazon-web-services boto3 unzip amazon-gamelift


    【解决方案1】:

    通过https://github.com/boto/boto3/issues/2646 的 boto3 维护者的一些帮助,我得以让它工作 (感谢@swetashre)

    这是代码,它最多只能工作 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])
    

    【讨论】:

      【解决方案2】:

      我得到了脚本,但我通过避免使用 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 脚本,请告诉我。

      【讨论】:

        猜你喜欢
        • 2018-11-22
        • 2022-11-24
        • 2019-11-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-28
        相关资源
        最近更新 更多