Skip to content

[AI Foundry Data Management] Enhance .upload_file to accept dataset name and version arguments #40336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions sdk/ai/azure-ai-projects/azure/ai/projects/_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,18 @@ def from_connection_string(cls, conn_str: str, credential: "TokenCredential", **
project_name = parts[3]
return cls(endpoint, subscription_id, resource_group_name, project_name, credential, **kwargs)

def upload_file(self, file_path: Union[Path, str, PathLike]) -> Tuple[str, str]:
def upload_file(
self,
file_path: Union[Path, str, PathLike],
file_name: str = str(uuid.uuid4()),
file_version: str = "1",
) -> Tuple[str, str]:
"""Upload a file to the Azure AI Foundry project.
This method required *azure-ai-ml* to be installed.

:param file_path: The path to the file to upload.
:type file_path: Union[str, Path, PathLike]
:param Union[str, Path, PathLike] file_path: The path to the file to upload.
:param str, optional file_name: The name of the file to upload.
:param str, optional file_version: The version of the file to upload.
:return: The tuple, containing asset id and asset URI of uploaded file.
:rtype: Tuple[str]
"""
Expand All @@ -267,9 +273,9 @@ def upload_file(self, file_path: Union[Path, str, PathLike]) -> Tuple[str, str]:
data = Data(
path=str(file_path),
type=AssetTypes.URI_FILE,
name=str(uuid.uuid4()), # generating random name
name=file_name,
is_anonymous=True,
version="1",
version=file_version,
)

ml_client = MLClient(
Expand All @@ -278,8 +284,12 @@ def upload_file(self, file_path: Union[Path, str, PathLike]) -> Tuple[str, str]:
self._config3.resource_group_name,
self._config3.project_name,
)

data_asset = ml_client.data.create_or_update(data)
try:
data_asset = ml_client.data.create_or_update(data)
except Exception as e:
raise RuntimeError(
"Failed to upload file. Please ensure that the file path is correct and you have the necessary permissions."
) from e

return data_asset.id, data_asset.path

Expand Down