在 MonoDevelop 中使用带有官方 C# 驱动程序的 GridFS
Using GridFS with official C# driver in MonoDevelop
我在 PC-BSD 10.1 上使用 MonoDevelop 并使用 MongoDB 3.2。我从 Nuget 下载了 MongoDB.Driver (+Bson& Core)。我可以进行基本的读写,并试图通过遵循 Whosebug 中最新的示例来让 GridFS 工作:
MongoDB GridFs with C#, how to store files such as images?
首先,我的系统无法识别(看似)静态的 MongoServer class,因此我切换到 MognoClient 来获取数据库。然后我得到以下信息:
"Type MongoDB.Driver.IMongoDatabase' does not contain a definition for
GridFS' and no extension method GridFS' of type
MongoDB.Driver.IMongoDatabase' could be found. "
using System;
using System.IO;
using MongoDB;
using MongoDB.Driver;
using MongoDB.Driver.Core;
using MongoDB.Bson;
//using MongoDB.Driver.GridFS; -> an attempt to use the legacy driver.
namespace OIS.Objektiv.SocketServer
{
public class Gridfs
{
public Gridfs ()
{
var server = MongoServer.Create("mongodb://localhost:27017");
var database = server.GetDatabase("test");
// var client = new MongoClient("mongodb://localhost:27017");
// var database = client.GetDatabase("test");
var fileName = "D:\Untitled.png";
var newFileName = "D:\new_Untitled.png";
using (var fs = new FileStream(fileName, FileMode.Open))
{
var gridFsInfo = database.GridFS.Upload(fs, fileName);
var fileId = gridFsInfo.Id;
ObjectId oid= new ObjectId(fileId);
var file = database.GridFS.FindOne(Query.EQ("_id", oid));
using (var stream = file.OpenRead())
{
var bytes = new byte[stream.Length];
stream.Read(bytes, 0, (int)stream.Length);
using(var newFs = new FileStream(newFileName, FileMode.Create))
{
newFs.Write(bytes, 0, bytes.Length);
}
}
}
}
}
}
我犯了什么愚蠢的错误? GridFS 是否有我缺少的依赖项?这应该工作! :(
丁斯代尔
您已下载2.0版驱动。它目前没有 GridFS API。您可以在此处跟踪该功能 (https://jira.mongodb.org/browse/CSHARP-1191)。另外,MongoServer在2.0API中已经没有了。
但是,如果您提取 mongocsharpdriver nuget 包,则可以使用遗留 API 的包装器。这样,您将同时拥有 MongoServer 和 GridFS。
对于驱动程序版本 2.2,您必须下载一个名为 MongoDB.Driver.GridFS
的单独 NuGet 包。
你可以这样使用它:
IMongoDatabase database;
var bucket = new GridFSBucket(database, new GridFSOptions
{
BucketName = "videos",
ChunkSizeBytes = 1048576, // 1MB
WriteConcern = WriteConcern.Majority,
ReadPreference = ReadPeference.Secondary
});
IGridFSBucket bucket;
bytes[] source;
var options = new GridFSUploadOptions
{
ChunkSizeBytes = 64512, // 63KB
Metadata = new BsonDocument
{
{ "resolution", "1080P" },
{ "copyrighted", true }
}
};
var id = bucket.UploadFromBytes("filename", source, options);
我在 PC-BSD 10.1 上使用 MonoDevelop 并使用 MongoDB 3.2。我从 Nuget 下载了 MongoDB.Driver (+Bson& Core)。我可以进行基本的读写,并试图通过遵循 Whosebug 中最新的示例来让 GridFS 工作:
MongoDB GridFs with C#, how to store files such as images?
首先,我的系统无法识别(看似)静态的 MongoServer class,因此我切换到 MognoClient 来获取数据库。然后我得到以下信息:
"Type MongoDB.Driver.IMongoDatabase' does not contain a definition for
GridFS' and no extension method GridFS' of type
MongoDB.Driver.IMongoDatabase' could be found. "
using System;
using System.IO;
using MongoDB;
using MongoDB.Driver;
using MongoDB.Driver.Core;
using MongoDB.Bson;
//using MongoDB.Driver.GridFS; -> an attempt to use the legacy driver.
namespace OIS.Objektiv.SocketServer
{
public class Gridfs
{
public Gridfs ()
{
var server = MongoServer.Create("mongodb://localhost:27017");
var database = server.GetDatabase("test");
// var client = new MongoClient("mongodb://localhost:27017");
// var database = client.GetDatabase("test");
var fileName = "D:\Untitled.png";
var newFileName = "D:\new_Untitled.png";
using (var fs = new FileStream(fileName, FileMode.Open))
{
var gridFsInfo = database.GridFS.Upload(fs, fileName);
var fileId = gridFsInfo.Id;
ObjectId oid= new ObjectId(fileId);
var file = database.GridFS.FindOne(Query.EQ("_id", oid));
using (var stream = file.OpenRead())
{
var bytes = new byte[stream.Length];
stream.Read(bytes, 0, (int)stream.Length);
using(var newFs = new FileStream(newFileName, FileMode.Create))
{
newFs.Write(bytes, 0, bytes.Length);
}
}
}
}
}
}
我犯了什么愚蠢的错误? GridFS 是否有我缺少的依赖项?这应该工作! :(
丁斯代尔
您已下载2.0版驱动。它目前没有 GridFS API。您可以在此处跟踪该功能 (https://jira.mongodb.org/browse/CSHARP-1191)。另外,MongoServer在2.0API中已经没有了。
但是,如果您提取 mongocsharpdriver nuget 包,则可以使用遗留 API 的包装器。这样,您将同时拥有 MongoServer 和 GridFS。
对于驱动程序版本 2.2,您必须下载一个名为 MongoDB.Driver.GridFS
的单独 NuGet 包。
你可以这样使用它:
IMongoDatabase database;
var bucket = new GridFSBucket(database, new GridFSOptions
{
BucketName = "videos",
ChunkSizeBytes = 1048576, // 1MB
WriteConcern = WriteConcern.Majority,
ReadPreference = ReadPeference.Secondary
});
IGridFSBucket bucket;
bytes[] source;
var options = new GridFSUploadOptions
{
ChunkSizeBytes = 64512, // 63KB
Metadata = new BsonDocument
{
{ "resolution", "1080P" },
{ "copyrighted", true }
}
};
var id = bucket.UploadFromBytes("filename", source, options);