我正在尝试使用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文件传递给数据 attribut:
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)
答案 0 :(得分:3)
我之前在一个项目中做过这个。这是我如何做到的代码片段:
#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)
希望有所帮助
答案 1 :(得分:1)
我在AWS Device Farm工作。当某些请求参数与用于对URL签名的参数不匹配时,会出现此问题。我看过这个问题的时代,它与内容类型有关。我建议不要在CreateUpload请求中传递它。如果您确实通过了它,那么在进行PUT调用时,您还需要将其作为请求标头包含在内。