# How to download a file from datastore using python script?

**URL:** <https://discourse.slicer.org/t/how-to-download-a-file-from-datastore-using-python-script/4964>\
**Category:** Support\
**Created:** [December 4, 2018, 10:21pm UTC](https://discourse.slicer.org/t/how-to-download-a-file-from-datastore-using-python-script/4964 "2018-12-04T22:21:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![brhoom](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/brhoom/32/1228_2.png) [@brhoom](https://discourse.slicer.org/u/brhoom)\
**Post date:** [December 4, 2018, 10:21pm UTC](https://discourse.slicer.org/t/how-to-download-a-file-from-datastore-using-python-script/4964/1 "2018-12-04T22:21:15Z")

</div>

Hi,

I am trying to add testing part to my module using public images from [datastore](https://www.slicer.org/wiki/Documentation/Nightly/Modules/DataStore). How can I download the image with python script?

Thanks!

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [December 4, 2018, 11:52pm UTC](https://discourse.slicer.org/t/how-to-download-a-file-from-datastore-using-python-script/4964/2 "2018-12-04T23:52:34Z")

</div>

I’m not familiar with the DataStore module, but there are several Slicer modules that use data from the SampleData module for testing purposes. See [https://github.com/Slicer/Slicer/blob/2e5dcf178d0d26d359a10437aa16223e3a594e2f/Modules/Scripted/SegmentStatistics/SegmentStatistics.py#L653-L654](https://github.com/Slicer/Slicer/blob/2e5dcf178d0d26d359a10437aa16223e3a594e2f/Modules/Scripted/SegmentStatistics/SegmentStatistics.py#L653-L654) for an example.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [December 5, 2018, 4:49am UTC](https://discourse.slicer.org/t/how-to-download-a-file-from-datastore-using-python-script/4964/3 "2018-12-05T04:49:47Z")

</div>

You can download sample data sets from any URL (MIDAS, GitHub, etc.) using SampleData module:

```auto
import SampleData
loadedNodes = SampleData.SampleDataLogic().downloadFromSource(SampleData.SampleDataSource(
    nodeNames='fixed', 
    fileNames='fixed.nrrd', 
    uris='http://slicer.kitware.com/midas3/download/item/157188/small-mr-eye-fixed.nrrd'))

```

---

<div class="post-metadata">

**Author:** ![brhoom](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/brhoom/32/1228_2.png) [@brhoom](https://discourse.slicer.org/u/brhoom)\
**Post date:** [December 5, 2018, 8:16am UTC](https://discourse.slicer.org/t/how-to-download-a-file-from-datastore-using-python-script/4964/4 "2018-12-05T08:16:56Z")

</div>

Thanks all for the provided information. I used a workaround which works for me as I want the sample to be downloaded in a specific location.

I have only a problem with midas link as the file is created before it is downloaded completely which makes loadVolume fail. For now I am using a different server.

```
try:         
    print("Downloading cochlea sample image ...")
    import urllib
    imgLaWebLink = "https://mtixnat.uni-koblenz.de/owncloud/index.php/s/eMvm9LHNHEHoZg3/download"
    #imgLaWebLink = "http://slicer.kitware.com/midas3/download/item/381221/P100001_DV_L_a"
    urllib.urlretrieve (imgLaWebLink , self.outputPath+"/imgA.nrrd" )
except Exception as e:
              print("Error: can not download sample file ...")
              print(e)   
              return -1
#end try-except 
[success, inputVolumeNode] = slicer.util.loadVolume( self.outputPath+"/imgA.nrrd", returnNode=True)
```

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [December 6, 2018, 2:26am UTC](https://discourse.slicer.org/t/how-to-download-a-file-from-datastore-using-python-script/4964/5 "2018-12-06T02:26:50Z")

</div>

SampleData uses urllib under the hood but has a number of additional features that might be relevant for some use cases. For example downloaded data sets are cached (this is probably the most significant, as downloading a large data set may take tens of seconds), failed, partial, and corrupted downloads are automatically reset/restarted, nodes can be loaded from of zip files (zip files are automatically unpacked), callback can be set for progress reporting, the same data source can be registered with SampleData module to appear in the module’s GUI.
