python-3.x - 将文件上传到 AWS Device Farm

标签 python-3.x amazon-web-services aws-device-farm

我正在尝试使用 python + boto3 在设备场中创建上传(上传测试或应用程序)。 “create_upload”方法工作正常,因为它返回一个上传 arn 和上传到它的 url。

当我尝试使用请求将文件上传到此 URL 时,出现错误:

<Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message><AWSAccessKeyId>AKIAJV4C3CWPBUMBC3GA</AWSAccessKeyId><StringToSign>AWS4-HMAC-SHA256

我的代码:

response = client.create_upload(
  projectArn=df_project,
  name="test.zip",
  type="APPIUM_JAVA_TESTNG_TEST_PACKAGE",
  contentType='application/octet-stream'
)
test_url = response["upload"]["url"]
files = {'upload_file': open('/tmp/test.zip','rb')}
r = requests.post(test_url, files=files, data={})

我还尝试使用curl和requests.post将文件传递给数据 属性:

r = requests.put(test_url, data=open("/tmp/test.zip", "rb").read())
print(r.text)

cmd = "curl --request PUT --upload-file /tmp/test.zip \""+test_url+"\""
result = subprocess.call(cmd, shell=True)
print(result)

最佳答案

我之前在过去的项目中这样做过。这是我如何做到这一点的代码片段:

创建新上传

#http://boto3.readthedocs.io/en/latest/reference/services/devicefarm.html#DeviceFarm.Client.create_upload
print('Creating the upload presigned url')
response = client.create_upload(projectArn=args.projectARN,name=str(args.testPackageZip),type='APPIUM_WEB_JAVA_TESTNG_TEST_PACKAGE')
#create the s3 bucket to store the upload test suite
uploadArn = response['upload']['arn']
preSignedUrl = response['upload']['url']
print('uploadArn: ' + uploadArn + '\n')
print('pre-signed url: ' + preSignedUrl + '\n')

#print the status of the upload
#http://boto3.readthedocs.io/en/latest/reference/services/devicefarm.html#DeviceFarm.Client.get_upload
status = client.get_upload(arn=uploadArn)
print("S3 Upload Bucket Status: " + status['upload']['status'] + '\n')

print("Uploading file...")
#open the file and make payload
payload = {'file':open(args.testPackageZip,'rb')}
#try and perform the upload
r = requests.put(url = preSignedUrl,files = payload)
#print the response code back
print(r)

希望有帮助

关于python-3.x - 将文件上传到 AWS Device Farm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46339946/

相关文章:

python - 创建多项选择测验python

python - os.walk()应用于订单程序

amazon-web-services - 在 AWS RDS 中, "managed"和 "fully managed"服务有什么区别?

ios - 如何使用适用于 iOS 的 AWS 设备场将警报自动接受设置为真/假?

react-native - 在真实设备上运行 react-native detox e2e 测试

python - 如何在 Python3 中获取 PyObject 的字符串表示形式?

python - BeautifulSoup 只需提取表头

spring - 在 Spring Security SAML 扩展中检索整个 SAML 响应

php - 如何为 PHP 设置 AWS S3 凭证

amazon-web-services - 在 AWS Device Farm 上运行 appium 时出现错误 "Unable to create a new remote session"