Skip to content

Downloading files by Synapse ID

This tutorial shows how to download any set of files from Synapse using their Synapse IDs. Rather than syncing an entire project or folder, this approach lets you target exactly the files you need and download them concurrently — even directing each file to a different local directory.

Tutorial Purpose

In this tutorial you will:

  1. Build a mapping of Synapse IDs to local download directories
  2. Download all files concurrently using the async API

Prerequisites

  • Make sure that you have completed the following tutorials:
  • The target directories (~/temp/subdir1, etc.) must exist before running the script. Create them or replace them with directories of your choice.

1. Build a mapping of Synapse IDs to download directories

Create a dictionary that maps each Synapse ID to the local path where that file should be saved. Files can be directed to different directories as needed.

# A mapping of Synapse IDs to the local directory each file should be downloaded to.
# Files can be directed to different directories as needed.
SYN_IDS_AND_PATHS = {
    "syn60584250": "~/temp/subdir1",
    "syn60584256": "~/temp/subdir1",
    "syn60584248": "~/temp/subdir1",
    "syn60584252": "~/temp/subdir1",
    "syn60584258": "~/temp/subdir1",
    "syn60584260": "~/temp/subdir1",
    "syn60584257": "~/temp/subdir1",
    "syn60584251": "~/temp/subdir1",
    "syn60584253": "~/temp/subdir1",
    "syn60584390": "~/temp/subdir1",
    "syn60584405": "~/temp/subdir2",
    "syn60584400": "~/temp/subdir3",
}

2. Download all files concurrently

Use File.get_async() together with asyncio.gather to kick off every download at the same time and wait for them all to finish.

tasks = []
    for syn_id, path in SYN_IDS_AND_PATHS.items():
        tasks.append(File(id=syn_id, path=path).get_async())

    # Download all files concurrently and wait for every one to finish
    results = await asyncio.gather(*tasks)

    print(f"Retrieved {len(results)} files")
After all downloads finish you'll see output like:
Retrieved 12 files

Source code for this tutorial

Click to show me
"""
Here is where you'll find the code for the downloading a file tutorial.
"""

import asyncio

from synapseclient import Synapse
from synapseclient.models import File

syn = Synapse()
syn.login()

# A mapping of Synapse IDs to the local directory each file should be downloaded to.
# Files can be directed to different directories as needed.
SYN_IDS_AND_PATHS = {
    "syn60584250": "~/temp/subdir1",
    "syn60584256": "~/temp/subdir1",
    "syn60584248": "~/temp/subdir1",
    "syn60584252": "~/temp/subdir1",
    "syn60584258": "~/temp/subdir1",
    "syn60584260": "~/temp/subdir1",
    "syn60584257": "~/temp/subdir1",
    "syn60584251": "~/temp/subdir1",
    "syn60584253": "~/temp/subdir1",
    "syn60584390": "~/temp/subdir1",
    "syn60584405": "~/temp/subdir2",
    "syn60584400": "~/temp/subdir3",
}


async def main():
    # Build a list of concurrent download tasks — one per Synapse ID
    tasks = []
    for syn_id, path in SYN_IDS_AND_PATHS.items():
        tasks.append(File(id=syn_id, path=path).get_async())

    # Download all files concurrently and wait for every one to finish
    results = await asyncio.gather(*tasks)

    print(f"Retrieved {len(results)} files")


asyncio.run(main())

References used in this tutorial