Skip to content

Commit

Permalink
add support download file bim 360
Browse files Browse the repository at this point in the history
  • Loading branch information
chuongmep committed May 11, 2024
1 parent 7834e79 commit b8a56a7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
39 changes: 39 additions & 0 deletions APSToolkitPython/src/aps_toolkit/BIM360.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,3 +515,42 @@ def delete_file_item(self, project_id: str, folder_id: str, file_name: str):
if response.status_code != 201:
raise Exception(response.content)
return response.content

def download_file_item(self, file_path: str, project_id: str, folder_id: str, file_name: str, version: int = -1):
"""
Download a file in BIM 360 Docs or Autodesk Construction Cloud
:param file_path: :class:`str` the path of file need to download
:param project_id: :class:`str` the unique identifier of a project
:param folder_id: :class:`str` the unique identifier of a folder
:param file_name: :class:`str` the name of file at the folder need to download
:param version: :class:`int` the version of file need to download
:return: :class:`str` the path of file after download
"""
item_id = self._get_item_id(project_id, folder_id, file_name)
if item_id is None:
raise Exception("File not found")
url = f"{self.host}/data/v1/projects/{project_id}/items/{item_id}/versions"
headers = {'Authorization': 'Bearer ' + self.token.access_token}
response = requests.get(url, headers=headers)
if response.status_code != 200:
raise Exception(response.content)
item_versions = response.json()
if version == -1:
url = item_versions['data'][0]['relationships']['storage']['data']['id']
else:
version = version - 1
if version < 0 or version >= len(item_versions['data']):
raise Exception("Version not found")
url = item_versions['data'][version]['relationships']['storage']['data']['id']
bucket_key = url.split("/").pop(0).split(":").pop()
object_key = url.split("/").pop()
s3_url = f"{self.host}/oss/v2/buckets/{bucket_key}/objects/{object_key}/signeds3download"
response = requests.get(s3_url, headers=headers)
if response.status_code != 200:
raise Exception(response.content)
download_url = response.json()['url']
print("Start Download File:", download_url)
response = requests.get(download_url)
with open(file_path, 'wb') as file:
file.write(response.content)
return file_path
8 changes: 8 additions & 0 deletions APSToolkitPython/src/test/test_bim360.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,11 @@ def test_delete_file_item(self):
file_name = "Test.dwg"
result = self.bim360.delete_file_item(self.project_id, self.folder_id, file_name)
self.assertNotEquals(result, 0)

def test_download_file_item(self):
file_name = "Test.dwg"
file_path = r"./test/resources/Test2.dwg"
file_path_result = self.bim360.download_file_item(file_path, self.project_id, self.folder_id, file_name, 2)
size = os.path.getsize(file_path)
size_result = os.path.getsize(file_path_result)
self.assertEqual(size, size_result)

0 comments on commit b8a56a7

Please sign in to comment.