如何使用C#在Lambda中存储和访问文件,我使用了可用于lambda的tmp文件夹,但出现了无法加载文件或程序集的错误。我该如何解决错误?我使用了ADP nuget。
using (WebClient webClient = new WebClient())
{
webClient.DownloadFile(reportLine, Path.GetTempPath() +
"sample_auth.key");
}
我用它来将文件下载到lambda的tmp文件夹中。我没有在字符串secret中包含其他配置,但是您可以在下面的github上找到完全相同的示例。
string config = @"{
""sslCertPath"": ""/tmp/sample.pfx"",
""sslKeyPath"": ""/tmp/sample_auth.key"",
}";
ADPAccessToken token = null;
if (String.IsNullOrEmpty(clientconfig))
{
Console.WriteLine("Settings file or default options not available.");
}
else
{
ClientCredentialConfiguration connectionCfg = JSONUtil.Deserialize<ClientCredentialConfiguration>(clientconfig);
ClientCredentialConnection connection = (ClientCredentialConnection)ADPApiConnectionFactory.createConnection(connectionCfg);
//context.Logger.Log(ADPApiConnection.certificatepath);
//context.Logger.Log(clientconfig);
try
{
connection.connect();
if (connection.isConnectedIndicator())
{
token = connection.accessToken;
// context.Logger.Log("Connected to API end point");
// //Console.WriteLine("Token: ");
// //Console.WriteLine(" AccessToken: {0} ", token.AccessToken);
// //Console.WriteLine(" TokenType: {0} ", token.TokenType);
// //Console.WriteLine(" ExpiresIn: {0} ", token.ExpiresIn);
// //Console.WriteLine(" Scope: {0} ", token.Scope);
// //Console.WriteLine(" ExpiresOn: {0} ", token.ExpiresOn);
// //Console.ReadLine();
}
}
catch (ADPConnectionException e)
{
context.Logger.Log(e.Message);
}
//catch (Exception e)
//{
// context.Logger.Log(e.Message);
//}
//Console.Read();
}
return "Ok";
}
我收到lambda检查/ var / task文件夹的错误
errorMessage": "One or more errors occurred. (Could not load file or
assembly 'System.Net.Http.WebRequest, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file
specified.\n)",
"cause": {
"errorType": "FileNotFoundException",
"errorMessage": "Could not load file or assembly
'System.Net.Http.WebRequest, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file
specified.\n",
以下是示例程序:https://github.com/adplabs/adp-connection-NET/blob/master/ADPClientDemo/Program.cs
我可以在控制台上运行该程序,但是当我尝试在lambda中运行该程序时出现错误。是因为来自AWS的NuGet吗?
我有以下NuGet
Amazon Lambda Core
Amazon Lambda S3 Events
Amazon lambda Serialization json
AWS SDK Core
Microsoft Asp Net Web Api Client
ADP library connection NET
答案 0 :(得分:4)
无法加载文件或程序集:System.Net.Http.WebRequest
该错误似乎是由版本问题引起的,我认为您需要使用System.Net.Http.WebRequest
dll的.Net核心版本或比.Net 4.0更高的版本才能使用.NET Core 2.0。
实际上,请参见以下答案,您可能不走运:您使用的针对.NET Core的库需要提供:https://github.com/dotnet/corefx/issues/28267#issuecomment-396349873
另请参见https://stackoverflow.com/a/41683787/495455和Could not load file or assembly "System.Net.Http.Webrequest" in .NET AWSSDK for mono,以了解类似的版本控制问题和修补程序。
如果仍不能解决问题,请考虑使用AWS API。您可以将sample_auth.key文件放在S3存储桶中并读取它,例如 https://docs.aws.amazon.com/lambda/latest/dg/with-s3.html
或者按照您链接到的示例,它们将Lambda打包为json文件: https://github.com/adplabs/adp-connection-NET/tree/master/ADPClientDemo/Content/config
他们使用StreamReader
读取了它,也许可以使用System.IO
dll来工作,而不是尝试查找System.Net.Http.WebRequest
dll:
string configFileName = "default.json";
StreamReader sr = new StreamReader("..\\..\\Content\\config\\" + configFileName);
string clientconfig = sr.ReadToEnd();