简体   繁体   中英

Upgrade aws-sdk to version 3 - streaming S3 upload

I am trying to upgrade a program to aws-sdk version 3, but I am getting the error:

NotImplemented: A header you provided implies functionality that is not implemented

The function get_s3_stream needs to return a stream to the main program.

SDK version 2, this works:

var AWS = require("aws-sdk");
var s3 = new AWS.S3({apiVersion: '2006-03-01'});
const stream = require('stream');

function get_s3_stream() {
  var pass = new stream.PassThrough();
  var params = {Bucket: "bucketname", Key: "testfile1.txt", Body: pass};
  s3.upload(params, function(err, data) {
    console.log(err, data);
  });
  return pass;
}

const inputStream = stream.Readable.from(["input string1"])
const outStream = get_s3_stream()
inputStream.pipe(outStream);

SDK version 3, doesn't work:

const { S3Client, PutObjectCommand } = require("@aws-sdk/client-s3");
const s3Client = new S3Client({ region: "us-east-1" });
const stream = require('stream')

function get_s3_stream() {
    const pass = new stream.PassThrough();  
    var params = {Bucket: "bucketname", Key: "testfile2.txt", Body: pass};
    s3Client.send(new PutObjectCommand(params, function(err, data) {
        console.log(err, data);
      }));
    return pass;
};

const inputStream = stream.Readable.from(["input string2"])
const outStream = get_s3_stream()
inputStream.pipe(outStream);

How can a stream be sent to S3 with the new version?

Apparently this is a know issue: https://github.com/aws/aws-sdk-js-v3/issues/1920 .

A workaround in order to use passthrough streams would be to use Upload from @aws-sdk/lib-storage :

const { S3Client } = require("@aws-sdk/client-s3");
const { Upload } = require('@aws-sdk/lib-storage');
const stream = require('stream')

const s3Client = new S3Client({ region: "us-east-1" });

function get_s3_stream() {
    const pass = new stream.PassThrough();
    const upload = new Upload({
        client: s3Client,
        params: {
            Bucket: 'bucketname',
            Key: 'testfile2.txt',
            Body: pass,
            ContentType: 'text/plain',
        },
    });

    upload.done().then((res, error) => {
        console.log(res);
    });

    return pass;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM