- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在开始之前,我已经阅读了 SO post与同一问题相关。然而该帖子中的 SDK 版本是旧的。根据AWS nugetpage有新版本 3 可用,其中每个服务都有单独的包。
The new version 3 of the AWS SDK for .NET uses separate packages for each service. For example Amazon S3 is in the AWSSDK.S3 package, Amazon SQS is in AWSSDK.SQS and Amazon DynamnoDB is in AWSSDK.DynamoDBv2.
所以我已经从 nuget 安装了 AWSSDK.S3
包,它还安装了依赖项 AWSSDK.Core
。我使用的存储桶受到 IP 的限制,因此我执行代码的机器可以完全访问该存储桶。
下面是我将文件上传到 S3 的代码。但是,当我使用 new AmazonS3Client(RegionEndpoint.USWest2);
AmazonS3Client
的新实例时,出现异常
再次注意,我在创建实例时遇到异常,而不是在上传文件时遇到异常。
var s3Client = new AmazonS3Client(RegionEndpoint.USWest2); //exception at this line
var bucketName = "mybucketname";
PutObjectRequest putRequest1 = new PutObjectRequest
{
BucketName = bucketName,
Key = "MyKey/test1.pdf",
FilePath = filePath
};
PutObjectResponse response1 = s3Client.PutObject(putRequest1);
但是我得到了异常(exception)
[15:57:43 ERR] Unhandled exception Amazon.Runtime.AmazonServiceException: Unable to find credentials
Exception 1 of 5: System.InvalidOperationException: The app.config/web.config files for the application did not contain credential information at Amazon.Runtime.AppConfigAWSCredentials..ctor() in E:\JenkinsWorkspaces\v3-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\AWSCredentials.cs:line 977 at Amazon.Runtime.FallbackCredentialsFactory.<>c.b__6_0() in E:\JenkinsWorkspaces\v3-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\AWSCredentials.cs:line 2098 at Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(Boolean fallbackToAnonymous) in E:\JenkinsWorkspaces\v3-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\AWSCredentials.cs
Exception 2 of 5: System.ArgumentException: App.config does not contain credentials information. Either add the AWSAccessKey and AWSSecretKey properties or the AWSProfileName property. at Amazon.Runtime.StoredProfileAWSCredentials..ctor(String profileName, String profilesLocation) in E:\JenkinsWorkspaces\v3-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\AWSCreden at Amazon.Runtime.FallbackCredentialsFactory.<>c.b__6_1() in E:\JenkinsWorkspaces\v3-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\AWSCredentials.cs:line 2099 at Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(Boolean fallbackToAnonymous) in E:\JenkinsWorkspaces\v3-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\AWSCredentials.cs
Exception 3 of 5: System.ArgumentException: Profile '' was not found or could not be loaded from the SDK credential store. Verify that the profile name and data are correct. at Amazon.Runtime.StoredProfileFederatedCredentials..ctor(String profileName, String profilesLocation, WebProxy proxySettings) in E:\JenkinsWorkspaces\v3-stage-release\AWSDotNetPublic\sdk\src at Amazon.Runtime.FallbackCredentialsFactory.<>c.b__6_2() in E:\JenkinsWorkspaces\v3-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\AWSCredentials.cs:line 2101 at Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(Boolean fallbackToAnonymous) in E:\JenkinsWorkspaces\v3-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\AWSCredentials.cs
Exception 4 of 5: System.InvalidOperationException: The environment variables AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY/AWS_SESSION_TOKEN were not set with AWS credentials. at Amazon.Runtime.EnvironmentVariablesAWSCredentials.FetchAWSCredentials() in E:\JenkinsWorkspaces\v3-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\AWSCredentials.cs:line 856 at Amazon.Runtime.EnvironmentVariablesAWSCredentials..ctor() in E:\JenkinsWorkspaces\v3-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\AWSCredentials.cs:line 836 at Amazon.Runtime.FallbackCredentialsFactory.<>c.b__6_3() in E:\JenkinsWorkspaces\v3-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\AWSCredentials.cs:line 2102 at Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(Boolean fallbackToAnonymous) in E:\JenkinsWorkspaces\v3-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\AWSCredentials.cs
Exception 5 of 5: Amazon.Runtime.AmazonServiceException: Unable to reach credentials server at Amazon.Runtime.URIBasedRefreshingCredentialHelper.GetContents(Uri uri) in E:\JenkinsWorkspaces\v3-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\AWSCredentials.cs:line 1194 at Amazon.Runtime.InstanceProfileAWSCredentials.d__10.MoveNext() in E:\JenkinsWorkspaces\v3-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\AWSCredentials.cs:line at Amazon.Runtime.InstanceProfileAWSCredentials.GetFirstRole() in E:\JenkinsWorkspaces\v3-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\AWSCredentials.cs:line 1422 at Amazon.Runtime.FallbackCredentialsFactory.ECSEC2CredentialsWrapper() in E:\JenkinsWorkspaces\v3-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\AWSCredentials.cs:line 2129 at Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(Boolean fallbackToAnonymous) in E:\JenkinsWorkspaces\v3-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\AWSCredentials.cs
at Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(Boolean fallbackToAnonymous) in E:\JenkinsWorkspaces\v3-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\AWSCredentials.cs at Amazon.Runtime.FallbackCredentialsFactory.GetCredentials() in E:\JenkinsWorkspaces\v3-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\AWSCredentials.cs:line 2136 at Amazon.S3.AmazonS3Client..ctor(RegionEndpoint region) in E:\JenkinsWorkspaces\v3-stage-release\AWSDotNetPublic\sdk\src\Services\S3\Generated_bcl45\AmazonS3Client.cs:line 80 at Utility.Program.Dowork() in C:\repos\poc\Utility\Utility\Program.cs:line 133 at Utility.Program.Main(String[] args) in C:\repos\poc\Utility\Utility\Program.cs:line 23
最佳答案
链接中为较旧的 SDK 指定的行为仍然有效。如果您为 AmazonS3Client
配置访问 key 和 key 的 null
值,则客户端将以匿名模式运行。
这在 SDK 的 .NET 文档中没有记录,但在 S3 Developer Guide's .NET section 中有记录。 .
Note
You can create the
AmazonS3Client
client without providing your security credentials. Requests sent using this client are anonymous requests, without a signature. Amazon S3 returns an error if you send anonymous requests for a resource that is not publicly available.
您可能想要使用像takes a key, secret, and region这样的构造函数。 .
// passing null for the key and secret will make the client generate anonymous requests
var client = new AmazonS3Client(null, null, RegionEndpoint.USWest2);
// do things with the client
关于amazon-web-services - 如何在没有访问 key 和 key 的情况下将文件上传到AWS S3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41247132/
我是一名优秀的程序员,十分优秀!