繁体   English   中英

使用 libcurl 上传 C 字符串

[英]Uploading a C String using libcurl

我有一个存储 250KB 数据的字符数组。 我想使用 libcurl 将此字符数组上传到 S3 上的存储桶。 我知道如何上传文件,我可以将所有数据写入文件并将文件发送到 S3,但这会增加不必要的额外步骤。

长话短说,如何使用 libcurl 上传内存的特定部分?

我终于找到了解决方案并在不使用中间文件的情况下成功上传了数据。

如果您不声明读取回调函数,libcurl 使用 fread,它要求您输入文件指针作为输入。 我发现克服这个指针问题的方法是编写一个读取回调函数并输入 void 指针作为 READDATA 的输入。 事不宜迟,这是工作代码。 另一个重要的注意事项是您将上传的框架集的大小。

#include <stdio.h>
#include <curl/curl.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>

int framesetSize = 10;
void *frameset = "0123456789";
static size_t my_read_callback(void *ptr, size_t size, size_t nmemb, void *dummy)
{
    // we will ignore dummy, and pass frameset as a global parameter, along with framesetSize
    size_t retcode = 0;

    if  (framesetSize == 0) 
// we have already finished off all the data
        return retcode; // initialized to 0

    retcode =  ( size * nmemb >= framesetSize) ? framesetSize : size * nmemb; 
// set return code as the smaller of max allowed data and remaining data

    framesetSize -=  retcode; // adjust left amount
    memcpy(ptr, frameset, retcode);
    frameset += retcode ; // advance frameset pointer
    return retcode;
}

int main(void)
{
    CURL *curl;
    CURLcode res;
    double speed_upload, total_time;

    curl = curl_easy_init();
    if(curl) {
        /* upload to this place */
        curl_easy_setopt(curl, CURLOPT_READFUNCTION, my_read_callback);

        curl_easy_setopt(curl, CURLOPT_URL,
                         "54.231.19.24/{YOURBUCKETNAME}/furkan-1.txt");

        /* tell it to "upload" to the URL */
        curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);

        /* set where to read from (on Windows you need to use READFUNCTION too) */
        curl_easy_setopt(curl, CURLOPT_READDATA, &frameset);

        /* and give the size of the upload (optional) */
        curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, framesetSize);

        /* enable verbose for easier tracing */
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

        res = curl_easy_perform(curl);
        /* Check for errors */
        if(res != CURLE_OK) {
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                    curl_easy_strerror(res));

        }
        else {
            /* now extract transfer info */
            curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD, &speed_upload);
            curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total_time);

            fprintf(stderr, "Speed: %.3f bytes/sec during %.3f seconds\n",
                    speed_upload, total_time);

        }
        /* always cleanup */
        curl_easy_cleanup(curl);
    }
    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM