簡體   English   中英

Windows 服務上傳到 AWS S3

[英]Windows Service Upload to AWS S3

我正在嘗試創建一個 Windows 服務,該服務會定期檢查文件夾並將創建的任何新文件上傳到 S3 存儲桶中。

代碼似乎沒有找到我的 app.config 文件,因此沒有找到 AWSProfileName 或任何其他值。

 private bool Upload(List<string> files,string bucketname, RegionEndpoint bucketRegion)
    {
        IAmazonS3 S3Client = new AmazonS3Client(bucketRegion);
        TransferUtility tranUtil = new TransferUtility(S3Client);
        foreach (string file in files)
        {
            tranUtil.Upload(file, bucketname, file.Replace(configFolderPath, ""));
        }
        return true;
    }

拋出這個異常

System.NullReferenceException: 'Object reference not set to an instance of an object.'

這也是我的應用程序配置(刪除了值)

 <?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>
  <appSettings>
    <!--AWSProfileName is used to reference an account that has been registered with the SDK.
If using AWS Toolkit for Visual Studio then this value is the same value shown in the AWS Explorer.
It is also possible to register an account using the <solution-dir>/packages/AWSSDK-X.X.X.X/tools/account-management.ps1 PowerShell script
that is bundled with the nuget package under the tools folder.
-->
    <add key="AWSProfileName" value="******" />
    <add key="profile" value="******" />
    <add key="region" value="******" />
    <add key="configuration" value="******" />
    <add key="framework" value="netcoreapp2.1" />
  </appSettings>
</configuration>

我可以在控制台應用程序中運行完全相同的代碼和 app.config,它將毫無問題地上傳文件。

我還試圖打破憑據:

private bool Upload(List<string> files,string bucketname, RegionEndpoint bucketRegion)
        {
            AWSCredentials creds = new StoredProfileAWSCredentials("My AWS Profile Name");
            IAmazonS3 S3Client = new AmazonS3Client(creds, bucketRegion);
            TransferUtility tranUtil = new TransferUtility(S3Client);...

只是為了得到這個例外:

System.ArgumentException: 'App.config does not contain credentials information. Either add the AWSAccessKey and AWSSecretKey properties or the AWSProfileName property.'

但我確實在配置中有 AWSProfileName ......

打破這一點的 Windows 服務是怎么回事?

找到解決方法

我正在關注這個例子

正確設置用戶的 IAM 策略后,您可以使用 S3 客戶端構造函數的重載自己手動設置訪問密鑰和機密。 顯然想從項目外部的文件中加載它。

        IAmazonS3 S3Client = new AmazonS3Client("IAM USER ACCESS KEY ID", "IAM USER ACCESS KEY SECRET", bucketRegion);

在發布模式下構建 Windows 服務時,Visual Studio 會將 app.config 文件轉換為名為 yourprogram.exe.config 的文件。 檢查此配置文件是否包含連接信息。 此文件必須與 exe 位於同一位置。

您的配置正在指定 SDK Store 中包含的配置文件。 SDK Store 配置文件特定於特定主機中的特定用戶。

當您在控制台中運行該應用程序時,您是在您的用戶帳戶下運行它。 該應用程序將能夠在您的 SDK 商店中查找並找到配置文件和關聯的憑據。

當您將應用程序作為服務運行時,它將使用不同的用戶帳戶; 因此,它將無法在 SDK Store 中找到配置文件,因此將無法連接。

您可以創建一個憑據文件並鏈接,例如:

    <configuration>
      <appSettings>
        <add key="AWSProfileName" value="xxxxxx"/>
        <add key="AWSProfilesLocation" value="C:\aws_service_credentials\credentials"/>
      </appSettings>
    </configuration>

您可以使用文本編輯器來管理憑證文件中的配置文件。 該文件應命名為憑證並存儲在您在 AWSProfilesLocation 中指定的位置

每個配置文件具有以下格式:

[{profile_name}]
aws_access_key_id = {accessKey}
aws_secret_access_key = {secretKey}

更多信息:
https://docs.aws.amazon.com/sdk-for-net/v2/developer-guide/net-dg-config-creds.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM