c# - 无法使用适用于 .NET 的 AWS 开发工具包获取预签名对象 URL
问题描述
我目前正在使用 S3,需要提取一个流式传输超时的 S3 资源,以便客户端在特定时间后无法使用该 URL。
- 我已经使用了“Presigned Object URL Using AWS SDK for .NET”文档中提供的一些代码。
- 该代码将提供一个临时 URL,任何人都可以使用该 URL 下载 S3 资源……但在特定的时间限制内。
- 我还使用了适用于 Visual Studio 的 Amazon S3 Explorer,但它不支持为嵌入 AWSKMS 密钥的资源生成 URL。
- 还尝试删除 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/generating-amazon-s3-pre-signed-urls-with-sse-part-1/ https://aws.amazon.com/blogs/developer/生成-amazon-s3-pre-signed-urls-with-sse-kms-part-2/
您还需要确保在上传请求中提供标x-amz-server-side-encryption
头x-amz-server-side-encryption-aws-kms-key-id
推荐阅读
- html - 如何将一个网站嵌入另一个网站?
- mongodb - 如何在 MongoDB 中更新具有两个日期差异的列?
- c++ - 使用带有光电传感器和“切换器”的步进电机
- python - 在最接近点的线上绘制一个点
- assembly - 将空格打印到 8086 程序集中的每个输入
- database - MongoDB - 仅获取已填充文档的数组中的第一项 - 例如 user.populate('posts', 'images.0')
- json - 从雪花中的嵌套变体/JSON 列中提取数据
- javascript - Angular 子组件路由失败?
- clojure - 最接近 clojure 的 core.async 的通用 lisp libset
- laravel - laravel 父 * 和 * 子模型中的属性总和?