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 badges1 Answer
Reset to default 6You 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",