How to determine if object exists AWS S3 Node.JS sdk

后端 未结 8 1559
梦毁少年i
梦毁少年i 2020-12-13 03:39

I need to check if a file exists using AWS SDK. Here is what I\'m doing:

var params = {
    Bucket: config.get(\'s3bucket\'),
    Key: path
};

s3.getSignedU         


        
8条回答
  •  抹茶落季
    2020-12-13 03:54

    The simplest solution without try/catch block.

    const exists = await s3
      .headObject({
        Bucket: S3_BUCKET_NAME,
        Key: s3Key,
      })
      .promise()
      .then(
        () => true,
        err => {
          if (err.code === 'NotFound') {
            return false;
          }
          throw err;
        }
      );
    

提交回复
热议问题