我正在使用亚马逊的AWS .NET SDK连接到亚马逊的S3。
PutObjectRequest的WithKey()方法会自动编码您向其投掷的任何字符串,但仍有一些模式无法处理。不处理密钥意味着抛出以下错误:
Amazon.S3.AmazonS3Exception: The request signature we calculated
does not match the signature you provided
我发现几乎没有关于亚马逊法律密钥的文档。在S3键中使用哪些模式是非法的并抛出此异常?
答案 0 :(得分:3)
我在上传到
时创建了一个规范化键中斜杠的方法private static string NormalizeKey(string relativePath)
{
return relativePath.Replace("~/", "").Replace(@"~\", "").Replace(@"\", @"/").Replace(@"//", @"/");
}
问候。
答案 1 :(得分:1)
在我的特定情况下,问题是双重的:
我已经编写了以下两种方法来帮助构建我的密钥:
// Cleans a piece of a key - a folder name or final object name:
// - replaces illegal characters with valid ones
// - avoids accidental folder creation by removing slashes inside the key
private string CleanPartialKey(string partialKey)
{
return partialKey.Replace('/', '-') // Add slashes separately - avoid creating accidental folders
.Replace('\\', '_'); // Amazon knows not how to deal with backslashes, so replace them with something else
}
// Ensures a full key does not have any illegal patterns.
// This should only be called with a complete key
private string CleanKey(string fullKey)
{
return fullKey.Replace("./", "/"); // ending a folder with a period is illegal
}