首页 > 其他分享 >libcurl之文件上传阿里云

libcurl之文件上传阿里云

时间:2022-10-27 16:38:29浏览次数:49  
标签:std setopt libcurl pCurl 阿里 easy curl 上传 CURLOPT

结构顺序如下:

配置请求头->数据准备->初始化libcurl->设置libcurl属性->设置请求头->执行libcurl->释放libcurl

 

部分执行源码:

typedef struct _DUMP_PROCESS_INFO
{
  WCHAR filePath[MAX_PATH];
  WCHAR exeName[MAX_PATH];
  WCHAR canonFolderName[MAX_PATH];
  DWORD dwProcessId;
  DWORD dwThreadId;
  bool bMiniDump;
  bool bUploadDump;
  EXCEPTION_POINTERS *pException;
}DUMP_PROCESS_INFO, *PDUMP_PROCESS_INFO;

void uploadDump(DUMP_PROCESS_INFO *dumpInfo)
{
// get filename
  std::string folderName = global::wstr2str(dumpInfo->canonFolderName);
  std::string filePath = global::wstr2str(dumpInfo->filePath);
  size_t pos = filePath.find_last_of("\\");
  std::string fileName = filePath.substr(pos + 1);
  std::string fileUrl;

 

  std::ostringstream ss;
  ss << "/" << BUCKET_DUMP_NAME << "/" << folderName << "/" << fileName;
  std::string canonRes = ss.str();

  ss.str("");
  ss << "https://" << BUCKET_DUMP_NAME << "." << OSS_ENDPOINT << "/" << folderName <<
  "/" << fileName;
  fileUrl = ss.str();

  //打开文件将需要上传的文件加载到内存
  FILE* fd;
  fopen_s(&fd, filePath.c_str(), "rb");
  if (!fd)
  {
    global::outputDebugPrintf("sdcrash open file is error");
    return;
  }

  // get the file size
  struct _stat64i32 fileInfo;
  if (_fstat(_fileno(fd), &fileInfo) != 0)
  {
    global::outputDebugPrintf("dumpfile size is null");
    return;
  }

  //上传配置的请求头,时间戳,日期,token,authorization请求标头包含用于向服务器认证用户代理的凭证
  std::string szDate = getGmTime();
  std::string header_date = std::string("Date:") + szDate;
  std::string header_content_type = getContentType();
  std::string header_authorization = getOssAuthorization(szDate, canonRes);
  std::string header_token = OSS_TOKEN";";

  CURLcode res;
  long responseCode = 0;
  curl_off_t speed_upload, total_time;
  curl_global_init(CURL_GLOBAL_DEFAULT);
  CURL* pCurl = curl_easy_init();
  if (pCurl)
  {
    curl_easy_setopt(pCurl, CURLOPT_URL, fileUrl.c_str());//上传服务器路径
    curl_easy_setopt(pCurl, CURLOPT_UPLOAD, 1L); //启用上传
    curl_easy_setopt(pCurl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);//上传协议
    curl_easy_setopt(pCurl, CURLOPT_CUSTOMREQUEST, "PUT");//请求方式
    curl_easy_setopt(pCurl, CURLOPT_PUT, 1); //启用PUT请求方式
    curl_easy_setopt(pCurl, CURLOPT_READFUNCTION, read_callback);//读取数据后返回callback
    curl_easy_setopt(pCurl, CURLOPT_READDATA, (void*)fd); //需要上传的目标数据流
    curl_easy_setopt(pCurl, CURLOPT_POSTFIELDSIZE_LARGE, fileInfo.st_size);//需要上传目标数据的大小
    curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, 0L);//关闭校验证书真实性
    curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYHOST, 0L);//关闭校验证书中主机名
    curl_easy_setopt(pCurl, CURLOPT_VERBOSE, 1L); //生产环境,默认关闭,使库显示有关其在此句柄上的操作的大量详细信息。 对 libcurl 和/或协议调试和理解很有用
    curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, 30L);//超时秒为单位
    curl_easy_setopt(pCurl, CURLOPT_CONNECTTIMEOUT, 10L);//链接超时,秒为单位

    std::string response = "";
    curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, write_callback);//写入数据回调
    curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, &response);//传递给写回调数据指针,win32不写可能会crash

    struct curl_slist *headers = NULL;
    headers = curl_slist_append(headers, header_content_type.c_str());
    headers = curl_slist_append(headers, header_date.c_str());
    headers = curl_slist_append(headers, header_token.c_str());
    headers = curl_slist_append(headers, header_authorization.c_str());

    curl_easy_setopt(pCurl, CURLOPT_HTTPHEADER, headers);//自定义http请求头
    res = curl_easy_perform(pCurl);//执行libcurl,并获取执行结果
  if (res != CURLE_OK)
  {
    global::outputDebugPrintf("curl_easy_perform() failed: %s\n",
    curl_easy_strerror(res));
  }
  else
  {
    curl_easy_getinfo(pCurl, CURLINFO_SPEED_UPLOAD_T, &speed_upload);
    curl_easy_getinfo(pCurl, CURLINFO_TOTAL_TIME_T, &total_time);
    curl_easy_getinfo(pCurl, CURLINFO_RESPONSE_CODE, &responseCode);

    //打印上传内容的附加信息

    global::outputDebugPrintf("Speed: %" CURL_FORMAT_CURL_OFF_T " bytes/sec during %"
    CURL_FORMAT_CURL_OFF_T ".%06ld seconds\n",
    speed_upload,
    (total_time / 1000000), (long)(total_time % 1000000));
  }
  /* always cleanup */
  curl_easy_cleanup(pCurl);
 }
  fclose(fd);
}

标签:std,setopt,libcurl,pCurl,阿里,easy,curl,上传,CURLOPT
From: https://www.cnblogs.com/liuruoqian/p/16832718.html

相关文章

  • 使用RestTemplate上传文件
    1、使用form表单上传文件文件发送:@TestpublicvoidstoreFile2(){Filefile=newFile("D:\\test.txt");StringuploadUrl="http://127.0.0.1:8080/uplo......
  • 整理牛客网---阿里校招笔试后端Java版,dfs和算法题。
    一、2021(校招)阿里巴巴7.22笔试(Java版)1.1题目1给定一个n,求[1,n]这n个数字的排列组合有多少个。条件:相邻的两个数字的绝对值不能等于1.例如:4[2,4,1,3][3,1,4......
  • SpringMVC处理上传文件
    SpringMVC处理文件上传SpringMVC为文件上传提供了直接支持,这种支持是通过即插即用的MultipartResolver实现.Spring使用JakartaCommonsFileUpload技术实现了一个Mult......
  • Docker配置阿里云镜像加速
    Docker配置阿里云镜像加速登录阿里云:https://cn.aliyun.com控制台--->产品与服务--->容器镜像服务阿里云镜像加速器配置地址:https://kgmux0ys.mirror.ali......
  • Springboot 一行代码实现文件上传 20个平台!少写代码到极致
    又是做好人好事的一天,有个小可爱私下问我有没有好用的springboot文件上传工具,这不巧了嘛,正好我私藏了一个好东西,顺便给小伙伴们也分享一下,demo地址放在文末了。文件上传......
  • 阿里云实时计算 Flink 版 x Hologres: 构建企业级一站式实时数仓
    作者|徐榜江余文兵赵红梅编辑|伍翀随着大数据的迅猛发展,企业越来越重视数据的价值,这就意味着需要数据尽快到达企业分析决策人员,以最大化发挥数据价值。企业最常见的做法就......
  • Docker 使用阿里云加速拉取官方镜像
    首先登陆阿里云容器镜像服务控制台,在左侧导航栏选择镜像工具 > 镜像加速器,在镜像加速器页面获取镜像加速地址。例如:加速器地址:[系统分配前缀].mirror.aliyuncs.com......
  • JQuery ajax上传多个文件 java
    HTML<divid="divImg"><inputtype="file"class="file"name="file"multiple="multiple"id="file"/><buttontype="button"onclick="uploadFile()">上传</......
  • 阿里巴巴不推荐使用JDK自带工具类创建线程池的原因
    目录一、线程和线程池的关系线程复用二、阿里巴巴为什么不推荐使用JDK自带工具类创建线程池Executors.newCachedThreadPool()Executors.newFixedThreadPool(10)Executors.n......
  • 阿里云市场AtomData购买与部署指南
    本文主要描述企业用户如何从阿里云市场购买、使用石原子公司的AtomData企业级实时数仓产品。1、购买1.1找到商品通过阿里云官网的云市场类目进入云市场首页,搜索AtomData......