From 0cb588c6739cc25669aff96e446ccde52cc28aab Mon Sep 17 00:00:00 2001 From: Dheepak Shunmugavel Date: Tue, 17 Sep 2024 13:19:05 -0500 Subject: [PATCH 1/3] checking access --- cms_netstorage.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cms_netstorage.py b/cms_netstorage.py index 1df8e05..0662a6f 100644 --- a/cms_netstorage.py +++ b/cms_netstorage.py @@ -15,7 +15,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - +#adding folderUpload support import ast import optparse, sys From 450fcdfaf77d5cea73de3219997af9025e0c559f Mon Sep 17 00:00:00 2001 From: Dheepak Shunmugavel Date: Tue, 17 Sep 2024 13:25:41 -0500 Subject: [PATCH 2/3] Changes to add support for Folder upload --- cms_netstorage.py | 55 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/cms_netstorage.py b/cms_netstorage.py index 0662a6f..7d97651 100644 --- a/cms_netstorage.py +++ b/cms_netstorage.py @@ -16,18 +16,15 @@ # See the License for the specific language governing permissions and # limitations under the License. #adding folderUpload support - +import os import ast import optparse, sys - from akamai.netstorage import Netstorage - class NetstorageParser(optparse.OptionParser): def format_epilog(self, formatter): return self.epilog - def print_result(response, action): print("=== Request Header ===") print(response.request.headers) @@ -39,14 +36,38 @@ def print_result(response, action): print("=== Response Content ===") print(response.text) +# Uploads a single file to NetStorage +def upload_file(ns, local_file, remote_file): + print(f"Uploading {local_file} to {remote_file}") + + # If the upload method requires three arguments + ok, res = ns.upload(local_file, remote_file) + + # Check if the upload was successful + if ok: + print(f"Upload successful: {local_file} -> {remote_file}") + else: + print(f"Upload failed: {local_file} -> {remote_file}") + + return ok, res + +# Recursively upload directory contents +def upload_directory(ns, local_dir, remote_dir): + for root, dirs, files in os.walk(local_dir): + for file in files: + local_file_path = os.path.join(root, file) + relative_path = os.path.relpath(local_file_path, local_dir) # Preserve directory structure + remote_file_path = os.path.join(remote_dir, relative_path).replace("\\", "/") # Ensure remote path uses forward slashes + ok, response = upload_file(ns, local_file_path, remote_file_path) + print_result(response, 'upload') if __name__ == '__main__': action_list = '''\ dir: to list the contents of the directory /123456 dir /123456 - upload: to upload file.txt to /123456 directory + upload: to upload file.txt or a folder to /123456 directory upload file.txt /123456/ or - upload file.txt /123456/file.txt + upload uploaddir /123456/uploaddir stat: to display status of /123456/file.txt stat /123456/file.txt du: to display disk usage on directory /123456 @@ -90,6 +111,7 @@ def print_result(response, action): ns = Netstorage(options.hostname, options.keyname, options.key) try: + skipFinalLog = False res = None if options.action == 'delete': ok, res = ns.delete(args[0]) @@ -120,18 +142,27 @@ def print_result(response, action): elif options.action == 'symlink': ok, res = ns.symlink(args[0], args[1]) elif options.action == 'upload': - if len(args) >= 3: - ok, res = ns.upload(args[0], args[1], args[2]) + local_path = args[0] + remote_path = args[1] + + if os.path.isdir(local_path): + # Upload directory recursively + skipFinalLog = True + upload_directory(ns, local_path, remote_path) + elif os.path.isfile(local_path): + # Upload single file + ok, res = upload_file(ns, local_path, remote_path) + else: - ok, res = ns.upload(args[0], args[1]) + print(f"Invalid path: {local_path}. Must be a file or directory.") elif options.action == 'rename': ok, res = ns.rename(args[0], args[1]) else: print("Invalid action.\nUse option -h or --help") exit() - - print_result(res, options.action) - + if not skipFinalLog: + print_result(res, options.action) + except IndexError as e: if options.action == 'download' and args[0]: ok, res = ns.download(args[0]) From 020188d5e843cdc42d647a6dfbb381ffc6634bd8 Mon Sep 17 00:00:00 2001 From: Dheepak Shunmugavel Date: Tue, 17 Sep 2024 13:28:56 -0500 Subject: [PATCH 3/3] update the gitignore --- .gitignore | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 7256add..07cbb47 100644 --- a/.gitignore +++ b/.gitignore @@ -65,4 +65,8 @@ target/ # others .DS_Store test/secret.py -spike/ \ No newline at end of file +spike/ + + +# PyBuilder +.vscode/ \ No newline at end of file