0
votes

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)
        })
    }
1
Any chance that your getObject call is happening before your upload 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
@jarmod, yes, i realised there was a chance my 'GetObject' could be called before my upload call completes. That solves the problemmulty18

1 Answers

0
votes

The number one reason that S3 GetObject fails after an upload is that the GetObject request actually happened before the upload completed, This is easy to do in async JavaScript.