2017-09-21 121 views
0

我想在設備農場(上傳測試或應用程序)中使用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) 

回答

2

我之前在過去的項目中這樣做過。下面是我如何做它的代碼片段:

創建一個新的上傳

#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) 

希望幫助

0

我爲AWS設備農場工作。當某些請求參數與用於簽署URL的內容不匹配時,會發生此問題。我看到這個問題的時候,它與內容類型有關。我建議不要將它傳遞給CreateUpload請求。如果您確實通過了它,那麼在進行PUT調用時,您還需要將其作爲請求標頭。

相關問題