簡體   English   中英

使用 AWS-SDK Nodejs 從 S3 下載圖像會下載損壞的圖像

[英]Downloading images from S3 with AWS-SDK Nodejs downloads a corrupt image

我正在嘗試使用適用於 nodejs 的 AWS-SDK 從 aws s3 下載圖像。 該文件確實被下載並且大小也正確。 但是,文件已損壞並在 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");
        });
    }

這是我遵循的鏈接 - https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/requests-using-stream-objects.html該文件應該可以毫無錯誤地下載。 我嘗試在堆棧上搜索並且有一些類似的問題,但是,他們使用 nodejs 將輸出傳送到前端,而這些解決方案對我不起作用。

沒有必要弄得一團糟,做這一切……可以直接通過-

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.");
        });
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM