Last active
April 1, 2020 17:55
-
-
Save jhodges10/2a6e6113cbd3b2787cb41aa7be3329b0 to your computer and use it in GitHub Desktop.
This file contains 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
import os | |
import time | |
import mimetypes | |
from frameioclient import FrameioClient | |
from pprint import pprint | |
def upload(file_p, parent_asset_id): | |
abs_path = os.path.abspath(file_p) | |
file_s = os.path.getsize(file_p) | |
file_n = os.path.split(file_p)[1] | |
file_mime = mimetypes.guess_type(abs_path)[0] | |
asset = client.create_asset( | |
parent_asset_id=parent_asset_id, | |
name=file_n, | |
type="file", | |
filetype=file_mime, | |
filesize=file_s | |
) | |
with open(abs_path, "rb") as ul_file: | |
asset_info = client.upload(asset, ul_file) | |
return asset_info | |
def create_folder(folder_n, parent_asset_id): | |
asset = client.create_asset( | |
parent_asset_id=parent_asset_id, | |
name=folder_n, | |
type="folder", | |
) | |
return asset['id'] | |
def recursive_upload(client, folder, parent_asset_id): | |
# Seperate files and folders: | |
file_list = list() | |
folder_list = list() | |
for item in os.listdir(folder): | |
if item == ".DS_Store": # Ignore .DS_Store files on Mac | |
continue | |
complete_item_path = os.path.join(folder, item) | |
if os.path.isfile(complete_item_path): | |
file_list.append(item) | |
else: | |
folder_list.append(item) | |
for file_i in file_list: | |
complete_dir_obj = os.path.join(folder, file_i) | |
upload(complete_dir_obj, parent_asset_id) | |
for folder_i in folder_list: | |
new_folder = os.path.join(folder, folder_i) | |
new_parent_asset_id = create_folder(folder_i, parent_asset_id) | |
recursive_upload(client, new_folder, new_parent_asset_id) | |
if __name__ == "__main__": | |
upload_folder = "./test_structure" | |
client = FrameioClient(os.getenv("FRAME_IO_TOKEN")) | |
parent_asset_id = "YOUR_PARENT_ASSET_ID" | |
recursive_upload(client, upload_folder, parent_asset_id) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment