简体   繁体   English

S3 上传 Excel 文件并通过 SignedURL 下载

[英]S3 Uploading Excel File and Downloading it via SignedURL

I'm trying to upload an excel file into S3 and download it via a signedURL.我正在尝试将 excel 文件上传到 S3 并通过 signedURL 下载它。 What I've noticed is that the object comes back in a different file-type instead of the expected xlsx type, and thus is unreadable locally.我注意到该对象以不同的文件类型而不是预期的 xlsx 类型返回,因此在本地不可读。

I have two lambdas, one for uploading the object and another for retrieving the signedURL.我有两个 lambda,一个用于上传对象,另一个用于检索 signedURL。

Upload:上传:

async function () => {
  const s3 = new aws.S3({ signatureVersion: 'v4' })
  const params = {
    Bucket: bucket,
    Key: key
  }

  try {
    const signedURL = await s3.getSignedUrl('getObject', params)
    return response(200, signedURL)
  } catch (err) {
    console.log(JSON.stringify(err))
    return response(400, err)
  }
}

GetSignedURL:获取签名网址:

async function () => {
  const s3 = new aws.S3({ signatureVersion: 'v4' })
  const params = {
     Bucket: bucket,
     Key: key
  }

  try {
     const signedURL = await s3.getSignedUrl('putObject', params)
     return response(200, { signedURL, key })
  } catch (err) {
    return response(400, err)
  }
}

I'm guessing that the file doesn't actually get saved with its original file-type and S3 actually just converts it to text-file.我猜测该文件实际上并未以其原始文件类型保存,而 S3 实际上只是将其转换为文本文件。 Maybe I need an additional parameter or package to explicitly save it as an Excel file.也许我需要一个额外的参数或包来明确地将它保存为 Excel 文件。 Please let me know your thoughts!请让我知道你的想法!

You are missing the ContentType in params.您在 params 中缺少 ContentType。 Im not sure if this one is the right one for excel but providing the correct content type should address the issue.我不确定这是否适合 excel,但提供正确的内容类型应该可以解决这个问题。 I had a similar issue when uploading images.上传图片时我遇到了类似的问题。 I forgot to set the ContentType to image/jpeg我忘了将 ContentType 设置为 image/jpeg

const params = {
    Bucket: bucket,
    Key: key,
    ContentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
  }
 Other possible options "xls" => "application/vnd.ms-excel", "xlsx" => "vnd.ms-excel",

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM