Last active
June 9, 2021 16:17
-
-
Save ryananeff/3681ae4e31427976ef30ece5d5891302 to your computer and use it in GitHub Desktop.
[Python] Recursively upload a directory and subdirectories to Synapse using Synapse Python API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!pip install synapseclient | |
| import synapseclient, os | |
| from synapseclient import Project, Folder, File, Link | |
| syn = synapseclient.Synapse() | |
| def upload_files_create_folders(path=".",parent_id=""): | |
| """ upload_files_create_folders(path=".",parent_id="") | |
| Recursively uploads all files and folders to a Synapse Project or folder. | |
| Inputs: | |
| path: | |
| The file path of a directory on disk to upload to Synapse. | |
| parent_id: | |
| The Synapse ID of the Project or Folder to upload to. | |
| Returns: | |
| None | |
| """ | |
| for f in os.listdir(path): | |
| print("checking %s"%f) | |
| basename = os.path.basename(f) | |
| newpath = path + basename | |
| if os.path.isdir(newpath): | |
| folder_name = basename | |
| data_folder = Folder(name=folder_name,parent=parent_id) | |
| new_folder = syn.store(data_folder) | |
| new_parent = new_folder.id | |
| print("Created new folder %s at path %s, synapse id: %s"%(folder_name,newpath,new_parent)) | |
| upload_files_create_folders(path=newpath,parent_id=new_parent) | |
| if os.path.isfile(newpath): | |
| if os.stat(newpath).st_size == 0: | |
| print("File at %s is 0 bytes, skipping"%newpath) | |
| continue | |
| new_file = File(path=newpath,parent=parent_id) | |
| new_file_id = syn.store(new_file) | |
| print("Created new file %s at path %s, synapse id: %s"%(basename,newpath,parent_id)) | |
| #Step 1. Login with your Synapse.org Credentials | |
| syn.login("username","password") | |
| #Step 2. Upload your files and folders recursively! | |
| upload_files_create_folders(path="/home/path/on/disk",parent_id="synapse_id") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment