簡體   English   中英

如何在 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

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)

通過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])

我讓腳本工作,但我通過避免使用 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 腳本,請告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM