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.getSignedUrl('getObject', params, callback);
It works but the problem is that when the object doesn't exists, the callback (with arguments err and url) returns no error, and when I try to access the URL, it says "NoSuchObject".
Shouldn't this getSignedUrl
method return an error object when the object doesn't exists? How do I determine if the object exists? Do I really need to make a call on the returned URL?
You can also use the
waitFor
method together with the stateobjectExists
. This will useS3.headObject()
internally.Use
getObject
method like this:Before creating the signed URL, you need to check if the file exists directly from the bucket. One way to do that is by requesting the HEAD metadata.
by using
headObject
methodAs params are constant, the best way to use it with
const
. If the file is not found in the s3 it throws the errorNotFound : null
.If you want to apply any operations in the bucket, you have to change the permissions of
CORS Configuration
in the respective bucket in the AWS. For changing permissionsBucket->permission->CORS Configuration
and Add this code.for more information about CORS Configuration : https://docs.aws.amazon.com/AmazonS3/latest/dev/cors.html