上传文件到AWS设备农场
问题描述:
我想在设备农场(上传测试或应用程序)中使用python + boto3创建上传。 “create_upload”方法可以正常工作,因为它返回一个上传的arn和url以上传到它。上传文件到AWS设备农场
当我尝试使用要求上传文件到这个网址我得到一个错误:
<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)
答
我之前在过去的项目中这样做过。下面是我如何做它的代码片段:
创建一个新的上传
#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)
希望帮助
答
我为AWS设备农场工作。当某些请求参数与用于签署URL的内容不匹配时,会发生此问题。我看到这个问题的时候,它与内容类型有关。我建议不要将它传递给CreateUpload请求。如果您确实通过了它,那么在进行PUT调用时,您还需要将其作为请求标头。