Ad

Can't Download Images From Aws S3 Although All Things Is Correct In Node Js

i use aws-sdk for download images from aws s3 code no error but no any image download

const filePath = '../imgs';
const bucketName = 'xxx';
const key =  "uploads/imgs/logo.png";

const AWS = require('aws-sdk');
AWS.config = new AWS.Config();
AWS.config.accessKeyId = "xxxxxx";
AWS.config.secretAccessKey = "xxxxxxxx";
AWS.config.region = "us-east-1";
const fs = require('fs')
var s3 = new AWS.S3();

const s3download = (bucketName, keyName, localDest) => {

    if (typeof localDest == 'undefined') {
        localDest = keyName;
    }

    let params = {
        Bucket: bucketName,
        Key: keyName
    }

    let file = fs.createWriteStream(localDest)

    return new Promise((resolve, reject) => {
        s3.getObject(params).createReadStream()
            .on('end', () => {
                return resolve();
            })
            .on('error', (error) => {
                return reject(error);
            }).pipe(file)
    });
};
s3download(bucketName, key, filePath)

when print result appear this

_readableState: ReadableState {
    objectMode: false,
    highWaterMark: 16384,
    buffer: BufferList { head: null, tail: null, length: 0 },
    length: 0,
    pipes: [],
    flowing: null,
    ended: false,
    endEmitted: false,
    reading: false,
    sync: false,
    needReadable: false,
    emittedReadable: false,
    readableListening: false,
    resumeScheduled: false,
    errorEmitted: false,
    emitClose: true,
    autoDestroy: true,
    destroyed: false,
    errored: false,
    closed: false,
    closeEmitted: false,
    defaultEncoding: 'utf8',
    awaitDrainWriters: null,
    multiAwaitDrain: false,
    readingMore: false,
    decoder: null,
    encoding: null,
    [Symbol(kPaused)]: null
  },
  _events: [Object: null prototype] { prefinish: [Function: prefinish] },
  _eventsCount: 1,
  _maxListeners: undefined,
  _writableState: WritableState {
    objectMode: false,
    highWaterMark: 16384,
    finalCalled: false,
    needDrain: false,
    ending: false,
    ended: false,
    finished: false,
    destroyed: false,
    decodeStrings: true,
    defaultEncoding: 'utf8',
    length: 0,
    writing: false,
    corked: 0,
    sync: true,
    bufferProcessing: false,
    onwrite: [Function: bound onwrite],
    writecb: null,
    writelen: 0,
    afterWriteTickInfo: null,
    buffered: [],
    bufferedIndex: 0,
    allBuffers: true,
    allNoop: true,
    pendingcb: 0,
    prefinished: false,
    errorEmitted: false,
    emitClose: true,
    autoDestroy: true,
    errored: false,
    closed: false,
    closeEmitted: false
  },
  allowHalfOpen: true,
  [Symbol(kCapture)]: false,
  [Symbol(kCallback)]: null
}
Ad

Answer

filePath provides the path to the folder where we want to save the file that we get from S3 but we do not provide a filename here:

let file = fs.createWriteStream(localDest)

localDest should be a full-path + filename and currently it only provides the full path

Ad
source: stackoverflow.com
Ad