gpt4 book ai didi

c# - Windows 服务上传到 AWS S3

转载 作者:行者123 更新时间:2023-11-30 22:56:17 26 4
gpt4 key购买 nike

我正在尝试创建一个 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 服务是怎么回事?

找到解决方法

我在关注这个 example

正确设置用户的 IAM 策略后,您可以手动设置访问 key 并使用 S3 客户端构造函数的重载自行保密。显然会希望从项目外部的文件中加载它。

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

最佳答案

当您在 Release模式下构建 Windows 服务时,Visual Studio 会将您的 app.config 文件转换为名为 yourprogram.exe.config 的文件。检查此配置文件是否包含连接信息。此文件必须与 exe 位于同一位置。

您的配置指定了 SDK Store 中包含的配置文件。 SDK 商店配置文件特定于特定主机中的特定用户。

当您在控制台中运行该应用程序时,您是在您的用户帐户下运行它。该应用程序将能够在您的 SDK 商店中查找并找到配置文件和关联的凭据。

当您将应用程序作为服务运行时,它将使用不同的用户帐户;因此它将无法在 SDK Store 中找到配置文件,因此将无法连接。

您可以创建一个凭据文件并链接该文件,例如:

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

您使用文本编辑器来管理凭据文件中的配置文件。该文件应命名为 credentials 并存储在您在 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

关于c# - Windows 服务上传到 AWS S3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54542899/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com