亚马逊运行时异常:无法找到凭证

10

我们使用AWS SDK工具包在VS 2010中设计了WebService应用程序,它连接到AWS SNS服务。

当我们直接从VS 2010开发工具中运行时,它可以完美地工作, 但是当我们将webservice发布到本地IIS或专用web服务器时,它会失败,并显示以下错误消息。

Amazon.Runtime.AmazonServiceException: Unable to find credentials

Exception 1 of 4:
System.ArgumentException: Path cannot be the empty string or all whitespace.
Parameter name: path
   at System.IO.Directory.GetParent(String path)
   at Amazon.Runtime.StoredProfileAWSCredentials.DetermineCredentialsFilePath(String profilesLocation)
   at Amazon.Runtime.StoredProfileAWSCredentials..ctor(String profileName, String profilesLocation)
   at Amazon.Runtime.EnvironmentAWSCredentials..ctor()
   at Amazon.Runtime.FallbackCredentialsFactory.<Reset>b__1()
   at Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(Boolean fallbackToAnonymous)

Exception 2 of 4:
System.ArgumentException: Path cannot be the empty string or all whitespace.
Parameter name: path
   at System.IO.Directory.GetParent(String path)
   at Amazon.Runtime.StoredProfileAWSCredentials.DetermineCredentialsFilePath(String profilesLocation)
   at Amazon.Runtime.StoredProfileAWSCredentials..ctor(String profileName, String profilesLocation)
   at Amazon.Runtime.FallbackCredentialsFactory.<Reset>b__2()
   at Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(Boolean fallbackToAnonymous)

Exception 3 of 4:
System.InvalidOperationException: The environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY were not set with AWS credentials.
   at Amazon.Runtime.EnvironmentVariablesAWSCredentials..ctor()
   at Amazon.Runtime.FallbackCredentialsFactory.<Reset>b__3()
   at Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(Boolean fallbackToAnonymous)

Exception 4 of 4:
Amazon.Runtime.AmazonServiceException: Unable to reach credentials server
   at Amazon.Runtime.InstanceProfileAWSCredentials.GetContents(Uri uri)
   at Amazon.Runtime.InstanceProfileAWSCredentials.<GetAvailableRoles>d__0.MoveNext()
   at Amazon.Runtime.InstanceProfileAWSCredentials.GetFirstRole()
   at Amazon.Runtime.FallbackCredentialsFactory.<Reset>b__4()
   at Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(Boolean fallbackToAnonymous)
   at Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(Boolean fallbackToAnonymous)
   at Amazon.SimpleNotificationService.AmazonSimpleNotificationServiceClient..ctor()
   at CellicaAwsSnsService..ctor()
   at Service..ctor()

你好,你检查过 Web 服务 URL 是否可以从你的 Web 服务器访问吗?有时防火墙会阻止它。 - sankoobaba
是的,我已经检查过了,而且在本地IIS中也无法工作。当我将其发布到本地IIS时,会出现相同的错误。 - YogeshNC
2个回答

17

在任何可以从 Web 服务应用程序访问的路径上创建证书文件,例如 C:\awsfile\credentials,但请记住不要给此文件添加任何扩展名。文件应包含以下数据。

[default]
aws_access_key_id=[your_access_key]
aws_secret_access_key=[your_secret_key]
在此之后,您需要在Web.config文件中的appsetting标记中设置路径:
<appSettings>
<add key="AWSProfilesLocation" value="C:\awsfile\credentials" />
<add key="AWSRegion" value="us-east-1" />
</appSettings>

已批准 :) 将删除这些评论 - Rippo

2
在Visual Studio的AWS Explorer中,您可以创建用户配置文件,这些配置文件在AWS上给您不同的权限,然后您可以选择要在AWS Explorer中使用哪个配置文件。这些配置文件仅适用于您的Windows用户帐户,如果其他人使用您的计算机,则他们必须创建自己的配置文件。您在用户帐户下运行的任何软件也可以使用这些配置文件。
如果您没有将应用程序配置为使用特定的配置文件,则它将使用"default"配置文件。
此问题发生是因为IIS运行在与您登录的用户帐户不同的用户帐户下,因此无法访问您的AWS配置文件。
有几种方法可以告诉您的应用程序在运行时使用哪个AWS配置文件(请参见http://docs.aws.amazon.com/sdk-for-net/v2/developer-guide/net-dg-config-creds.html)。对于开发人员来说,最简单的选项是创建一个凭据文件,并从web.config引用该文件。例如,如果您创建了一个名为“C:\aws\credentials”的文件,则可以通过将以下内容添加到您的web.config文件来告诉您的应用程序从此凭证文件使用profile2。
<configuration>

  <configSections>
    <section name="aws" type="Amazon.AWSSection, AWSSDK.Core" />
  </configSections>

  <aws 
    region="us-east-1" 
    profileName="profile2"
    profilesLocation="C:\aws\credentials" />

</configuration>

凭据文件的内容应类似于这样:
[profile1]
aws_access_key_id = {accessKey}
aws_secret_access_key = {secretKey}

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

要获取访问密钥和秘密密钥,请转到AWS IAM控制台https://console.aws.amazon.com/iam/home?region=us-east-1#/users,选择您希望应用程序运行的用户,然后单击“安全凭证”选项卡,接着单击“创建访问密钥”按钮。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接