2012-02-02 91 views
0

我正在使用亚马逊的AWS .NET SDK连接到亚马逊的S3。AWS .NET SDK非法密钥

PutObjectRequest的WithKey()方法自动编码任何你在其上面扔的字符串,但是仍然有一些它无法处理的模式。不处理密钥意味着抛出以下错误:

Amazon.S3.AmazonS3Exception: The request signature we calculated 
does not match the signature you provided 

我发现几乎没有关于来自亚马逊的合法密钥的文档。在S3键中使用哪些模式是非法的并抛出此异常?

回答

3

,同时上传到

private static string NormalizeKey(string relativePath) 
    { 
      return relativePath.Replace("~/", "").Replace(@"~\", "").Replace(@"\", @"/").Replace(@"//", @"/"); 
    } 

问候我已经创建了正常化键斜线的方法。

+1

谢谢邻省!我有一个类似的方法来避免在构建密钥时意外创建文件夹。什么是用于替换呼叫的波浪线“〜”?没有它似乎为我工作得很好。在我的代码中使用了 – 2012-02-04 13:10:29

+1

〜,因为我已经设置了我的相对路径,就像它们存在于我的本地项目中一样。在你的情况下,这可以避免。 – 2012-02-06 07:20:55

1

在我的具体情况,这个问题是两方面:

  1. 亚马逊无法处理的按键反斜杠“\”字符
  2. 亚马逊不允许文件夹在一段
结束

我写了下面的两种方法构建我的钥匙时的帮助:

// 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 
}