MongoDB EC2 C# 上传问题 - 缺少密钥
本文关键字:密钥 问题 EC2 MongoDB | 更新日期: 2023-09-27 18:32:50
寻求这方面的帮助。
我目前正在构建一个 C# 接口,理想情况下,该接口应该将 JSON 文档上传到存储在 EC2 服务器上的 MongoDB 数据库中。
到目前为止,我已经能够使用 PuTTY 和生成的控制台行界面访问 EC2 服务器上的 Mongo 实例,但无法在 C# 中执行此操作。每次我都收到相同的错误:
Additional information: Unable to connect to server bitnami@ec2-54-174-43-69.compute-1.amazonaws.com:22: A non-recoverable error occurred during a database lookup.
我使用的代码如下(用户名、密码和数据库名称为隐私而修改(:
// Create a metadataObj for upload.
MetadataTemplate metaUpload = createMetadataObj();
// Upload to MongoDB
var connectionString = "mongodb://USERNAME:PASSWORD@bitnami@ec2-54-174-43-69.compute-1.amazonaws.com:22/DATABASE";
var mongoEC2 = new MongoClient(connectionString);
var server = mongoEC2.GetServer();
var database = server.GetDatabase("DATABASE");
var collection = database.GetCollection<MetadataTemplate>("COLLECTION");
// Insert record into DB
collection.Insert(metaUpload);
我想知道是否缺少的是我没有包括在 PuTTY 中使用的 AWS 私有密钥 (ppk(。如果是这种情况,谁能指出我如何将它包含在 Mongo 连接中?截至目前,MongoClient(connectionString(是我与Mongo或EC2连接的唯一形式。是否缺少与 EC2 实例的初步连接?
话又说回来,我承认...我可能完全错过了别的东西。欢迎所有建议。
更新:
以下是我用来创建SSH窗口的代码。
// Create a metadataObj for upload.
MetadataTemplate metaUpload = createMetadataObj();
// Create Secure SSH Connection
var keyFile = new PrivateKeyFile(@"C:'Users'ahaque89'Documents'AmazonUploader'Resources'MYOPENKEY.ppk");
var keyFiles = new[] { keyFile };
var username = "bitnami";
var methods = new List<AuthenticationMethod>();
methods.Add(new PrivateKeyAuthenticationMethod(username, keyFiles));
var con = new ConnectionInfo(@"ec2-54-174-43-69.compute-1.amazonaws.com", 22, username, methods.ToArray());
// Connect through SSH Client
using (var client = new SshClient(con))
{
client.Connect();
var cmd = client.RunCommand("ls");
var output = cmd.Result;
MessageBox.Show(output.ToString()); // Works successfully
// Upload to MongoDB (ec2 server)
var connectionString = "mongodb://USERNAME:PASSWORD@bitnami@ec2-54-174-43-69.compute-1.amazonaws.com:22";
var mongoEC2 = new MongoClient(connectionString);
var server = mongoEC2.GetServer();
var database = server.GetDatabase("DATABASE");
var collection = database.GetCollection<MetadataTemplate>("COLLECTION");
// Insert Record into DB
collection.Insert(metaUpload); // Creates error
}
看起来MongoDB C#驱动程序尚不支持此功能。在这里查看克雷格的回应:https://groups.google.com/forum/#!topic/mongodb-csharp/b8PS4Qz9gjg