diff --git a/dagshub/upload/wrapper.py b/dagshub/upload/wrapper.py index c0fcac31..12ae3836 100644 --- a/dagshub/upload/wrapper.py +++ b/dagshub/upload/wrapper.py @@ -191,7 +191,7 @@ def upload_files( remote_path: Optional[str] = None, bucket: bool = False, **kwargs, -): +) -> Optional[str]: """ Upload file(s) into a repository. @@ -204,10 +204,14 @@ def upload_files( bucket: Upload the file(s) to the DagsHub Storage bucket For kwarg docs look at :func:`Repo.upload() `. + + Returns: + Optional[str]: The DagsHub URL where the uploaded file(s) can be viewed, + or ``None`` for bucket uploads (see :func:`Repo.upload`). """ owner, repo = validate_owner_repo(repo) repo_obj = Repo(owner, repo) - repo_obj.upload(local_path, commit_message=commit_message, remote_path=remote_path, bucket=bucket, **kwargs) + return repo_obj.upload(local_path, commit_message=commit_message, remote_path=remote_path, bucket=bucket, **kwargs) def upload_file_to_s3( @@ -293,7 +297,7 @@ def upload( remote_path: Optional[str] = None, bucket: bool = False, **kwargs, - ): + ) -> Optional[str]: """ Upload a file or a directory to the repo. @@ -307,6 +311,11 @@ def upload( commit_message will be ignored. The kwargs are the parameters of :func:`upload_files` + + Returns: + Optional[str]: When uploading to the repo, returns the DagsHub file-browser URL for ``remote_path`` + on the target branch (``new_branch`` kwarg if provided, otherwise ``self.branch``). + Returns ``None`` for bucket uploads, which don't have a canonical file-browser URL. """ if commit_message is None: commit_message = DEFAULT_COMMIT_MESSAGE @@ -320,17 +329,21 @@ def upload( except ValueError: # local_path is outside cwd, use only its basename remote_path = local_path.name - remote_path = cast(str, Path(remote_path).as_posix()) + remote_path = posixpath.normpath(cast(str, Path(remote_path).as_posix())) if bucket: self.upload_files_to_bucket(local_path, remote_path, **kwargs) + return None + + if local_path.is_dir(): + dir_to_upload = self.directory(remote_path) + dir_to_upload.add_dir(str(local_path), commit_message=commit_message, **kwargs) else: - if local_path.is_dir(): - dir_to_upload = self.directory(remote_path) - dir_to_upload.add_dir(str(local_path), commit_message=commit_message, **kwargs) - else: - file_to_upload = DataSet.get_file(str(local_path), PurePosixPath(remote_path)) - self.upload_files([file_to_upload], commit_message=commit_message, **kwargs) + file_to_upload = DataSet.get_file(str(local_path), PurePosixPath(remote_path)) + self.upload_files([file_to_upload], commit_message=commit_message, **kwargs) + + target_branch = kwargs.get("new_branch") or self.branch + return self.get_repo_url(FILES_UI_URL, remote_path, branch=target_branch) @retry(retry=retry_if_exception_type(InternalServerErrorError), wait=wait_fixed(3), stop=stop_after_attempt(5)) def upload_files( @@ -589,7 +602,7 @@ def get_repo_url(self, url_format, directory, branch=None): owner=self.owner, reponame=self.name, branch=branch, - path=urllib.parse.quote(directory, safe=""), + path=urllib.parse.quote(directory, safe="/"), ), )