无法使用适用于 .NET 的 AWS 开发工具包获取预签名对象 URL

2024-03-23

我目前正在使用 S3,需要提取具有流式传输超时的 S3 资源,以便客户端在特定时间后无法使用该 URL。

  1. 我已经使用了“使用 AWS SDK for .NET 预签名对象 URL”文档中提供的一些代码。
  2. 该代码将提供一个临时 URL,任何人都可以使用该 URL 下载 S3 资源……但要在特定的时间限制内。
  3. 我还使用了适用于 Visual Studio 的 Amazon S3 Explorer,但它不支持嵌入 AWSKMS 密钥的资源的 URL 生成。
  4. 还尝试删除 S3 文件夹的 KMS 密钥,但这会引发错误。

如果有可能删除 KMS 密钥的链接,您也可以将其包含在您的答案中。

//Code Start

using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using System;

namespace URLDownload
{
    public class Class1
    {
        private const string bucketName = "some-value";
        private const string objectKey = "some-value";
        // Specify your bucket region (an example region is shown).
        private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USEast1;
        private static IAmazonS3 s3Client;

        public static void Main()
        {
            s3Client = new AmazonS3Client(bucketRegion);
            string urlString = GeneratePreSignedURL();
            Console.WriteLine(urlString);
            Console.Read();
        }
        static string GeneratePreSignedURL()
        {
            string urlString = "";
            try
            {
                //ServerSideEncryptionMethod ssem = new ServerSideEncryptionMethod("AWSKMS");
                GetPreSignedUrlRequest request1 = new GetPreSignedUrlRequest
                {
                    BucketName = bucketName,
                    Key = objectKey,
                    Expires = DateTime.Now.AddMinutes(5),
                    Verb = 0,
                    ServerSideEncryptionKeyManagementServiceKeyId = "some-value",
                    ServerSideEncryptionMethod = ServerSideEncryptionMethod.AWSKMS
                };
                urlString = s3Client.GetPreSignedURL(request1);
            }
            catch (AmazonS3Exception e)
            {
                Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
            }
            return urlString;
        }
    }
}

SignatureDoesNotMatch我们计算的请求签名与您提供的签名不匹配。检查您的密钥和签名方法。 AKIA347A6YXQ3XM4JQ7A

这是我在尝试访问生成的 URL 时收到的错误,这可能是因为 AWSKMS 身份验证存在一些问题。


我发现已经过去几年了,但这个问题有答案吗?您的代码片段似乎缺少的一件事是 V4 签名标志设置为 true:

AWSConfigsS3.UseSignatureVersion4 = true;

资料来源:https://aws.amazon.com/blogs/developer/generate-amazon-s3-pre-signed-urls-with-sse-part-1/ https://aws.amazon.com/blogs/developer/generating-amazon-s3-pre-signed-urls-with-sse-part-1/ https://aws.amazon.com/blogs/developer/generate-amazon-s3-pre-signed-urls-with-sse-kms-part-2/ https://aws.amazon.com/blogs/developer/generating-amazon-s3-pre-signed-urls-with-sse-kms-part-2/

您还需要确保您提供x-amz-server-side-encryption and x-amz-server-side-encryption-aws-kms-key-id您的上传请求的标头

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法使用适用于 .NET 的 AWS 开发工具包获取预签名对象 URL 的相关文章

随机推荐