File API

class supervisely_lib.api.file_api.FileApi(api)[source]

Bases: supervisely_lib.api.module_api.ModuleApiBase

API for working with Files. FileApi object is immutable.

Parameters

api (Api) – API connection to the server.

Usage example
# You can connect to API directly
address = 'https://app.supervise.ly/'
token = 'Your Supervisely API Token'
api = sly.Api(address, token)

# Or you can use API from environment
os.environ['SERVER_ADDRESS'] = 'https://app.supervise.ly'
os.environ['API_TOKEN'] = 'Your Supervisely API Token'
api = sly.Api.from_env()

team_id = 8
file_path = "/999_App_Test/"
files = api.file.list(team_id, file_path)
static info_sequence()[source]

NamedTuple FileInfo information about File.

Example
FileInfo(team_id=8,
         id=7660,
         user_id=7,
         name='00135.json',
         hash='z7Hv9a7WIC5HIJrfX/69KVrvtDaLqucSprWHoCxyq0M=',
         path='/999_App_Test/ds1/00135.json',
         storage_path='/h5un6l2bnaz1vj8a9qgms4-public/teams_storage/8/y/P/rn/...json',
         mime='application/json',
         ext='json',
         sizeb=261,
         created_at='2021-01-11T09:04:17.959Z',
         updated_at='2021-01-11T09:04:17.959Z',
         full_storage_url='http://supervise.ly/h5un6l2bnaz1vj8a9qgms4-public/teams_storage/8/y/P/rn/...json')
static info_tuple_name()[source]

NamedTuple name - FileInfo.

list(team_id: int, path: str) → List[dict][source]

List of files in the Team Files.

Parameters
  • team_id (int) – Team ID in Supervisely.

  • path (str) – Path to File or Directory.

Returns

List of all Files with information. See info_sequence

Return type

List[dict]

Usage example
os.environ['SERVER_ADDRESS'] = 'https://app.supervise.ly'
os.environ['API_TOKEN'] = 'Your Supervisely API Token'
api = sly.Api.from_env()

team_id = 8
file_path = "/999_App_Test/"
files = api.file.list(team_id, file_path)

print(files)
# Output: [
#     {
#         "id":7660,
#         "userId":7,
#         "path":"/999_App_Test/ds1/00135.json",
#         "storagePath":"/h5un6l2bnaz1vj8a9qgms4-public/teams_storage/8/y/P/rn/...json",
#         "meta":{
#             "ext":"json",
#             "mime":"application/json",
#             "size":261
#         },
#         "createdAt":"2021-01-11T09:04:17.959Z",
#         "updatedAt":"2021-01-11T09:04:17.959Z",
#         "hash":"z7Wv1a7WIC5HIJrfX/69XXrqtDaLxucSprWHoCxyq0M=",
#         "fullStorageUrl":"http://supervise.ly/h5un6l2bnaz1vj8a9qgms4-public/teams_storage/8/y/P/rn/...json",
#         "teamId":8,
#         "name":"00135.json"
#     },
#     {
#         "id":7661,
#         "userId":7,
#         "path":"/999_App_Test/ds1/01587.json",
#         "storagePath":"/h5un6l2bnaz1vj8a9qgms4-public/teams_storage/8/9/k/Hs/...json",
#         "meta":{
#             "ext":"json",
#             "mime":"application/json",
#             "size":252
#         },
#         "createdAt":"2021-01-11T09:04:18.099Z",
#         "updatedAt":"2021-01-11T09:04:18.099Z",
#         "hash":"La9+XtF2+cTlAqUE/I72e/xS12LqyH1+z<3T+SgD4CTU=",
#         "fullStorageUrl":"http://supervise.ly/h5un6l2bnaz1vj8a9qgms4-public/teams_storage/8/9/k/Hs/...json",
#         "teamId":8,
#         "name":"01587.json"
#     }
# ]
download(team_id: int, remote_path: str, local_save_path: str, cache: Optional[supervisely_lib.io.fs_cache.FileCache] = None, progress_cb: Optional[supervisely_lib.task.progress.Progress] = None) → None[source]

Download File from Team Files.

Parameters
  • team_id (int) – Team ID in Supervisely.

  • remote_path (str) – Path to File in Team Files.

  • local_save_path (str) – Local save path.

  • cache (FileCache, optional) – optional

  • progress_cb (Progress, optional) – Function for tracking download progress.

Returns

None

Return type

NoneType

Usage example
os.environ['SERVER_ADDRESS'] = 'https://app.supervise.ly'
os.environ['API_TOKEN'] = 'Your Supervisely API Token'
api = sly.Api.from_env()

path_to_file = "/999_App_Test/ds1/01587.json"
local_save_path = "/home/admin/Downloads/01587.json"

api.file.download(8, path_to_file, local_save_path)
upload(team_id: int, src: str, dst: str, progress_cb: Optional[supervisely_lib.task.progress.Progress] = None) → NamedTuple[source]

Upload File to Team Files.

Parameters
  • team_id (int) – Team ID in Supervisely.

  • src (str) – Local source file path.

  • dst (str) – Path to File in Team Files.

  • progress_cb (Progress, optional) – Function for tracking download progress.

Returns

Information about File. See info_sequence

Return type

NamedTuple

Usage example
os.environ['SERVER_ADDRESS'] = 'https://app.supervise.ly'
os.environ['API_TOKEN'] = 'Your Supervisely API Token'
api = sly.Api.from_env()

src_path = "/home/admin/Downloads/01587.json"
dst_remote_path = "/999_App_Test/ds1/01587.json"

api.file.upload(8, src_path, dst_remote_path)
upload_bulk(team_id: int, src_paths: List[str], dst_paths: List[str], progress_cb: Optional[supervisely_lib.task.progress.Progress] = None) → List[NamedTuple][source]

Upload Files to Team Files.

Parameters
  • team_id (int) – Team ID in Supervisely.

  • src (List[str]) – Local source file paths.

  • dst (List[str]) – Destination paths for Files to Team Files.

  • progress_cb (Progress, optional) – Function for tracking download progress.

Returns

Information about Files. See info_sequence

Return type

List[NamedTuple]

Usage example
os.environ['SERVER_ADDRESS'] = 'https://app.supervise.ly'
os.environ['API_TOKEN'] = 'Your Supervisely API Token'
api = sly.Api.from_env()

src_paths = ["/home/admin/Downloads/01587.json", "/home/admin/Downloads/01588.json","/home/admin/Downloads/01589.json"]
dst_remote_paths = ["/999_App_Test/ds1/01587.json", "/999_App_Test/ds1/01588.json", "/999_App_Test/ds1/01589.json"]

api.file.upload_bulk(8, src_paths, dst_remote_paths)
rename(old_name: str, new_name: str) → None[source]

Renames file in Team Files

Parameters
  • old_name (str) – Old File name.

  • new_name (str) – New File name.

Returns

None

Return type

NoneType

Usage example
os.environ['SERVER_ADDRESS'] = 'https://app.supervise.ly'
os.environ['API_TOKEN'] = 'Your Supervisely API Token'
api = sly.Api.from_env()

# NotImplementedError('Method is not supported')
remove(team_id: int, path: str) → None[source]

Removes file from Team Files.

Parameters
  • team_id (int) – Team ID in Supervisely.

  • path (str) – Path to File in Team Files.

Returns

None

Return type

NoneType

Usage example
os.environ['SERVER_ADDRESS'] = 'https://app.supervise.ly'
os.environ['API_TOKEN'] = 'Your Supervisely API Token'
api = sly.Api.from_env()

api.file.remove(8, "/999_App_Test/ds1/01587.json")
exists(team_id: int, remote_path: str) → bool[source]

Checks if file exists in Team Files.

Parameters
  • team_id (int) – Team ID in Supervisely.

  • remote_path (str) – Remote path to File in Team Files.

Returns

True if file exists, otherwise False

Return type

bool

Usage example
os.environ['SERVER_ADDRESS'] = 'https://app.supervise.ly'
os.environ['API_TOKEN'] = 'Your Supervisely API Token'
api = sly.Api.from_env()

file = api.file.exists(8, "/999_App_Test/ds1/02163.json") # True
file = api.file.exists(8, "/999_App_Test/ds1/01587.json") # False
dir_exists(team_id: int, remote_directory: str) → bool[source]

Checks if directory exists in Team Files.

Parameters
  • team_id (int) – Team ID in Supervisely.

  • remote_path (str) – Remote path to directory in Team Files.

Returns

True if directory exists, otherwise False

Return type

bool

Usage example
os.environ['SERVER_ADDRESS'] = 'https://app.supervise.ly'
os.environ['API_TOKEN'] = 'Your Supervisely API Token'
api = sly.Api.from_env()

file = api.file.exists(8, "/999_App_Test/")   # True
file = api.file.exists(8, "/10000_App_Test/") # False
get_free_name(team_id: int, path: str) → str[source]

Adds suffix to the end of the file name.

Parameters
  • team_id (int) – Team ID in Supervisely.

  • path (str) – Remote path to file in Team Files.

Returns

New File name with suffix at the end

Return type

str

Usage example
os.environ['SERVER_ADDRESS'] = 'https://app.supervise.ly'
os.environ['API_TOKEN'] = 'Your Supervisely API Token'
api = sly.Api.from_env()

file = api.file.get_free_name(8, "/999_App_Test/ds1/02163.json")
print(file)
# Output: /999_App_Test/ds1/02163_000.json
get_url(file_id: int) → str[source]

Gets URL for the File by ID.

Parameters

file_id (int) – File ID in Supervisely.

Returns

File URL

Return type

str

Usage example
os.environ['SERVER_ADDRESS'] = 'https://app.supervise.ly'
os.environ['API_TOKEN'] = 'Your Supervisely API Token'
api = sly.Api.from_env()

file_id = 7660
file_url = sly.api.file.get_url(file_id)
print(file_url)
# Output: http://supervise.ly/files/7660
get_info_by_path(team_id: int, remote_path: str) → NamedTuple[source]

Gets File information by path in Team Files.

Parameters
  • team_id (int) – Team ID in Supervisely.

  • remote_path (str) – Remote path to file in Team Files.

Returns

Information about File. See info_sequence

Return type

NamedTuple

Usage example
os.environ['SERVER_ADDRESS'] = 'https://app.supervise.ly'
os.environ['API_TOKEN'] = 'Your Supervisely API Token'
api = sly.Api.from_env()

file_path = "/999_App_Test/ds1/00135.json"
file_info = api.file.get_info_by_id(8, file_path)
print(file_info)
# Output: FileInfo(team_id=8,
#                  id=7660,
#                  user_id=7,
#                  name='00135.json',
#                  hash='z7Hv9a7WIC5HIJrfX/69KVrvtDaLqucSprWHoCxyq0M=',
#                  path='/999_App_Test/ds1/00135.json',
#                  storage_path='/h5un6l2bnaz1vj8a9qgms4-public/teams_storage/8/y/P/rn/...json',
#                  mime='application/json',
#                  ext='json',
#                  sizeb=261,
#                  created_at='2021-01-11T09:04:17.959Z',
#                  updated_at='2021-01-11T09:04:17.959Z',
#                  full_storage_url='http://supervise.ly/h5un6l2bnaz1vj8a9qgms4-public/teams_storage/8/y/P/rn/...json')
InfoType

alias of supervisely_lib.api.module_api.FileInfo

get_info_by_id(id: int) → NamedTuple[source]

Gets information about File by ID.

Parameters

id (int) – File ID in Supervisely.

Returns

Information about File. See info_sequence

Return type

NamedTuple

Usage example
os.environ['SERVER_ADDRESS'] = 'https://app.supervise.ly'
os.environ['API_TOKEN'] = 'Your Supervisely API Token'
api = sly.Api.from_env()

file_id = 7660
file_info = api.file.get_info_by_id(file_id)
print(file_info)
# Output: FileInfo(team_id=8,
#                  id=7660,
#                  user_id=7,
#                  name='00135.json',
#                  hash='z7Hv9a7WIC5HIJrfX/69KVrvtDaLqucSprWHoCxyq0M=',
#                  path='/999_App_Test/ds1/00135.json',
#                  storage_path='/h5un6l2bnaz1vj8a9qgms4-public/teams_storage/8/y/P/rn/...json',
#                  mime='application/json',
#                  ext='json',
#                  sizeb=261,
#                  created_at='2021-01-11T09:04:17.959Z',
#                  updated_at='2021-01-11T09:04:17.959Z',
#                  full_storage_url='http://supervise.ly/h5un6l2bnaz1vj8a9qgms4-public/teams_storage/8/y/P/rn/...json')