1- from typing import Any , Dict
1+ from typing import Any , Dict , Optional , Union
22
33from httpx import request
44
@@ -59,7 +59,9 @@ def download(
5959
6060 def upload (
6161 self ,
62- bucket_key : str ,
62+ * ,
63+ bucket_key : Optional [str ] = None ,
64+ bucket_name : Optional [str ] = None ,
6365 blob_file_path : str ,
6466 content_type : str ,
6567 source_path : str ,
@@ -68,11 +70,18 @@ def upload(
6870
6971 Args:
7072 bucket_key: The key of the bucket
73+ bucket_name: The name of the bucket
7174 blob_file_path: The path where the file will be stored in the bucket
7275 content_type: The MIME type of the file
7376 source_path: The local path of the file to upload
7477 """
75- bucket = self .retrieve_by_key (bucket_key )
78+ if bucket_key :
79+ bucket = self .retrieve_by_key (bucket_key )
80+ elif bucket_name :
81+ bucket = self .retrieve (bucket_name )
82+ else :
83+ raise ValueError ("Must specify a bucket name or bucket key" )
84+
7685 bucket_id = bucket ["Id" ]
7786
7887 endpoint = Endpoint (
@@ -99,6 +108,60 @@ def upload(
99108 else :
100109 request ("PUT" , write_uri , headers = headers , files = {"file" : file })
101110
111+ def upload_from_memory (
112+ self ,
113+ * ,
114+ bucket_key : Optional [str ] = None ,
115+ bucket_name : Optional [str ] = None ,
116+ blob_file_path : str ,
117+ content_type : str ,
118+ content : Union [str , bytes ],
119+ ) -> None :
120+ """Upload content from memory to a bucket.
121+
122+ Args:
123+ bucket_key: The key of the bucket
124+ bucket_name: The name of the bucket
125+ blob_file_path: The path where the content will be stored in the bucket
126+ content_type: The MIME type of the content
127+ content: The content to upload (string or bytes)
128+ """
129+ if bucket_key :
130+ bucket = self .retrieve_by_key (bucket_key )
131+ elif bucket_name :
132+ bucket = self .retrieve (bucket_name )
133+ else :
134+ raise ValueError ("Must specify a bucket name or bucket key" )
135+
136+ bucket_id = bucket ["Id" ]
137+
138+ endpoint = Endpoint (
139+ f"/orchestrator_/odata/Buckets({ bucket_id } )/UiPath.Server.Configuration.OData.GetWriteUri"
140+ )
141+
142+ result = self .request (
143+ "GET" ,
144+ endpoint ,
145+ params = {"path" : blob_file_path , "contentType" : content_type },
146+ ).json ()
147+ write_uri = result ["Uri" ]
148+
149+ headers = {
150+ key : value
151+ for key , value in zip (
152+ result ["Headers" ]["Keys" ], result ["Headers" ]["Values" ]
153+ )
154+ }
155+
156+ # Convert string to bytes if needed
157+ if isinstance (content , str ):
158+ content = content .encode ("utf-8" )
159+
160+ if result ["RequiresAuth" ]:
161+ self .request ("PUT" , write_uri , headers = headers , content = content )
162+ else :
163+ request ("PUT" , write_uri , headers = headers , content = content )
164+
102165 @infer_bindings ()
103166 def retrieve (self , name : str ) -> Any :
104167 """Retrieve bucket information by its name.
@@ -192,14 +255,14 @@ def custom_headers(self) -> Dict[str, str]:
192255 def _retrieve_spec (self , name : str ) -> RequestSpec :
193256 return RequestSpec (
194257 method = "GET" ,
195- endpoint = Endpoint ("/odata/Buckets" ),
258+ endpoint = Endpoint ("/orchestrator_/ odata/Buckets" ),
196259 params = {"$filter" : f"Name eq '{ name } '" , "$top" : 1 },
197260 )
198261
199262 def _retrieve_by_key_spec (self , key : str ) -> RequestSpec :
200263 return RequestSpec (
201264 method = "GET" ,
202265 endpoint = Endpoint (
203- f"/odata/Buckets/UiPath.Server.Configuration.OData.GetByKey(identifier={ key } )"
266+ f"/orchestrator_/ odata/Buckets/UiPath.Server.Configuration.OData.GetByKey(identifier={ key } )"
204267 ),
205268 )
0 commit comments