Joh*_*ohn 8 c# amazon-s3 amazon-web-services aws-lambda .net-core-2.0
如何使用C#在Lambda中存储和访问文件我使用了lambda可用的tmp文件夹,但是我收到了无法加载文件或程序集的错误.我该如何解决错误?我使用了ADP nuget.
using (WebClient webClient = new WebClient())
{
webClient.DownloadFile(reportLine, Path.GetTempPath() +
"sample_auth.key");
}
Run Code Online (Sandbox Code Playgroud)
我用它将文件下载到lambda的tmp文件夹中.我没有在字符串保密中包含其他配置,但您可以检查下面的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";
}
Run Code Online (Sandbox Code Playgroud)
我得到一个错误,我认为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",
Run Code Online (Sandbox Code Playgroud)
以下是示例程序: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
Run Code Online (Sandbox Code Playgroud)
无法加载文件或程序集:System.Net.Http.WebRequest
该错误似乎是由版本问题引起的,我认为您需要使用.Net核心版本的System.Net.Http.WebRequestdll或更高版本的.Net 4.0才能使用.NET Core 2.0.
实际上请看到这个答案你可能运气不好:你使用的库需要发布目标.NET Core:https://github.com/dotnet/corefx/issues/28267#issuecomment-396349873
另请参阅/sf/answers/2917865121/并且无法在.NET AWSSDK中为单声道加载文件或程序集"System.Net.Http.Webrequest"以获取类似的版本问题和修复.
如果这不能解决问题,请考虑使用AWS API.您可以将sample_auth.key文件放在S3存储桶上并读取它,例如 https://docs.aws.amazon.com/lambda/latest/dg/with-s3.html
或者根据您链接的示例,他们将json文件打包为Lambda:https: //github.com/adplabs/adp-connection-NET/tree/master/ADPClientDemo/Content/config
他们使用读取它StreamReader,也许这将使用System.IODLL而不是试图找到System.Net.Http.WebRequestDLL:
string configFileName = "default.json";
StreamReader sr = new StreamReader("..\\..\\Content\\config\\" + configFileName);
string clientconfig = sr.ReadToEnd();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1584 次 |
| 最近记录: |