[英]Corrupted file from aws s3
The file I get from the backend says it's corrupted (pdf) or shows nothing (image/png).我从后端获得的文件说它已损坏(pdf)或什么也没显示(图像/png)。
This is how I upload to AWS:这就是我上传到 AWS 的方式:
const s3 = new AWS.S3();
try {
await s3.upload({
Bucket: bucket,
Key: filename,
Body: file,
ContentType: mimetype,
ContentDisposition: contentDisposition,
}).promise();
return { success: true, data: null };
} catch(e) {
return { success: false, data: null, message: e.code };
}
This is how I get the object from AWS这就是我从 AWS 获得 object 的方式
try {
const data = await s3.getObject({Bucket: bucket, Key: idAWSFile}).promise()
return { success: true, data }
} catch(e) {
return { success: false, data: null, message: e.code };
}
This is the object I get in the client side.这是我在客户端得到的 object。
{
AcceptRanges: "bytes",
Body: <Buffer>,
ContentDisposition: "attachment; filename = "blablabla"",
ContentLength: 23361,
ContentType: "application/pdf",
ETag: <randomnumbers>,
LastModified: <DATE>,
Metadata: {},
}
This is how I'm trying to download it in the client side:这就是我尝试在客户端下载它的方式:
const blob = new Blob([data.Body], { type: data.ContentType });
const url = URL.createObjectURL(blob)
window.open(url)
What have I tried doing:我试过做什么:
file-saver
and same result -> corrupted file我尝试使用file-saver
和相同的结果 - >损坏的文件toString(utf-8)
on the Body in the s3.getObject
function as I saw in other SO answers.正如我在其他 SO 答案中看到的那样,我尝试在s3.getObject
function 的 Body 上使用toString(utf-8)
。 But when I do this I get gibberish in the Body on the client side and don't know and haven't found what to do with it.但是当我这样做时,我会在客户端的 Body 中出现乱码,不知道也没有找到如何处理它。This change in the way I was getting the blob
, in the client side, solved it.我在客户端获取blob
的方式的这种变化解决了它。
const blob = new Blob([Buffer.from(data.Body, 'binary')], {type: data.ContentType})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.