Can't upload chunked file with curl
AnsweredHi there:
I am having problems uploading with php a big file in chunks. I have also reproduced my problem using curl from command line. This is what i do:
First we prepare the file borrargrande.txt of 21MB to upload in chunks
#split -b 8388608 borrargrande.txt borrargrande
(Here we obtain 3 files > borrargrandeaa, borrargrandeab and borrargrandeac)
#sha1sum borrargrandeaa
5fde1cce603e6566d20da811c9c8bcccb044d4ae
#echo -n '5fde1cce603e6566d20da811c9c8bcccb044d4ae' | base64
NWZkZTFjY2U2MDNlNjU2NmQyMGRhODExYzljOGJjY2NiMDQ0ZDRhZQ==
(so we get the BASE64_ENCODED_DIGEST)
Now we call to begin the uploading session and it works
# curl -X POST https://upload.box.com/api/2.0/files/upload_sessions \
-H "Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXXX" \
-H "Content-Type: application/json" \
-d '{
"folder_id": "0",
"file_size": 22020096,
"file_name": "borrargrande.txt"
}'
{"total_parts":3,"part_size":8388608,"session_endpoints":{"list_parts":"https://upload.box.com/api/2.0/files/upload_sessions/34B9C1CA812E2C12717C4D65090A7DB9/parts","commit":"https://upload.box.com/api/2.0/files/upload_sessions/34B9C1CA812E2C12717C4D65090A7DB9/commit","log_event":"https://upload.box.com/api/2.0/files/upload_sessions/34B9C1CA812E2C12717C4D65090A7DB9/log","upload_part":"https://upload.box.com/api/2.0/files/upload_sessions/34B9C1CA812E2C12717C4D65090A7DB9","status":"https://upload.box.com/api/2.0/files/upload_sessions/34B9C1CA812E2C12717C4D65090A7DB9","abort":"https://upload.box.com/api/2.0/files/upload_sessions/34B9C1CA812E2C12717C4D65090A7DB9"},"session_expires_at":"2020-01-31T08:34:51Z","id":"34B9C1CA812E2C12717C4D65090A7DB9","type":"upload_session","num_parts_processed":0}
Finally we make the curl to upload the first chunk (borrargrandeaa)
# curl -X PUT https://upload.box.com/api/2.0/files/upload_sessions/34B9C1CA812E2C12717C4D65090A7DB9 \
-H "Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXX" \
-H "Digest: sha=NWZkZTFjY2U2MDNlNjU2NmQyMGRhODExYzljOGJjY2NiMDQ0ZDRhZQ==" \
-H "Content-Range: 0-8388608/22020096" \
-H "Content-Type: application/octet-stream" \
--data-binary @borrargrandeaa
{"code":"invalid_digest","message":"Digest header invalid: sha=NWZkZTFjY2U2MDNlNjU2NmQyMGRhODExYzljOGJjY2NiMDQ0ZDRhZQ==","request_id":"b12a340b6cdfe4bfc0635d8235dcc577"}
I don't now what i am doing wrong. Can you help me?
-
Finaly I found the problem. I was encoding in base64 the hexadecimal string that sha1sum returns, but the api needs to encode the binary result. The correct way to generate the the digest from command line is something like this:
openssl sha1 -binary borrargrandeaa | base64
and in php:
base64_encode(sha1($data,true));
Please sign in to leave a comment.
Comments
1 comment