Skip to content

Commit

Permalink
improve bucket object
Browse files Browse the repository at this point in the history
  • Loading branch information
chuongmep committed Jun 12, 2024
1 parent 86a1f7a commit 5054acd
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 20 deletions.
73 changes: 62 additions & 11 deletions APSToolkitPython/src/aps_toolkit/Bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pandas as pd
import requests
from .Token import Token
import os


class PublicKey(Enum):
Expand Down Expand Up @@ -66,7 +67,7 @@ def create_bucket(self, bucket_name: str, policy_key: PublicKey) -> dict:
}
response = requests.post(self.host, headers=headers, json=data)
if response.status_code != 200:
raise Exception(response.content)
raise Exception(response.reason)
return response.json()

def delete_bucket(self, bucket_name: str):
Expand All @@ -87,7 +88,7 @@ def delete_bucket(self, bucket_name: str):
url = f"{self.host}/{bucket_name}"
response = requests.delete(url, headers=headers)
if response.status_code != 200:
raise Exception(response.content)
raise Exception(response.reason)
return response.content

def get_objects(self, bucket_name: str) -> pd.DataFrame:
Expand All @@ -108,7 +109,7 @@ def get_objects(self, bucket_name: str) -> pd.DataFrame:
url = f"{self.host}/{bucket_name}/objects"
response = requests.get(url, headers=headers)
if response.status_code != 200:
raise Exception(response.content)
raise Exception(response.reason)
data = response.json()
df = pd.DataFrame(data["items"])
return df
Expand All @@ -124,6 +125,26 @@ def upload_object(self, bucket_name: str, file_path: str, object_name: str) -> d
file_path (str): The path of the file to be uploaded.
object_name (str): The name of the object to be created in the bucket.
Returns:
dict: A dictionary containing the response from the Autodesk OSS API.
"""
if not os.path.isabs(file_path):
file_path = os.path.abspath(file_path)
with open(file_path, "rb") as file:
stream = file.read()
return self.upload_object_stream(bucket_name, stream, object_name)

def upload_object_stream(self, bucket_name: str, stream: bytes, object_name: str) -> dict:
"""
Uploads an object to a specified bucket in the Autodesk OSS API.
This method sends a PUT request to the Autodesk OSS API. It includes an Authorization header with a bearer token for authentication and a Content-Type header set to "application/octet-stream". The bucket name and object name are passed in the URL of the request, and the file to be uploaded is passed in the body of the request as binary data. If the response status code is not 200, it raises an exception with the response content.
Args:
bucket_name (str): The name of the bucket to which the object will be uploaded.
stream (bytes): The stream of the file to be uploaded.
object_name (str): The name of the object to be created in the bucket.
Returns:
dict: A dictionary containing the response from the Autodesk OSS API.
"""
Expand All @@ -132,11 +153,10 @@ def upload_object(self, bucket_name: str, file_path: str, object_name: str) -> d
"Content-Type": "application/octet-stream"
}
url = f"{self.host}/{bucket_name}/objects/{object_name}"
with open(file_path, "rb") as file:
response = requests.put(url, headers=headers, data=file)
if response.status_code != 200:
raise Exception(response.content)
return response.json()
response = requests.put(url, headers=headers, data=stream)
if response.status_code != 200:
raise Exception(response.reason)
return response.json()

def delete_object(self, bucket_name: str, object_name: str) -> dict:
"""
Expand All @@ -157,7 +177,7 @@ def delete_object(self, bucket_name: str, object_name: str) -> dict:
url = f"{self.host}/{bucket_name}/objects/{object_name}"
response = requests.delete(url, headers=headers)
if response.status_code != 200:
raise Exception(response.content)
raise Exception(response.reason)
return response.content

def download_object(self, bucket_name: str, object_name: str, file_path: str) -> bool:
Expand All @@ -179,11 +199,42 @@ def download_object(self, bucket_name: str, object_name: str, file_path: str) ->
headers = {
"Authorization": f"Bearer {self.token.access_token}"
}
url = f"{self.host}/{bucket_name}/objects/{object_name}"
url = f"{self.host}/{bucket_name}/objects/{object_name}/signeds3download"
response = requests.get(url, headers=headers)
if response.status_code != 200:
raise Exception(response.content)
raise Exception(response.reason)
if not os.path.isabs(file_path):
file_path = os.path.abspath(file_path)
if not os.path.exists(os.path.dirname(file_path)):
os.makedirs(os.path.dirname(file_path))
result = response.json()
url = result["url"]
response = requests.get(url)
with open(file_path, "wb") as file:
file.write(response.content)
file.close()
return True

def download_stream_object(self, bucket_name: str, object_name: str) -> bytes:
"""
Downloads an object from a specified bucket in the Autodesk OSS API.
This method sends a GET request to the Autodesk OSS API. It includes an Authorization header with a bearer token for authentication. The bucket name and object name are passed in the URL of the request. If the response status code is not 200, it raises an exception with the response content.
The downloaded content is written to a file at the specified file path.
Args:
bucket_name (str): The name of the bucket from which the object will be downloaded.
object_name (str): The name of the object to be downloaded.
Returns:
None
"""
headers = {
"Authorization": f"Bearer {self.token.access_token}"
}
url = f"{self.host}/{bucket_name}/objects/{object_name}"
response = requests.get(url, headers=headers)
if response.status_code != 200:
raise Exception(response.reason)
return response.content
25 changes: 16 additions & 9 deletions APSToolkitPython/src/test/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,36 @@ def test_delete_bucket(self):

def test_get_objects(self):
bucket = Bucket(self.token)
bucket_name = "chuong_bucket"
bucket_name = "hello_world_23232"
objects = bucket.get_objects(bucket_name)
self.assertNotEqual(len(objects), 0)

def test_upload_object(self):
bucket = Bucket(self.token)
bucket_name = "chuong_bucket"
file_path = "/Users/chuongmep/Downloads/008950495344-Sep_2023.pdf"
object_name = "chuong.pdf"
bucket_name = "hello_world_23232"
file_path = "./test/resources/Test.dwg"
object_name = "Test.dwg"
response = bucket.upload_object(bucket_name, file_path, object_name)
self.assertEqual(response["bucketKey"], bucket_name)

def test_delete_object(self):
bucket = Bucket(self.token)
bucket_name = "chuong_bucket"
object_name = "chuong.pdf"
bucket_name = "hello_world_23232"
object_name = "Test.dwg"
result = bucket.delete_object(bucket_name, object_name)
self.assertEqual(result, b'')

def test_download_object(self):
bucket = Bucket(self.token)
bucket_name = "chuong_bucket"
object_name = "chuong.pdf"
file_path = "/Users/chuongmep/Downloads/hello.pdf"
bucket_name = "hello_world_23232"
object_name = "Test.dwg"
file_path = "./test/resources/Test2.dwg"
bucket.download_object(bucket_name, object_name, file_path)
self.assertTrue(True)

def test_download_stream_object(self):
bucket = Bucket(self.token)
bucket_name = "hello_world_23232"
object_name = "chuong.pdf"
stream = bucket.download_stream_object(bucket_name, object_name)
self.assertIsNotNone(stream)

0 comments on commit 5054acd

Please sign in to comment.