我正在尝试使用 libcurl 将 C/C++ 代码上传到 S3。我正在上传要从回调生成的二进制数据。
我已经盯着这个看了一段时间,所以我可能离题了:(但这是我的代码现在的样子:
首先,我像这样构建我的表单:
struct curl_httppost *buildForm(void *streamData, long contentLength) {
struct curl_httppost *formpost=NULL;
struct curl_httppost *lastptr=NULL;
for( int i=0; i<form.size(); ++i ) {
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, form[i].first.c_str(),
CURLFORM_COPYCONTENTS, form[i].second.c_str(),
CURLFORM_END);
}
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "file",
CURLFORM_STREAM, streamData,
CURLFORM_CONTENTSLENGTH, (long) contentLength,
CURLFORM_FILENAME, "chunk",
//CURLFORM_CONTENTTYPE, "application/octet-stream",
CURLFORM_END);
return formpost;
}
然后我像这样设置请求:
curl_easy_reset( handle );
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Expect:");
curl_easy_setopt( handle, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt( handle, CURLOPT_VERBOSE, 1L );
curl_easy_setopt( handle, CURLOPT_DEBUGFUNCTION, curlDebugger );
curl_easy_setopt( handle, CURLOPT_URL, postInstructions.url.c_str() );
curl_easy_setopt( handle, CURLOPT_POST, 1L);
// set the data and header read callbacks:
curl_easy_setopt( handle, CURLOPT_READFUNCTION, &(UploadCallback::writeDataStatic));
curl_easy_setopt( handle, CURLOPT_HTTPPOST, formpost);
//I have tried with and without this line
curl_easy_setopt( handle, CURLOPT_POSTFIELDSIZE_LARGE, (curl_off_t)contentLength);
curl_easy_setopt( handle, CURLOPT_WRITEFUNCTION, &(XonamiResponse::readDataStatic));
curl_easy_setopt( handle, CURLOPT_WRITEDATA, &response);
curl_easy_setopt( handle, CURLOPT_HEADERFUNCTION, &(XonamiResponse::readHeaderStatic));
curl_easy_setopt( handle, CURLOPT_WRITEHEADER, &response);
// do it!
int success = curl_easy_perform( handle );
然而,卷曲抱怨:
operation aborted by callback
即使我生成的字节数与 contentLength 相同。当我将回调设置为只用零填充缓冲区时,只要请求数据,S3 就会响应:
The body of your POST request is not well-formed multipart/form-data.
看着原始形式,我觉得最后一个(关闭)边界不见了。
curl --version 说:
curl 7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8r zlib/1.2.3 协议:tftp ftp telnet dict ldap http 文件 https ftps 功能:GSS-Negotiate IPv6 Largefile NTLM SSL libz