How do I download a file from a URL, if a username and password are required? For example, in MATLAB, I can do
url = 'https://n5eil01u.ecs.nsidc.org/DP4/ICEBRIDGE/IRMCR2.001/2010.03.24/IRMCR2_20100324_01.csv';
urlread(url,'Username','chadagreene','Password','notactuallymypassword')
and that saves a file called IRMCR2_20100324_01.csv
to my local directory. That’s exactly what I want. Now how do I do it in Julia?
Better to use Downloads which is a standard library. Something like
using Downloads: download
file = download(url)
You can create a ~/.netrc
file with a username and password. You can also put the username and password in the URL.
3 Likes
Please do not post code generated with ChatGTP if you have not tested it (it may be correct, incorrect or not even valid julia). The good thing about this forum is that actual julia users share their knowledge and experience.
6 Likes
6 posts were split to a new topic: Download with password (digression)
Another security-related item that isn’t explicitly mentioned yet but definitely deserves a mention is that if you make a call like
url = 'https://n5eil01u.ecs.nsidc.org/DP4/ICEBRIDGE/IRMCR2.001/2010.03.24/IRMCR2_20100324_01.csv';
urlread(url,'Username','chadagreene','Password','notactuallymypassword')
then that line is going to be stored in your REPL history (in any language). So at a bare minimum you should be using some external file to store passwords and keys and load those programmatically.
2 Likes
@chadagreene in the SpaceLiDAR.jl package we write a .netrc file using this function:
keys(file)
end
return true
catch e
@error "Granule at $(granule.url) failed with $e."
return false
end
end
"""
netrc!(username, password)
Writes/updates a .netrc file for ICESat-2 and GEDI downloads. A .netrc is a plaintext
file containing your username and password for NASA EarthData and DAACs, and can be automatically
used by Julia using `Downloads` and tools like `wget`, `curl` among others.
"""
function netrc!(username, password)
if Sys.iswindows()
fn = joinpath(homedir(), "_netrc")
else
fn = joinpath(homedir(), ".netrc")
This file will allow NASA EarthData and DAACs to authenticate