-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreateFile.cpp
41 lines (34 loc) · 1.29 KB
/
createFile.cpp
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
#include "Appwrite.hpp"
#include <iostream>
#include <fstream>
#include <vector>
int main() {
std::string projectId = "66fbb5a100070a3a1d19";
std::string apiKey = "";
std::string bucketId = "bucket12322";
std::string fileName = "example.txt";
std::string filePath = "examples/storage/files/example.txt";
Appwrite appwrite(projectId);
Storage& storage = appwrite.getStorage();
storage.setup(apiKey, projectId);
try {
std::ifstream file(filePath, std::ios::binary | std::ios::ate);
if (!file.is_open()) {
std::cerr << "Failed to open file: " << filePath << std::endl;
return 1;
}
auto fileSize = file.tellg();
file.seekg(0, std::ios::beg);
std::string fileContent(fileSize, '\0');
if (!file.read(&fileContent[0], fileSize)) {
std::cerr << "Failed to read file content." << std::endl;
return 1;
}
std::vector<std::string> permissions = {"role:all"};
std::string response = storage.createFile(bucketId, fileName, fileContent, permissions);
std::cout << "File created successfully!\n\nResponse: " << response << std::endl;
} catch (const AppwriteException& ex) {
std::cerr << "Exception: " << ex.what() << std::endl;
}
return 0;
}