I uploaded a file to my s3 bucket and tried to read the file immediately after upload. I most of the time get "err NoSuchKey: The specified key does not exist". i check the bucket using the console and the file actually exist.
After refreshing the page, the file is able to be read. Aws region is US East (N Virginia). File is uploaded with a private read.
export function uploadFile(absolutePath: string, fileBuffer: Buffer, callback: (err, result) => void) {
try {
let uploadParams: awsSdk.S3.PutObjectRequest = {
Bucket: cfg.aws[process.env.NODE_ENV].bucket,
Key: absolutePath,
Body: fileBuffer,
ACL: 'private',
CacheControl: 'public, max-age=2628000'
}
s3.upload(uploadParams, function (err, result) {
if (err) {
Util.logError('Aws Upload File', err)
}
return callback(err, result)
})
} catch (err) {
Util.logError('Aws Upload File', err)
return callback(err, null)
}
}
export function obtainObjectOutput(absolutePath: string, callback: (err, result: awsSdk.S3.GetObjectOutput) => void) {
let getParaams: awsSdk.S3.GetObjectRequest = {
Bucket: cfg.aws[process.env.NODE_ENV].bucket,
Key: absolutePath
}
s3.getObject(getParaams, (error, result) => {
(error) ? callback(error, null) : callback(null, result)
})
}
getObject
call is happening before yourupload
call completes? PS you should consider making these functions async, invoking the.promise()
variants of the SDK calls, and returning those promises for the client to await fulfillment. – jarmod