How to get file URL from media library

If you check the documentation for media_sideload_image() it returns HTML img tag for the image right away. Alternately it can return just the URL.

Note that while this is suitable for immediate use, it’s generally challenging to go back from URL to attachment ID. IDs are much more friendly for many things, such as being stored, processed, used to access image sizes, and so on.

If there are intermediary steps in your process you might want to use deeper level media_handle_sideload(), which returns ID for the attachment rather than processed HTML.