-
Notifications
You must be signed in to change notification settings - Fork 816
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: s3 upload index.html at last (#12852)
* fix: s3 upload index.html at last * fix: refactor * fix: sort fileList * refactor: create sortUploadFiles * test: unit test sortUploadFiles * test: revert test * test: add test for uploadFileCalls * chore: run prettier-write * chore: clearAllMocks * chore: remove unnecessary async --------- Co-authored-by: MJ☔ <zhamujun@amazon.com>
- Loading branch information
Showing
3 changed files
with
59 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
packages/amplify-category-hosting/lib/S3AndCloudFront/helpers/upload-file.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const fs = require('fs-extra'); | ||
const path = require('path'); | ||
const mime = require('mime-types'); | ||
|
||
|
||
async function uploadFile(s3Client, hostingBucketName, distributionDirPath, filePath, hasCloudFront) { | ||
let relativeFilePath = path.relative(distributionDirPath, filePath); | ||
// make Windows-style relative paths compatible to S3 | ||
relativeFilePath = relativeFilePath.replace(/\\/g, '/'); | ||
const fileStream = fs.createReadStream(filePath); | ||
const contentType = mime.lookup(relativeFilePath); | ||
const uploadParams = { | ||
Bucket: hostingBucketName, | ||
Key: relativeFilePath, | ||
Body: fileStream, | ||
ContentType: contentType || 'text/plain', | ||
}; | ||
|
||
if (!hasCloudFront) { | ||
uploadParams.ACL = 'public-read'; | ||
} | ||
|
||
const data = await s3Client.upload(uploadParams).promise(); | ||
|
||
return data; | ||
} | ||
|
||
module.exports = { | ||
uploadFile | ||
}; | ||
|