AWS.NET SDK非法密钥

本文关键字:密钥 非法 SDK NET AWS | 更新日期: 2023-09-27 18:22:11

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

PutObjectRequest的WithKey()方法会自动对您向其抛出的任何字符串进行编码,但仍有一些模式无法处理。不处理密钥意味着抛出以下错误:

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

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

AWS.NET SDK非法密钥

我创建了一个方法,在上传到时规范键中的斜杠

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

谨致问候。

在我的特殊情况下,问题有两个方面:

  1. Amazon无法处理密钥中的反斜杠"''"字符
  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
}