javascript - upload file to s3 using nodejs - Stack Overflow

I have coded a function which upload file to s3 which using aws-sdk, but I've got a problem.When I

I have coded a function which upload file to s3 which using aws-sdk, but I've got a problem.

When I replace Body by Body: "test" then the function will execute successfully and I can see the content test in s3.

But I want to upload a document such as pdf file, it can't run and throws an error as:

UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be one of type string, Buffer, or URL. Received type object at Object.open (fs.js:406:3) at ReadStream.open (internal/fs/streams.js:110:12) at new ReadStream (internal/fs/streams.js:99:10) at Object.createReadStream (fs.js:1725:10)

This is my code:

import { createWriteStream, createReadStream } from "fs";
import AWS from 'aws-sdk';
const fs = require('fs');


const uploadS3 = async ({file}) => {
  const { stream, filename} = file;
  AWS.config.update({
    accessKeyId: '*******',
    secretAccessKey: '********',
    region: 'us-east-1' 
  });

  let params = {
    Bucket: "test-files-staging",
    Key: 'documents/' + filename,
    Body: fs.createReadStream(file),
    ACL: 'public-read'
  };
  await new AWS.S3().putObject(params).promise().then(() => {
    console.log('Success!!!')
  }).catch((err) => { console.log(`Error: ${err}`) })
}

Thanks in advance.

I have coded a function which upload file to s3 which using aws-sdk, but I've got a problem.

When I replace Body by Body: "test" then the function will execute successfully and I can see the content test in s3.

But I want to upload a document such as pdf file, it can't run and throws an error as:

UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be one of type string, Buffer, or URL. Received type object at Object.open (fs.js:406:3) at ReadStream.open (internal/fs/streams.js:110:12) at new ReadStream (internal/fs/streams.js:99:10) at Object.createReadStream (fs.js:1725:10)

This is my code:

import { createWriteStream, createReadStream } from "fs";
import AWS from 'aws-sdk';
const fs = require('fs');


const uploadS3 = async ({file}) => {
  const { stream, filename} = file;
  AWS.config.update({
    accessKeyId: '*******',
    secretAccessKey: '********',
    region: 'us-east-1' 
  });

  let params = {
    Bucket: "test-files-staging",
    Key: 'documents/' + filename,
    Body: fs.createReadStream(file),
    ACL: 'public-read'
  };
  await new AWS.S3().putObject(params).promise().then(() => {
    console.log('Success!!!')
  }).catch((err) => { console.log(`Error: ${err}`) })
}

Thanks in advance.

Share Improve this question edited Feb 10, 2020 at 13:27 Zeeshan Hassan Memon 8,3354 gold badges46 silver badges59 bronze badges asked Aug 6, 2019 at 7:29 Khanh PhamKhanh Pham 2,9732 gold badges31 silver badges37 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Well the error is pretty clear: the path argument needs to be string, buffer or URL, but you pass in an object which is the readStream created by fs.createReadStream.

Instead, use the fs.readFileSync function to read your file into a buffer (if you use the async fs.readFile you'll get a promise- which is again neither a string, buffer or URL and will fail again):

let params = {
    Bucket: "test-files-staging",
    Key: 'documents/' + filename,
    Body: fs.readFileSync(filename), // assuming filename is the full path to your object
    ACL: 'public-read'
  };

more info on fs functions can be found in documentation- here

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745437881a4627692.html

相关推荐

  • javascript - upload file to s3 using nodejs - Stack Overflow

    I have coded a function which upload file to s3 which using aws-sdk, but I've got a problem.When I

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信