使用 S3 presigned-url 将文件上传到 Device Farm

Upload file using S3 presigned-url to device farm

尝试将 .apk 上传到我的设备农场项目但没有成功。我创建了这段代码来执行任务:

CLIENT = boto3.client("devicefarm", region_name="us-west-2")

def create_upload(project_arn, file_name, type):
    response = CLIENT.create_upload(
        projectArn=project_arn,
        name=file_name,
        type=type,
        contentType="application/octet-stream"
    )
    print(response)

    try:
        payload = {
            "file": open("app-release-04.apk", "rb")
        }
        header = {
            "contentType": "application/octet-stream"
        }
        app_arn = response["upload"]["arn"]
        signed_url = response["upload"]["url"]

        time.sleep(10)
        status = CLIENT.get_upload(arn=app_arn)
        print(status)

        api_response = requests.put(url=signed_url, files=payload, headers=header)
        print(api_response)
    except KeyError:
        raise KeyError(f"Upload failed! With project_arn: {project_arn},"
                    f"file_name: {file_name} and type: {type}: {response}")

    return app_arn

它有效,但是 api_response return <Response [200]>。但是,我在设备场的任何地方都看不到该文件。我的 python 代码哪里做错了?

如果我使用 cli + curl 就可以了:

aws devicefarm create-upload --project-arn <PROJECT_ARN> --name app-release-04.apk --type ANDROID_APP --region us-west-2

curl -T app-release-04.apk <SIGNED_URL>

原来问题首先是错误的 header 格式 "contentType" 而不是 "content-type"。然后我将有效负载更改为如下所示:

data = open(f"{file_path}/{file_name}", 'rb')
requests.put(url=signed_url, data=data, headers=header)

因此,在 put 请求中使用 data 而不是 files