【问题标题】:How do you download object from AWS S3 bucket using AmazonS3EncryptionClientV2 client side encryption?如何使用 AmazonS3EncryptionClientV2 客户端加密从 AWS S3 存储桶下载对象?
【发布时间】:2021-01-07 16:23:28
【问题描述】:

我们在代码中使用 AmazonS3EncryptionClient 与使用客户端加密的 S3 存储桶进行交互。但是在今天更新 nuget 包时,我注意到 AmazonS3EncryptionClient 已被标记为过时。如果我们想获得持续更新,看起来我们将需要使用 AmazonS3EncryptionClientV2。我在尝试从 AmazonS3EncryptionClient 迁移到 AmazonS3EncryptionClientV2 时遇到了这个问题。

在我们的旧代码中,我们使用了将 RegionEnpoint 作为参数的 AmazonS3EncryptionClient 构造函数。见下图。看起来采用 RegionEnpoint 的构造函数已在 AmazonS3EncryptionClientV2 中删除。

用于从 S3 存储桶获取对象的旧代码。

S3BucketConfiguration _s3BucketConfiguration = provider
   .GetService<IOptionsSnapshot<S3BucketConfiguration>>()
   .Value;

var credential = new BasicAWSCredentials(
    _s3BucketConfiguration.AccessKey, _s3BucketConfiguration.SecurityKey);

RegionEndpoint bucketRegion =
    RegionEndpoint.GetBySystemName(_s3BucketConfiguration.Region);

EncryptionMaterials encryptionMaterials = new EncryptionMaterials(_s3BucketConfiguration.KMSKeyId);

var client = new AmazonS3EncryptionClient(credential, bucketRegion, encryptionMaterials);

GetObjectResponse response = await _client.GetObjectAsync(new GetObjectRequest
{
    BucketName = _s3BucketConfig.BucketName,
    Key = filePath
});

我无法在 AmazonS3EncryptionClientV2 中传入 RegionEnpoint。

到目前为止我的代码。

S3BucketConfiguration _s3BucketConfiguration = provider
   .GetService<IOptionsSnapshot<S3BucketConfiguration>>()
   .Value;

var credential = new BasicAWSCredentials(
    _s3BucketConfiguration.AccessKey, _s3BucketConfiguration.SecurityKey);

RegionEndpoint bucketRegion =
    RegionEndpoint.GetBySystemName(_s3BucketConfiguration.Region);

var encryptionMaterials = new EncryptionMaterialsV2(
    _s3BucketConfiguration.KMSKeyId, 
    KmsType.KmsContext, 
    new Dictionary<string, string>()
);

var config = new AmazonS3CryptoConfigurationV2(SecurityProfile.V2AndLegacy);

//If I add this line it will instantiate AmazonS3EncryptionClientV2 but, the GetObject call fails.
//If I do not add this line, it will give me same error while instiantiating AmazonS3EncryptionClientV2
//config.RegionEndpoint = bucketRegion; 

vr client = new AmazonS3EncryptionClientV2(credential, config, encryptionMaterials);

GetObjectResponse response = client.GetObjectAsync(new GetObjectRequest
{
    BucketName = _s3BucketConfig.BucketName,
    Key = filePath,
}).GetAwaiter().GetResult();

例外

No RegionEndpoint or ServiceURL configured

【问题讨论】:

    标签: c# amazon-web-services amazon-s3 aws-sdk


    【解决方案1】:

    我可以通过RegionEndpoint成功地使用V1加密并使用V2客户端解密。

    var configuration = new AmazonS3CryptoConfiguration()
    {
        RegionEndpoint = RegionEndpoint.USWest2
    };
    var material = new EncryptionMaterials(KmsKeyId);
    var client = new AmazonS3EncryptionClient(configuration, material);
    
    var putObjectResponse = await client.PutObjectAsync(new PutObjectRequest()
    {
        ContentBody = ContentBody,
        BucketName = Bucket,
        Key = Key
    });
    
    if (putObjectResponse.HttpStatusCode == System.Net.HttpStatusCode.OK)
    {
        var configurationV2 = new AmazonS3CryptoConfigurationV2(SecurityProfile.V2AndLegacy)
        {
            RegionEndpoint = RegionEndpoint.USWest2
        };
        var materialV2 = new EncryptionMaterialsV2(KmsKeyId, KmsType.KmsContext, new Dictionary<string, string>());
        var clientV2 = new AmazonS3EncryptionClientV2(configurationV2, materialV2);
    
        var getObjectResponse = await clientV2.GetObjectAsync(new GetObjectRequest()
        {
            BucketName = Bucket,
            Key = Key
        });
    
        using (var reader = new StreamReader(getObjectResponse.ResponseStream))
        {
            Console.WriteLine(reader.ReadToEnd());
        }
    }
    

    能否确保在加密和解密过程中使用相同的RegionEndpoint

    【讨论】:

    • 感谢您的回复。我尝试了您的代码,但是我为 AmazonS3EncryptionClientV2 使用了另一个使用凭证的构造函数。它不起作用。得到同样的错误。 imgur.com/a/GUGtV31
    • 另外,如何在不传递凭据的情况下使用 AmazonS3EncryptionClientV2?我的意思是 sdk 怎么知道从哪里获取访问密钥和秘密?
    • 如果凭证文件中有stored credentials,SDK会默认自动获取。不过,这个问题似乎与凭证无关。您是否能够保存和检索新对象?类似于我在提供的代码 sn-p 中所做的。
    • 对不起,我这几天很忙。无法对此进行调查。当我使用您的代码时,我遇到了同样的错误,但是在调用 PutObjectAsync 方法时。我觉得这可能与存储的凭据有关。我尝试使用存储的凭据,它适用于 AmazonS3Client(没有客户端加密),但是当切换到客户端时出现此错误。
    猜你喜欢
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 2017-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-10
    • 1970-01-01
    相关资源
    最近更新 更多