2

I have some difficulty in translating cURL options into Python requests for uploading a file to a cloud server Picloud. Based on their description, the first step is to POST a request to make a new file to PiCloud to create a ticket for this upload. The second step is to send instructions extracted from this ticket to upload the file.

I am OK with the first step. But have questions about the second one.

1st step, create a upload ticket.

cURL statement:

$ curl -k -u 'key:secret_key' -X POST https://api.picloud.com/file/new/ -d name=file_name

My requests code:

r = requests.post('https://api.picloud.com/file/new/',auth=(api_key,api_secretkey), verify=False, data={'name':'file_name'})

The ticket looks like:

{"ticket": {"AWSAccessKeyId": "AKIAJCY7JV52WD4MJSNQ", 
"success_action_redirect": "https://api.picloud.com/file/uploaded/3303/4600939/4a6317595a03f85fbbe9102b2454119dc35d3969/", 
"acl": "private", 
"key": "44ec7b76cb3e046986e82b3f13b2da30208d43a5/your_file_name", 
"signature": "TzFhvowtJnBm2+HCvTSlZkBxM5w=", 
"policy": "eyJjb25kaXRpb25zIjogW3siYnVja2V0IjogInBpLXVzZXItZmlsZXMifSwgeyJrZXkiOiAiNDRlYzdiNzZjYjNlMDQ2OTg2ZTgyYjNmMTNiMmRhMzAyMDhkNDNhNS95b3VyX2ZpbGVfbmFtZSJ9LCB7ImFjbCI6ICJwcml2YXRlIn0sIHsic3VjY2Vzc19hY3Rpb25fcmVkaXJlY3QiOiAiaHR0cHM6Ly9hcGkucGljbG91ZC5jb20vZmlsZS91cGxvYWRlZC8zMzAzLzQ2MDA5MzkvNGE2MzE3NTk1YTAzZjg1ZmJiZTkxMDJiMjQ1NDExOWRjMzVkMzk2OS8ifSwgWyJzdGFydHMtd2l0aCIsICIkQ29udGVudC1UeXBlIiwgIiJdXSwgImV4cGlyYXRpb24iOiAiMjAxMi0wNS0xMVQwMTo1MDo0NFoifQ==",
"Content-Type": "application/octet-stream"}, 
"params": {"action": "https://pi-user-files.s3-external-1.amazonaws.com/"}}

2nd step, upload the file.

Basically, I do not quite understand in translating cURL -L and -F options. Based on instructions from cURL, -L means redirect, and -F means fill in as form.

cURL statement:

$ curl -L "https:/.s3.amazonaws.com/" -F "AWSAccessKeyId=1"  -F "success_action_redirect=https://api.picloud.com/file/uploaded/1234" -F "acl=info" -F "key=info"  -F "signature=info" -F "policy=info" -F content-type="info" -F file=your_file_to_upload

My bad trial:

up_d={"AWSAccessKeyId": "AKIAJCY7JV52WD4MJSNQ", "success_action_redirect": "https://api.picloud.com/file/uploaded/3303/4596631/28ec3c3898bec2523759e15cd62fb39fa62b5c53/", 
      "acl": "private", "key": "44ec7b76cb3e046986e82b3f13b2da30208d43a5/your_file_name", "signature": "BLsaltt+Ohua4tPPISr2eV2Tr8c=", 
      "policy": "eyJjb25kaXRpb25zIjogW3siYnVja2V0IjogInBpLXVzZXItZmlsZXMifSwgeyJrZXkiOiAiNDRlYzdiNzZjYjNlMDQ2OTg2ZTgyYjNmMTNiMmRhMzAyMDhkNDNhNS95b3VyX2ZpbGVfbmFtZSJ9LCB7ImFjbCI6ICJwcml2YXRlIn0sIHsic3VjY2Vzc19hY3Rpb25fcmVkaXJlY3QiOiAiaHR0cHM6Ly9hcGkucGljbG91ZC5jb20vZmlsZS91cGxvYWRlZC8zMzAzLzQ1OTY2MzEvMjhlYzNjMzg5OGJlYzI1MjM3NTllMTVjZDYyZmIzOWZhNjJiNWM1My8ifSwgWyJzdGFydHMtd2l0aCIsICIkQ29udGVudC1UeXBlIiwgIiJdXSwgImV4cGlyYXRpb24iOiAiMjAxMi0wNS0xMVQwMDozOTo0OFoifQ==", 
      "Content-Type": "application/octet-stream", "file":"new  2.txt"}
    k1=requests.post('https://pi-user-files-test.s3.amazonaws.com/', data=up_d)

The error message is

<Error><Code>InvalidArgument</Code><Message>Conflicting query string parameters: acl, policy</Message><ArgumentValue>acl</ArgumentValue><ArgumentName>ResourceType</ArgumentName><RequestId>4767425F264FB0CA</RequestId><HostId>K/pAE6BXLJaRRo56ETWw2uYZed8GR+1+VPPU3TI0pZGNyrqz/PAWSzuwtIAHz9eE</HostId></Error>

Thanks for your help!

4

1 回答 1

1

看起来您在正文中不需要参数或 AWSAccessKeyID。此外,对我来说,他们告诉您将 Content-Type 卡在正文中而不是将其设置在标头中,这对我来说似乎很奇怪……无论如何,您还需要在请求中设置标头。根据 curl 文档,-F 将标头中的 Content-Type 设置为 multipart/form-data,因此您也应该在 Python 中这样做。

于 2012-05-10T15:42:40.053 回答