簡體   English   中英

來自 aws s3 的損壞文件

[英]Corrupted file from aws s3

我從后端獲得的文件說它已損壞(pdf)或什么也沒顯示(圖像/png)。

這就是我上傳到 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 };
}

這就是我從 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 };
}

這是我在客戶端得到的 object。

{
  AcceptRanges: "bytes",
  Body: <Buffer>,
  ContentDisposition: "attachment; filename = "blablabla"",
  ContentLength: 23361,
  ContentType: "application/pdf",
  ETag: <randomnumbers>,
  LastModified: <DATE>,
  Metadata: {},
}

這就是我嘗試在客戶端下載它的方式:

const blob = new Blob([data.Body], { type: data.ContentType });
const url = URL.createObjectURL(blob)
window.open(url)

我試過做什么:

  1. 我嘗試使用file-saver和相同的結果 - >損壞的文件
  2. 正如我在其他 SO 答案中看到的那樣,我嘗試在s3.getObject function 的 Body 上使用toString(utf-8) 但是當我這樣做時,我會在客戶端的 Body 中出現亂碼,不知道也沒有找到如何處理它。

我在客戶端獲取blob的方式的這種變化解決了它。

const blob = new Blob([Buffer.from(data.Body, 'binary')], {type: data.ContentType})

暫無
暫無

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

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