1- from typing import Any , Dict
1+ from typing import Any , Dict , Optional , Union
22
33from httpx import request
44
@@ -61,7 +61,9 @@ def download(
6161
6262 def upload (
6363 self ,
64- bucket_key : str ,
64+ * ,
65+ bucket_key : Optional [str ] = None ,
66+ bucket_name : Optional [str ] = None ,
6567 blob_file_path : str ,
6668 content_type : str ,
6769 source_path : str ,
@@ -70,11 +72,18 @@ def upload(
7072
7173 Args:
7274 bucket_key: The key of the bucket
75+ bucket_name: The name of the bucket
7376 blob_file_path: The path where the file will be stored in the bucket
7477 content_type: The MIME type of the file
7578 source_path: The local path of the file to upload
7679 """
77- bucket = self .retrieve_by_key (bucket_key )
80+ if not (bucket_name or bucket_key ):
81+ raise ValueError ("Must specify a bucket name or bucket key" )
82+ if bucket_name :
83+ bucket = self .retrieve (bucket_name )
84+ else :
85+ bucket = self .retrieve_by_key (bucket_key )
86+
7887 bucket_id = bucket ["Id" ]
7988
8089 endpoint = Endpoint (
@@ -101,6 +110,60 @@ def upload(
101110 else :
102111 request ("PUT" , write_uri , headers = headers , files = {"file" : file })
103112
113+ def upload_from_memory (
114+ self ,
115+ * ,
116+ bucket_key : Optional [str ] = None ,
117+ bucket_name : Optional [str ] = None ,
118+ blob_file_path : str ,
119+ content_type : str ,
120+ content : Union [str , bytes ],
121+ ) -> None :
122+ """Upload content from memory to a bucket.
123+
124+ Args:
125+ bucket_key: The key of the bucket
126+ bucket_name: The name of the bucket
127+ blob_file_path: The path where the content will be stored in the bucket
128+ content_type: The MIME type of the content
129+ content: The content to upload (string or bytes)
130+ """
131+ if not (bucket_name or bucket_key ):
132+ raise ValueError ("Must specify a bucket name or bucket key" )
133+ if bucket_name :
134+ bucket = self .retrieve (bucket_name )
135+ else :
136+ bucket = self .retrieve_by_key (bucket_key )
137+
138+ bucket_id = bucket ["Id" ]
139+
140+ endpoint = Endpoint (
141+ f"/orchestrator_/odata/Buckets({ bucket_id } )/UiPath.Server.Configuration.OData.GetWriteUri"
142+ )
143+
144+ result = self .request (
145+ "GET" ,
146+ endpoint ,
147+ params = {"path" : blob_file_path , "contentType" : content_type },
148+ ).json ()
149+ write_uri = result ["Uri" ]
150+
151+ headers = {
152+ key : value
153+ for key , value in zip (
154+ result ["Headers" ]["Keys" ], result ["Headers" ]["Values" ]
155+ )
156+ }
157+
158+ # Convert string to bytes if needed
159+ if isinstance (content , str ):
160+ content = content .encode ("utf-8" )
161+
162+ if result ["RequiresAuth" ]:
163+ self .request ("PUT" , write_uri , headers = headers , content = content )
164+ else :
165+ request ("PUT" , write_uri , headers = headers , content = content )
166+
104167 @infer_bindings ()
105168 def retrieve (self , name : str ) -> Any :
106169 """Retrieve bucket information by its name.
@@ -194,14 +257,14 @@ def custom_headers(self) -> Dict[str, str]:
194257 def _retrieve_spec (self , name : str ) -> RequestSpec :
195258 return RequestSpec (
196259 method = "GET" ,
197- endpoint = Endpoint ("/odata/Buckets" ),
260+ endpoint = Endpoint ("/orchestrator_/ odata/Buckets" ),
198261 params = {"$filter" : f"Name eq '{ name } '" , "$top" : 1 },
199262 )
200263
201264 def _retrieve_by_key_spec (self , key : str ) -> RequestSpec :
202265 return RequestSpec (
203266 method = "GET" ,
204267 endpoint = Endpoint (
205- f"/odata/Buckets/UiPath.Server.Configuration.OData.GetByKey(identifier={ key } )"
268+ f"/orchestrator_/ odata/Buckets/UiPath.Server.Configuration.OData.GetByKey(identifier={ key } )"
206269 ),
207270 )
0 commit comments