|
| 1 | +[](){ #tutorial-downloading-a-file } |
| 2 | +# Downloading data by Synapse ID |
| 3 | + |
| 4 | +This tutorial shows how to download any set of files from Synapse using their |
| 5 | +Synapse IDs. Rather than syncing an entire project or folder, this approach lets |
| 6 | +you target exactly the files you need and download them **concurrently** — even |
| 7 | +directing each file to a different local directory. |
| 8 | + |
| 9 | + |
| 10 | +## Tutorial Purpose |
| 11 | +In this tutorial you will: |
| 12 | + |
| 13 | +1. Build a mapping of Synapse IDs to local download directories |
| 14 | +1. Download all files concurrently using the async API |
| 15 | + |
| 16 | + |
| 17 | +## Prerequisites |
| 18 | +* Make sure that you have completed the following tutorials: |
| 19 | + * [Folder](./folder.md) |
| 20 | + * [File](./file.md) |
| 21 | +* The target directories (`~/temp/subdir1`, etc.) must exist before running the |
| 22 | + script. Create them or replace them with directories of your choice. |
| 23 | + |
| 24 | + |
| 25 | +## 1. Build a mapping of Synapse IDs to download directories |
| 26 | + |
| 27 | +Create a dictionary that maps each Synapse ID to the local path where that file |
| 28 | +should be saved. Files can be directed to different directories as needed. |
| 29 | + |
| 30 | +```python |
| 31 | +{!docs/tutorials/python/tutorial_scripts/download_data_by_synid.py!lines=13-30} |
| 32 | +``` |
| 33 | + |
| 34 | + |
| 35 | +## 2. Download all files concurrently |
| 36 | + |
| 37 | +Use `File.get_async()` together with `asyncio.gather` to kick off every download |
| 38 | +at the same time and wait for them all to finish. |
| 39 | + |
| 40 | +```python |
| 41 | +{!docs/tutorials/python/tutorial_scripts/download_data_by_synid.py!lines=31-43} |
| 42 | +``` |
| 43 | + |
| 44 | +<details class="example"> |
| 45 | + <summary>After all downloads finish you'll see output like:</summary> |
| 46 | +``` |
| 47 | +Retrieved 12 files |
| 48 | +``` |
| 49 | +</details> |
| 50 | + |
| 51 | + |
| 52 | +## Source code for this tutorial |
| 53 | + |
| 54 | +<details class="quote"> |
| 55 | + <summary>Click to show me</summary> |
| 56 | + |
| 57 | +```python |
| 58 | +{!docs/tutorials/python/tutorial_scripts/download_data_by_synid.py!} |
| 59 | +``` |
| 60 | +</details> |
| 61 | + |
| 62 | +## References used in this tutorial |
| 63 | + |
| 64 | +- [File][synapseclient.models.File] |
| 65 | +- [File.get_async][synapseclient.models.File.get_async] |
| 66 | +- [syn.login][synapseclient.Synapse.login] |
0 commit comments