Skip to content

Commit

Permalink
add upload files example, improve upload function
Browse files Browse the repository at this point in the history
  • Loading branch information
jgstew committed Oct 11, 2023
1 parent 6245b65 commit 109b894
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
32 changes: 32 additions & 0 deletions examples/upload_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
Upload files in folder.
requires `besapi`, install with command `pip install besapi`
"""

import os

import besapi


def main(path_folder="./tmp"):
"""Execution starts here"""
print("main()")
bes_conn = besapi.besapi.get_bes_conn_using_config_file()
bes_conn.login()

print(f"INFO: Uploading new files within: {os.path.abspath(path_folder)}")

for entry in os.scandir(path_folder):
if entry.is_file() and "README.md" not in entry.path:
if " " in os.path.basename(entry.path):
print(f"ERROR: files cannot contain spaces! skipping: {entry.path}")
continue
print(f"Processing: {entry.path}")
output = bes_conn.upload(entry.path)
# print(output)
print(bes_conn.parse_upload_result_to_prefetch(output))


if __name__ == "__main__":
main("./examples/upload_files")
1 change: 1 addition & 0 deletions examples/upload_files/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
put files in this folder to have them be uploaded to the root server by the script upload_files.py
7 changes: 7 additions & 0 deletions src/besapi/besapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,13 @@ def upload(self, file_path, file_name=None, file_hash=None):
if not file_name:
file_name = os.path.basename(file_path)

# files cannot contain spaces:
if " " in file_name:
besapi_logger.warning(
"Replacing spaces with underscores in `%s`", file_name
)
file_name = file_name.replace(" ", "_")

if not file_hash:
besapi_logger.warning(
"SHA1 hash of file to be uploaded not provided, calculating it."
Expand Down

0 comments on commit 109b894

Please sign in to comment.