Vik*_*ram 0 c# amazon-s3 amazon-web-services
我是 Amazon AWS 的新手,尝试使用 .NET 的 SDK 使用如下所示的控制台应用程序将对象(在本例中为图像)放入(上传)到存储桶:
namespace AwsConsoleApp1
{
class Program
{
static string bucketName = "bucketName";
static string keyName = "keyName";
static string filePath = "filePath";
static IAmazonS3 client;
public static void Main(string[] args)
{
NameValueCollection appConfig = ConfigurationManager.AppSettings;
string accessKeyID = appConfig["AWSAccessKey"];
string secretAccessKeyID = appConfig["AWSSecretKey"];
try
{
using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKeyID, secretAccessKeyID, Amazon.RegionEndpoint.EUWest1))
{
Console.WriteLine("Uploading an object");
WritingAnObject();
}
}
catch (AmazonS3Exception s3Exception)
{
Console.WriteLine(s3Exception.Message, s3Exception.InnerException);
}
catch (AmazonSecurityTokenServiceException stsException)
{
Console.WriteLine(stsException.Message, stsException.InnerException);
}
}
/// <summary>
/// Put object to AWS bucket
/// </summary>
static void WritingAnObject()
{
try
{
PutObjectRequest putRequest1 = new PutObjectRequest
{
BucketName = bucketName,
Key = keyName,
FilePath = filePath
};
PutObjectResponse response1 = client.PutObject(putRequest1);
}
catch (AmazonS3Exception amazonS3Exception)
{
if (amazonS3Exception.ErrorCode != null &&
(amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId")
||
amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
{
Console.WriteLine("Check the provided AWS Credentials.");
Console.WriteLine(
"For service sign up go to http://aws.amazon.com/s3");
}
else
{
Console.WriteLine(
"Error occurred. Message:'{0}' when writing an object"
, amazonS3Exception.Message);
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
我们计算的请求签名与您提供的签名不匹配。请检查您的密钥和登录方法。
扩展此答案,说明自 AWSSDK.Core 版本 3.7.107 以来,S3 存储桶和文件路径的处理发生了新的重大更改。
以前,您可以将路径添加到存储桶名称后缀,并将文件名定义为路径的文件名部分。在上述版本之后,它不再起作用,并且会给你一个神秘的错误消息:
The request signature we calculated does not match the signature you provided. Check your key and signin method.
Run Code Online (Sandbox Code Playgroud)
您现在需要将文件路径作为前缀移动到文件名上。
更多信息可在此处获取:https://github.com/aws/aws-sdk-net/issues/2622
GetObjectRequest request = new GetObjectRequest
{
BucketName = "my-bucket/Folder", // No longer works
Key = "fileName"
};
GetObjectRequest request = new GetObjectRequest
{
BucketName = "my-bucket",
Key = "Folder/fileName" // Works
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5212 次 |
| 最近记录: |