/// <summary> /// 获取文件字节数组 /// </summary> /// <param name="host"></param> /// <param name="id">文件_id值?</param> /// <returns></returns> public static Byte[] DownloadFileAsByteArray(MongodbHost host, ObjectId id) { var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(host); Byte[] bytes = bucket.DownloadAsBytes(id); return(bytes); }
/// <summary> /// 获取文件字节数组 /// </summary> /// <param name="database"></param> /// <param name="id"></param> /// <returns></returns> public static Byte[] DownloadFileAsByteArray(IMongoDatabase database, ObjectId id) { var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(database); Byte[] bytes = bucket.DownloadAsBytes(id); return(bytes); }
/// <summary> /// 下载文件 /// </summary> /// <param name="host"></param> /// <param name="fileName"></param> /// <param name="destination"></param> /// <param name="options"></param> public static Stream DownloadFileToStreamByName(MongodbHost host, string fileName, GridFSDownloadByNameOptions options = null) { Stream destination = new MemoryStream(); var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(host); bucket.DownloadToStreamByName(fileName, destination, options); return(destination); }
/// <summary> /// 获取文件流(有问题?) /// </summary> /// <param name="database"></param> /// <param name="id"></param> /// <returns></returns> public static Stream DownloadFileToStream(IMongoDatabase database, ObjectId id) { Stream destination = new MemoryStream(); var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(database); bucket.DownloadToStream(id, destination); return(destination); }
/// <summary> /// 获取文件流 /// </summary> /// <param name="host"></param> /// <param name="id"></param> /// <returns></returns> public static Stream DownloadFileToStream(MongodbHost host, ObjectId id) { Stream destination = new MemoryStream(); var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(host); bucket.DownloadToStream(id, destination); return(destination); }
/// <summary> /// 获取文件流 /// </summary> /// <param name="host"></param> /// <param name="id">文件id,fs.files中的_id,也对应fs.chunks的files_id</param> /// <param name="options">下载选项</param> public static Stream DownloadFileToStream(MongodbHost host, BsonValue id, GridFSDownloadOptions options = null) { Stream destination = new MemoryStream(); var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(host); bucket.DownloadToStream(id, destination, options); return(destination); }
/// <summary> /// 上传文件流,并附上元数据 /// </summary> /// <param name="host"></param> /// <param name="fileName"></param> /// <param name="source"></param> /// <param name="metadata"></param> /// <returns></returns> public static ObjectId UploadFileFromStream(MongodbHost host, string fileName, Stream source, IDictionary <string, object> metadata) { var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(host); GridFSUploadOptions options = new GridFSUploadOptions { Metadata = new BsonDocument(metadata) }; return(bucket.UploadFromStream(fileName, source, options)); }
/// <summary> /// /// </summary> /// <param name="host"></param> /// <param name="filePath">文件完整路径</param> /// <param name="options"></param> /// <returns></returns> public static ObjectId UploadFileFromPath(MongodbHost host, string filePath, GridFSUploadOptions options = null) { if (!File.Exists(filePath)) { throw new Exception($"{filePath} does not exist"); } var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(host); FileStream fileStream = new FileStream(filePath, FileMode.Open); return(bucket.UploadFromStream(Path.GetFileName(filePath), fileStream, options)); }
/// <summary> /// 重命名所有匹配的文件 /// </summary> /// <param name="host"></param> /// <param name="oldFilename"></param> /// <param name="newFilename"></param> public static void RenameAllfile(MongodbHost host, string oldFilename, string newFilename) { var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(host); var filter = Builders <GridFSFileInfo> .Filter.Eq(x => x.Filename, oldFilename); var filesCursor = bucket.Find(filter); var files = filesCursor.ToList(); foreach (var file in files) { bucket.Rename(file.Id, newFilename); } }
/// <summary> /// 从本地路径上传文件 /// </summary> /// <param name="host"></param> /// <param name="filePath"></param> /// <param name="metadata"></param> /// <returns></returns> public static ObjectId UploadFileFromPath(MongodbHost host, string filePath, IDictionary <string, object> metadata) { if (!File.Exists(filePath)) { throw new Exception($"{filePath} does not exist"); } var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(host); GridFSUploadOptions options = new GridFSUploadOptions { Metadata = new BsonDocument(metadata) }; FileStream fileStream = new FileStream(filePath, FileMode.Open); return(bucket.UploadFromStream(fileStream.Name, fileStream, options)); }
/// <summary> /// 根据文件名查找所有匹配的文件 /// </summary> /// <param name="host">mongo服务器信息</param> /// <param name="fileName">文件名称</param> /// <returns></returns> public static IAsyncCursor <GridFSFileInfo> FindFiles(MongodbHost host, string fileName) { var filter = Builders <GridFSFileInfo> .Filter.And( Builders <GridFSFileInfo> .Filter.Eq(x => x.Filename, fileName) // Builders<GridFSFileInfo>.Filter.Eq("metadata.UserID", fileName) ); var sort = Builders <GridFSFileInfo> .Sort.Descending(x => x.UploadDateTime); var options = new GridFSFindOptions { // Limit = 1, Sort = sort }; var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(host); return(bucket.Find(filter, options)); }
/// <summary> /// 根据元数据查找文件集合 /// </summary> /// <param name="host"></param> /// <param name="metadatas"></param> /// <returns></returns> public static IAsyncCursor <GridFSFileInfo> FindFiles(MongodbHost host, IDictionary <string, object> metadatas) { IList <FilterDefinition <GridFSFileInfo> > filters = new List <FilterDefinition <GridFSFileInfo> >(); foreach (var item in metadatas) { filters.Add(Builders <GridFSFileInfo> .Filter.Eq(item.Key, item.Value)); } var sort = Builders <GridFSFileInfo> .Sort.Descending(x => x.UploadDateTime); var options = new GridFSFindOptions { Limit = 1, Sort = sort }; var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(host); return(bucket.Find(Builders <GridFSFileInfo> .Filter.And(filters), options)); }
/// <summary> /// 查找唯一文件 /// </summary> /// <param name="database"></param> /// <param name="metadatas"></param> /// <returns></returns> public static GridFSFileInfo FindSingleFile(IMongoDatabase database, IDictionary <string, object> metadatas) { IList <FilterDefinition <GridFSFileInfo> > filters = new List <FilterDefinition <GridFSFileInfo> >(); foreach (var item in metadatas) { filters.Add(Builders <GridFSFileInfo> .Filter.Eq(item.Key, item.Value)); } var sort = Builders <GridFSFileInfo> .Sort.Descending(x => x.UploadDateTime); var options = new GridFSFindOptions { Limit = 1, Sort = sort }; var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(database); using (var cursor = bucket.Find(Builders <GridFSFileInfo> .Filter.And(filters), options)) { return(cursor.ToList().FirstOrDefault()); } }
/// <summary> /// 根据文件名下载文件字节数组 /// </summary> /// <param name="host"></param> /// <param name="fileName"></param> /// <returns></returns> public static Byte[] DownloadFileAsBytesByName(MongodbHost host, string fileName) { var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(host); return(bucket.DownloadAsBytesByName(fileName)); }
/// <summary> /// 同步上传 /// </summary> /// <param name="database"></param> /// <param name="fileName"></param> /// <param name="source"></param> /// <param name="options"></param> /// <returns></returns> public static ObjectId UploadFileFromStream(IMongoDatabase database, string fileName, Stream source, GridFSUploadOptions options = null) { var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(database); return(bucket.UploadFromStream(fileName, source, options)); }
/// <summary> /// 异步通过字节流上传文件 /// </summary> /// <param name="host"></param> /// <param name="fileName">文件名</param> /// <param name="source">文件字节</param> /// <param name="options"></param> /// <returns></returns> public static Task <ObjectId> UploadFileFromBytesAsync(MongodbHost host, string fileName, byte[] source, GridFSUploadOptions options = null) { var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(host); return(bucket.UploadFromBytesAsync(fileName, source, options)); }
/// <summary> /// 删除fs数据库 /// </summary> /// <param name="host"></param> public static void DropFS(MongodbHost host) { var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(host); bucket.Drop(); }
/// <summary> /// 根据ObjectId删除文件 /// </summary> /// <param name="host"></param> /// <param name="id">ObjectId</param> public static void DeleteFileById(MongodbHost host, ObjectId id) { var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(host); bucket.Delete(id); }
/// <summary> /// 根据ObjectId删除文件 /// </summary> /// <param name="database"></param> /// <param name="id"></param> public static void DeleteFileById(IMongoDatabase database, ObjectId id) { var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(database); bucket.Delete(id); }
/// <summary> /// 获取文件字节数组 /// </summary> /// <param name="database"></param> /// <param name="fileName"></param> /// <returns></returns> public static Byte[] DownloadFileAsBytesByName(IMongoDatabase database, string fileName) { var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(database); return(bucket.DownloadAsBytesByName(fileName)); }
/// <summary> /// 重命名 /// </summary> /// <param name="host">mongo服务器信息</param> /// <param name="id">文件id,fs.files中的_id,也对应fs.chunks的files_id</param> /// <param name="newFileName">新文件名称</param> public static void RenameFile(MongodbHost host, BsonValue id, string newFileName) { var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(host); bucket.Rename(id, newFileName); }
/// <summary> /// 查找操作 /// </summary> /// <param name="host">mongodb服务器信息</param> /// <param name="filter">查询过滤器</param> /// <param name="options">查找选项</param> /// <returns></returns> public static IAsyncCursor <GridFSFileInfo> FindFiles(MongodbHost host, FilterDefinition <GridFSFileInfo> filter, GridFSFindOptions options = null) { var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(host); return(bucket.Find(filter, options)); }