我一直在尝试将文件上传到AWS S3,下面是我正在尝试的代码
private static void UploadToAWS(string localFilePath, string bucketName, string subDirectoryInBucket, string fileNameInS3)
{
string accessKey = ConfigurationManager.AppSettings["AMAZON_S3_ACCESSKEY"].ToString();
string secretKey = ConfigurationManager.AppSettings["AMAZON_S3_SECRETKEY"].ToString();
AmazonS3Config asConfig = new AmazonS3Config()
{
ServiceURL = "http://test.s3.amazonaws.com",
};
IAmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey,secretKey,asConfig);
TransferUtility utility = new TransferUtility(client);
TransferUtilityUploadRequest request = new TransferUtilityUploadRequest();
if (subDirectoryInBucket == "" || subDirectoryInBucket == null)
{
request.BucketName = bucketName; //no subdirectory just bucket name
}
else
{ // subdirectory and bucket name
request.BucketName = bucketName + @"/" + subDirectoryInBucket;
}
request.Key = fileNameInS3; //file name up in S3
request.FilePath = localFilePath; //local file name
request.Headers.CacheControl = "public";
request.Headers.Expires = DateTime.Now.AddYears(3);
request.Headers.ContentEncoding = "gzip";
utility.Upload(request); //commensing the transfer
}
UploadToAWS(@"D:\core_gz.min.js", "test123", "test/build/", "core_gz.min.js");
当我执行此操作时,我收到以下错误
我们计算的请求签名与您的签名不符 提供。检查您的密钥和签名方法。
任何人都可以帮助我,我在这里做错了什么
答案 0 :(得分:0)
我只想发布答案,以防万一有其他人有同样的问题
private static void UploadToAWS(string localFilePath, string bucketName, string subDirectoryInBucket, string fileNameInS3)
{
string accessKey = ConfigurationManager.AppSettings["AMAZON_S3_ACCESSKEY"].ToString();
string secretKey = ConfigurationManager.AppSettings["AMAZON_S3_SECRETKEY"].ToString();
AmazonS3Config asConfig = new AmazonS3Config()
{
ServiceURL = "http://test.s3.amazonaws.com",
RegionEndpoint = Amazon.RegionEndpoint.APSoutheast1 // this line fixed the issue
};
IAmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey,secretKey,asConfig);
TransferUtility utility = new TransferUtility(client);
TransferUtilityUploadRequest request = new TransferUtilityUploadRequest();
if (subDirectoryInBucket == "" || subDirectoryInBucket == null)
{
request.BucketName = bucketName; //no subdirectory just bucket name
}
else
{ // subdirectory and bucket name
request.BucketName = bucketName + @"/" + subDirectoryInBucket;
}
request.Key = fileNameInS3; //file name up in S3
request.FilePath = localFilePath; //local file name
request.Headers.CacheControl = "public";
request.Headers.Expires = DateTime.Now.AddYears(3);
request.Headers.ContentEncoding = "gzip";
utility.Upload(request); //commensing the transfer
}
在配置中添加此行修复了我的问题
RegionEndpoint = Amazon.RegionEndpoint.APSoutheast1