How can I check for existence of an uploaded file

thedelanyo
thedelanyo Member Posts: 3
edited August 23 in Developer APIs

Essentially I am using hash based system and before a file is uploaded, I want to check whether it exists based on the hash - which is set as the filename. I am using NodeJS and I have the following options, use_filename: true, unique_filename: false, filename_override: hashed_name.

Basically, I want to check for the existence of the hashed_name, before I continue with the upload or stop.

Tagged:

Best Answer

  • DannyFromCloudinary
    DannyFromCloudinary Member, Cloudinary Staff Posts: 150
    Answer ✓

    Hey @thedelanyo. Thanks for getting in touch.

    There are actually multiple ways you can get around this problem, including (but not limited to)

    • If your asset is publicly available, you could performing a head request with curl for https://res.cloudinary.com/your_cloud_name/image/upload/hashed_name. If the response comes back with a 200, the file exists
    • Add overwrite: false to the upload request, so if the file exists already, it isn't replaced
    • Upon successfully uploading an asset, store the hashed name in a database. For new uploads, check this DB for the hashed name before proceeding with the upload
    • I wouldn't recommend this because of rate-limits, but you could use the resource() method of the Admin API to check if the file exists

    I hope this helps, but if anything is unclear or if you have any further questions, please let us know.

    Kind regards,
    -Danny

Answers