当我运行以下程序时,会发生无法找到凭据的异常
using (IAmazonS3 s3Client = new AmazonS3Client(RegionEndpoint.USWest2))
{
PutObjectRequest request = new PutObjectRequest
{
BucketName = bucketName,
Key = ps1,
FilePath = path
};
PutObjectResponse response = s3Client.PutObject(request);
richTextBox1.Text = response.ToString();
}
此外,我已经像这样配置了app.config:
<configuration>
<appSettings>
<add key="AWSProfileName" value="ashu"/>
<add key="AWSProfilesLocation" value="C:\service_credential\credentials"/>
<add key="AWSRegion" value="us-west-2" />
</appSettings>
</configuration>
答案 0 :(得分:1)
您不需要使用new
来实例化客户端,执行此操作的最佳方法是使用AWSClientFactory
和StoredProfileAWSCredentials
来创建客户端。
SoredProfile方法,您必须确定个人资料名称以及是否存在。无论如何看到这个例子,告诉你如何弄清楚:https://blogs.aws.amazon.com/net/post/Tx1310VG2O81PSY/Referencing-Credentials-using-Profiles
var credentials = new StoredProfileAWSCredentials("ashu");
EnvirementCredential Approach ,您必须将
AWSAccessKey
和AWSSecretKey
放入App.config / Web.config中,并且必须确保这一点和地区是正确的。
<appSettings>
<add key="AWSAccessKey" value="Access_KEY" />
<add key="AWSSecretKey" value="Secret_KEY" />
</appSettings>
var credentials = new EnvironmentAWSCredentials();
using (IAmazonS3 s3Client = AWSClientFactory.CreateAmazonS3Client(credentials, RegionEndpoint.USWest2))
{
PutObjectRequest request = new PutObjectRequest
{
BucketName = bucketName,
Key = ps1,
FilePath = path
};
PutObjectResponse response = s3Client.PutObject(request);
richTextBox1.Text = response.ToString();
}