C++使用mongo-cxx-driver进行文件GridFS上传下载
#include <algorithm>
#include <iostream>
#include <ostream>
#include <bsoncxx/json.hpp>
#include <bsoncxx/stdx/make_unique.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/gridfs/bucket.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <bsoncxx/types/bson_value/view.hpp>
#include <fstream>
#include <bsoncxx/array/view.hpp>
#include <bsoncxx/builder/basic/array.hpp>
#include <bsoncxx/builder/basic/document.hpp>
#include <bsoncxx/builder/basic/kvp.hpp>
#include <bsoncxx/document/value.hpp>
#include <bsoncxx/document/view.hpp>
#include <bsoncxx/json.hpp>
#include <bsoncxx/stdx/string_view.hpp>
#include <bsoncxx/string/to_string.hpp>
#include <bsoncxx/types.hpp>
#include <bsoncxx/types/bson_value/view.hpp>
#include <bsoncxx/builder/stream/array.hpp>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/builder/stream/helpers.hpp>
#include <bsoncxx/builder/basic/document.hpp>
using namespace mongocxx;
using namespace bsoncxx;
using bsoncxx::stdx::make_unique;
using namespace bsoncxx;
using namespace builder::stream;
int main() {
std::cout << "start" << std::endl;
// 初始化连接
mongocxx::instance inst{};
mongocxx::client conn{ mongocxx::uri{/*数据库地址用户名密码等信息*/}};
auto db = conn["test"];
auto bucket = db.gridfs_bucket();
mongocxx::gridfs::uploader up = bucket.open_upload_stream("img");
std::ifstream fp;
//以读取方式打开jpg文件
fp.open("D:/2.png", std::ios::binary | std::ios::in);
//定位到文件末尾
fp.seekg(0, fp.end);
//获得文件总长度
size_t allLength = fp.tellg();
//将指针定位到文件首
fp.seekg(0, fp.beg);
char* buff = new char[allLength + 10]{ 0 };
fp.read(buff, allLength);
up.write(reinterpret_cast<uint8_t*>(const_cast<char*>(buff)), allLength);
delete[]buff;
buff = nullptr;
auto result = up.close();
// 文件唯一ID
auto kk = result.id();
/**************下载示例***************/
// 通过文件的ID读取文件
std::string idStr = "620f1faaf06f0000de000e52";
// 构造oid构造函数参数
bsoncxx::stdx::string_view fileIdSv = idStr;
// 构造BSON格式ID
auto bsonId = oid(fileIdSv);
builder::stream::document build_doc;
// 构建文档
build_doc << "id" << bsonId;
// 获取文档视图
auto doc = build_doc.view();
// 通过获取文档的值来转化为bsoncxx::types::bson_value::view类型
auto downloader = bucket.open_download_stream(doc["id"].get_value());
// 文件长度
auto file_length = downloader.file_length();
// 读取字节计数
auto bytes_counter = 0;
// 需要的缓冲区大小
auto buffer_size = std::min(file_length, static_cast<std::int64_t>(downloader.chunk_size()));
// 创建缓冲区
auto buffer = make_unique<std::uint8_t[]>(static_cast<std::size_t>(buffer_size));
// 文件存储位置
std::fstream fq;
fq.open("E:/Desktop/3.png", std::ios::binary | std::ios::out);
// 循环接受下载并写入
while (auto length_read =
downloader.read(buffer.get(), static_cast<std::size_t>(buffer_size))) {
bytes_counter += static_cast<std::int32_t>(length_read);
fq.write(reinterpret_cast<char*>(const_cast<uint8_t*>(buffer.get())), length_read);
}
fq.close();
std::cout << "总共接收字节数" << bytes_counter << std::endl;
return 0;
}
作者:Esofar
出处:https://www.cnblogs.com/WindSnowLi/p/16998172.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架