gpt4 book ai didi

javascript - 使用 AWS-SDK Nodejs 从 S3 下载图像会下载损坏的图像

转载 作者:行者123 更新时间:2023-12-02 16:55:26 26 4
gpt4 key购买 nike

我正在尝试使用适用于 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.");
});
}

关于javascript - 使用 AWS-SDK Nodejs 从 S3 下载图像会下载损坏的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56642626/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com