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;
}

猜你喜欢

转载自blog.csdn.net/qq_44575789/article/details/123017031