最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - S3 Uploading Excel File and Downloading it via SignedURL - Stack Overflow

programmeradmin2浏览0评论

I'm trying to upload an excel file into S3 and download it via a signedURL. What I've noticed is that the object es back in a different file-type instead of the expected xlsx type, and thus is unreadable locally.

I have two lambdas, one for uploading the object and another for retrieving the 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. Maybe I need an additional parameter or package to explicitly save it as an Excel file. Please let me know your thoughts!

I'm trying to upload an excel file into S3 and download it via a signedURL. What I've noticed is that the object es back in a different file-type instead of the expected xlsx type, and thus is unreadable locally.

I have two lambdas, one for uploading the object and another for retrieving the 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. Maybe I need an additional parameter or package to explicitly save it as an Excel file. Please let me know your thoughts!

Share Improve this question asked Feb 6, 2020 at 19:20 Cat_EnthusiastCat_Enthusiast 15.7k5 gold badges25 silver badges46 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

You are missing the ContentType in params. Im not sure if this one is the right one for excel but providing the correct content type should address the issue. I had a similar issue when uploading images. I forgot to set the ContentType to 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",
发布评论

评论列表(0)

  1. 暂无评论