Demiro-FE-Architect
Demiro-FE-Architect

Reputation: 3740

node: AWS s3 - How to see if a specific object exists (with IfModifiedSince)?

So, what I am doing in node is uploading an image to a tmp Bucket on S3 and then the lambda is resizing the image and saving it to a specific location (different bucket)..

now, what I would like is to get a feedback that lambda is done with manipulation.... normally I would know that just by checking if the resource is there yet, with: waitFor: objectExists

var params = {
  Bucket: 'STRING_VALUE', /* required */
  Key: 'STRING_VALUE', /* required */
};
s3.waitFor('objectExists', params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});

All good and dandy... works the first time... but if the resources are already there, it does not work as intended.... so I needed to check it if the new version is there.... the amazon's docs says there is an option called: IfModifiedSince ... which takes a date...

so if I do something like this:

const date = new Date();
const minute = date.getMinutes();
date.setMinutes(minute - 1); // it's just for illustration purposes, not last code ;)

var params = {
  Bucket: PLAYESR_BUCKET,
  Key: file,
  IfModifiedSince: date,
};

s3.waitFor('objectExists', params, function(err, data) {

...

I get an error:

ERROR NotFound: Resource is not in the state objectExists

{
  message: 'Resource is not in the state objectExists',
  code: 'ResourceNotReady',
  region: null,
  time: 2018-08-07T09:16:44.441Z,
  requestId: '6AFB651A109CD28F',
  extendedRequestId: 'xxx',
  cfId: undefined,
  retryable: true,
  statusCode: 404,
  retryDelay: 5000
}

Again, first time I do it, it works perfectly. As soon as the file exists, I get this error

Upvotes: 1

Views: 1055

Answers (1)

Demiro-FE-Architect
Demiro-FE-Architect

Reputation: 3740

As Michael suggested I am not checking if the specific object exists (as he mentioned it might get an override flag)... I am creating a ${timestamp}.json every time I create/override and check the existence of this one instead.... it might not be the most elegant but get's the job done

Upvotes: 1

Related Questions