Check if File Exists on Amazon s3 Signed URL
When using CURLOPT_NOBODY, libcurl sends an HTTP HEAD
request, not a GET
request.
...the string to be signed is formed by appending the REST verb, content-md5 value, content-type value, expires parameter value, canonicalized x-amz headers (see recipe below), and the resource; all separated by newlines.
— http://s3.amazonaws.com/doc/s3-developer-guide/RESTAuthentication.html
The "REST verb" -- e.g., GET
vs HEAD
-- must be consistent between the signature you generate, and the request that make, so a signature that is valid for GET
will not be valid for HEAD
and vice versa.
You will need to sign a HEAD
request instead of a GET
request in order to validate a file in this way.
You can check by the header part.
$full_url = 'https://www.example.com/image.jpg';$file_headers = @get_headers($full_url);if($file_headers && strpos($file_headers[0], '200 OK')){ // enter code here}
Or If you are using AWS S3 then you can also use this one.
if(!class_exists('S3')){ require('../includes/s3/S3.php');}S3::setAuth(awsAccessKey, awsSecretKey);$info = S3::getObjectInfo($bucketName, $s3_furl);// check for $info value and apply your condition.