最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Downloading images from S3 with AWS-SDK Nodejs downloads a corrupt image - Stack Overflow

programmeradmin1浏览0评论

I'm trying to download images from aws s3 using the AWS-SDK for nodejs. The file does get downloaded and the size is also correct. However, the file is corrupted and shows Depression error in IDAT.

async download(accessKeyId, secretAccessKey, region, bucketName, baseImage) {
        console.log("Entered download");
        const s3 = new AWS.S3({region: region});
        const params = {
             Bucket: bucketName,
             Key: `base/${baseImage}`
         };

        const outStream = fs.createWriteStream(this.config.baseFolder + baseImage);
        const awsStream = s3.getObject(params, (uerr, data) => {
            if(uerr) throw uerr;
            console.log(`Base file downloaded successfully!`)
        }).createReadStream().pipe(outStream);

        awsStream.on('end', function() {
            console.log("successfully Downloaded");
        }).on('error', function() {
            console.log("Some error occured while downloading");
        });
    }

Here's the link I followed - .html The file should get downloaded without any error. I tried searching on stack and there are some similar questions, however, they are using nodejs to deliver the output to the frontend and those solutions aren't working for me.

I'm trying to download images from aws s3 using the AWS-SDK for nodejs. The file does get downloaded and the size is also correct. However, the file is corrupted and shows Depression error in IDAT.

async download(accessKeyId, secretAccessKey, region, bucketName, baseImage) {
        console.log("Entered download");
        const s3 = new AWS.S3({region: region});
        const params = {
             Bucket: bucketName,
             Key: `base/${baseImage}`
         };

        const outStream = fs.createWriteStream(this.config.baseFolder + baseImage);
        const awsStream = s3.getObject(params, (uerr, data) => {
            if(uerr) throw uerr;
            console.log(`Base file downloaded successfully!`)
        }).createReadStream().pipe(outStream);

        awsStream.on('end', function() {
            console.log("successfully Downloaded");
        }).on('error', function() {
            console.log("Some error occured while downloading");
        });
    }

Here's the link I followed - https://docs.aws.amazon./sdk-for-javascript/v2/developer-guide/requests-using-stream-objects.html The file should get downloaded without any error. I tried searching on stack and there are some similar questions, however, they are using nodejs to deliver the output to the frontend and those solutions aren't working for me.

Share Improve this question edited Jun 18, 2019 at 6:54 Kartikeya Gokhale asked Jun 18, 2019 at 6:07 Kartikeya GokhaleKartikeya Gokhale 1353 silver badges12 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

It wasn't necessary to make a mess and do all this... It can directly be achieved by -

async download(accessKeyId, secretAccessKey, region, bucketName, baseImage) {
        console.log("Starting Download... ")
        const s3 = new AWS.S3({
            accessKeyId: accessKeyId,
            secretAccessKey: secretAccessKey,
            region: region
        });
        const params = {
            Bucket: bucketName,
            Key: `base/${baseImage}`
         };

        s3.getObject(params, (err, data) => {
            if(err) console.error(err);
            console.log(this.config.baseFolder + baseImage);
            fs.writeFileSync(this.config.baseFolder + baseImage, data.Body);
            console.log("Image Downloaded.");
        });
    }
发布评论

评论列表(0)

  1. 暂无评论