c# - AWS Lambda 访问本地资源或存储 C#

标签 c# amazon-web-services amazon-s3 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");
        }

我用它来将文件下载到 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

最佳答案

Could not load file or assembly: System.Net.Http.WebRequest

该错误似乎是由版本问题引起的,我认为您需要使用.Net 核心版本的System.Net.Http.WebRequest dll 或高于.Net 4.0 的版本才能使用 .NET Core 2.0。

实际上请看这个答案,您可能不走运:您使用的库需要以 .NET Core 为目标发布:https://github.com/dotnet/corefx/issues/28267#issuecomment-396349873

另见 https://stackoverflow.com/a/41683787/495455Could 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 动态链接库:

string configFileName = "default.json";
StreamReader sr = new StreamReader("..\\..\\Content\\config\\" + configFileName);
string clientconfig = sr.ReadToEnd();

关于c# - AWS Lambda 访问本地资源或存储 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52547210/

相关文章:

c# - 我怎样才能处理一个对象

amazon-web-services - EBS 快照可以保留多长时间?

python - 在 python 中生成一个 1 GB 的文件

hash - 使用 v4 身份验证将对象 PUT 到 S3,无需散列有效负载

php - AWS S3 查询字符串授权

c# - 如何仅在用户停止输入时处理 TextChanged 事件?

c# - CSS 不会通过捆绑呈现,如果直接写在页面上可以正常工作

c# - 计算数组中元素的差异

sql - AWS RDS Postgres : No space left on Device

amazon-s3 - S3 湖形成治理表和数据 block 增量表之间的主要区别是什么?