当对象不存在时,headObject 从不抛出错误

headObject never throws an error when object doesn't exist

我目前正在尝试使用适用于 Amazon s3 的 aws-sdk(更准确地说,函数 headObject)检查文件是否存在。

正如我在任何地方都能读到的那样,这是在尝试检查文件是否存在时应该使用的函数(以便通过 getSignedUrl 获取它的 URL),但是我可以'不要让它工作。

似乎,无论我做什么,函数s3.headObject 都告诉我该对象存在。我尝试检查现有项目、不存在项目,甚至检查不存在的存储桶:所有这些都得到了完全相同的输出。我尝试了不同的调用函数的方式(异步或不异步,使用或不使用它的回调),但没有区别。

下面是我如何实现对函数的调用:

var params = {
    Bucket: 'BUCKET NAME',
    Key: ""
}

// Some more code to determine file name, confirmed working

params.Key = 'FILE NAME'
try {
    s3.headObject(params)
    // Using here the file that is supposed to exist
} catch (headErr) {
    console.log("An error happened !")
    console.log(headErr)
}

我也尝试过使用回调:但是,似乎从未输入过所说的回调。这是我的代码的样子:

var params = {
    Bucket: 'BUCKET NAME',
    Key: ""
}

// Some more code to determine file name, confirmed working

params.Key = 'FILE NAME'
s3.headObject(params, function(err: any, data: any) {
    console.log("We are in the callback")
    if (err) console.log(err, err.code)
    else {   
    // Do things with file
    }
})
console.log("We are not in the callback")

使用此代码,"We are in the callback" 从未出现,而 "We are not in the callback" 正确出现。

无论我做什么,都不会发现任何错误。 根据我对函数应该如何工作的理解,如果文件不存在,它应该抛出一个错误(然后被我的捕获捕获),这样我就不会创建 false URLs使用 getSignedUrl 函数。

我在这里做错了什么?

谢谢大家的回答。如果您还有其他问题,我将非常乐意尽我所能回答。

这是使用 async/await 语法检查对象是否存在的正确方法:

// Returns a promise that resolves to true/false if object exists/doesn't exist
const objectExists = async (bucket, key) => {
  try {
    await s3.headObject({
      Bucket: bucket,
      Key: key,
    }).promise(); // Note the .promise() here
    return true; // headObject didn't throw, object exists
  } catch (err) {
    if (err.code === 'NotFound') {
      return false; // headObject threw with NotFound, object doesn't exist
    }
    throw err; // Rethrow other errors
  }
};

我确实尝试了 syntex,但它不起作用。它在我的 lambda 函数中。

params 和 params2 是一组预定义的桶和键。

var url = s3.getSignedUrl('getObject', params);
    const objectExist = async (par) => { 
        try{
            console.log(s3.headObject(par).response); //I honestly couldn't find any 
            //section in the resoonse,
            // that make a DNE file different from a existing file.
             const ext = await s3.headObject(par).promise((resolve, reject) =>{
                  console.log("bbbbbbbbbbb");
                if(err) { // it keeps saying the err is not global variable. 
                //I am wondering why this is not defined.
                //Really had no idea of what else I could put as condition.
                    console.log("aaaa"); //never reach here.
                    return reject(false);}
                return resolve(true);
             });
             console.log(ext); //always true. 
             if(!ext){url = url = s3.getSignedUrl('getObject', params2, callback); }
            }catch(err){
                console.log("reeeeeeeeee"); //when the method failed it execute. 
                url = s3.getSignedUrl('getObject', params2, callback);
                console.log(url); //even though I am sure that params2 are valid key, but url in the log always returned undefined.
            }
      
  };
  objectExist(params);