Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix fileName - undefined and fileType not present in s3 upload objects #5680

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions examples/aws-nodejs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,27 @@ function getSTSClient() {
return stsClient
}

// Generate a unique S3 key for the file
const generateS3Key = (filename) => `${crypto.randomUUID()}-${filename}`

// Extract the file parameters from the request
const extractFileParameters = (req) => {
const isPostRequest = req.method === 'POST'
const params = isPostRequest ? req.body : req.query

return {
filename: params.filename,
contentType: params.type
}
}

// Validate the file parameters
const validateFileParameters = (filename, contentType) => {
if (!filename || !contentType) {
throw new Error('Missing required parameters: filename and content type are required')
}
}

app.use(bodyParser.urlencoded({ extended: true }), bodyParser.json())

app.get('/s3/sts', (req, res, next) => {
Expand Down Expand Up @@ -109,8 +130,11 @@ const signOnServer = (req, res, next) => {
// are authorized to perform that operation, and if the request is legit.
// For the sake of simplification, we skip that check in this example.

const Key = `${crypto.randomUUID()}-${req.body.filename}`
const { contentType } = req.body
const { filename, contentType } = extractFileParameters(req)
validateFileParameters(filename, contentType)

// Generate S3 key and prepare command
const Key = generateS3Key(filename)

getSignedUrl(
getS3Client(),
Expand Down