-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5. Storage system API.cpp
More file actions
51 lines (41 loc) · 1.37 KB
/
5. Storage system API.cpp
File metadata and controls
51 lines (41 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>
#include <curl/curl.h>
// Callback function writes data to a std::string
size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* s) {
size_t newLength = size*nmemb;
size_t oldLength = s->size();
try {
s->resize(oldLength + newLength);
} catch(std::bad_alloc &e) {
// handle memory problem
return 0;
}
std::copy((char*)contents,(char*)contents+newLength,s->begin()+oldLength);
return size*nmemb;
}
// Function to upload an object
void uploadObject(const std::string& url, const std::string& objectData) {
CURL* curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curl, CURLOPT_READDATA, &objectData);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, &WriteCallback);
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
}
curl_easy_cleanup(curl);
}
curl_global_cleanup();
}
int main() {
std::string url = "http://example.com/api/object";
std::string objectData = "Hello, World!";
uploadObject(url, objectData);
return 0;
}