首页 > 解决方案 > AWS:无法使用流从 SSE-KMS 加密存储桶下载文件

问题描述

我有一个配置了启用加密的存储桶:

我有使用流在 S3 存储桶上上传/下载文件的 nodejs 项目。问题是我可以成功上传,但是当我尝试使用流下载/获取文件时,它只发出一次“数据”事件,然后发出“结束”事件,但仍有数据可以消耗从下面的日志中看到。你知道为什么会这样吗?

附加信息:

下载流:

    async downloadStream() {
        logger.info('Getting download stream...');
        const url = await this.getFileUrl(); // getSignedUrl
    
        const headers = {};
  
        this.downloadStream = request({ url, headers });
        this.downloadStream.on('data', (chunk) => this.emit('data', chunk.length));
        this.downloadStream.on('end', () => this.emit('end'));
        this.downloadStream.on('error', (error = {}) => this.emit('failure', error));
    
        logger.info('Acquired download stream successfully');
    
        this.downloadStream.pipe(this.response);;
    }

记录信息:

完整传输统计:
传输的字节数:13170 字节中的 4491 个剩余字节
:8679
平均块大小:4491 字节
估计剩余块:2
平均速度:0.000260375 MB/s 总传输时间:13.127 秒

标签: node.jsamazon-s3encryptionnodejs-streamamazon-kms

解决方案


在服务器端使用 KMS 加密的对象在访问它们时需要使用 SigV4 协议。 来源:aws论坛

创建 AWS.S3 客户端时,在选项中添加 signatureVersion: 'v4':

  return new aws.S3({
        credentials,
        params: {
            bucket,
        },
        region,
        signatureVersion: 'v4',
    });

这解决了我的问题。


推荐阅读