C# lambda函数在项目中找不到json文件

问题描述 投票:0回答:1

我的 lambda 函数使用 json 凭证文件 (./credentials/myfile.json)。 在我的计算机或 lambdar 测试环境上一切都运行良好,但是当我将其部署到 AWS 时,无论文件位于何处,它都可以找到该文件。

现在我的文件树是:

我的功能

  • 凭证
    • backupuser_OAuth2_Credentials.json
    • origin_GDrive_Credentials.json
  • 公用事业
    • GoogleDriveUtilities.cs
  • 函数.cs
  • myFunction.csproj

我正在尝试以这种方式访问凭据:

在 Function.cs 中:

string originCredentialPath = "./Credentials/origin_GDrive_Credentials.json";

GoogleDriveUtilities origin_gdu = new GoogleDriveUtilities(originCredentialPath, "Origin account", 0); 

在我的实用程序类中:

        string[] scopes = new string[]
        {
            //GDrive access
            DriveService.Scope.Drive,
        };

        GoogleCredential credential;

        try
        {
            using (var stream = new FileStream(credentialPath, FileMode.Open, FileAccess.Read))
            {
                credential = GoogleCredential.FromStream(stream)
                    .CreateScoped(DriveService.Scope.Drive);
            }

            driveService = new DriveService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = applicationName,
            });

            Console.WriteLine("Google Drive service created: " + this.serviceName + ".");
            return driveService;
        }
        catch (Exception e)
        {
            Console.WriteLine("Error when creating Google Drive service:\n >>" + e);
        }
        return null;

执行时,我在cloudwatch日志中出现以下错误:

System.IO.DirectoryNotFoundException: Could not find a part of the path '/var/task/Credentials/origin_GDrive_Credentials.json'.

有什么线索吗?

谢谢!

c# aws-lambda
1个回答
0
投票

最后我使用 AWS Secrets 代替使用文件解决了这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.