Skip to content

Commit 8066aae

Browse files
committed
Updated with support to upload directory to NS.
updated gitignore to ignore test files from local
1 parent 02ab493 commit 8066aae

File tree

2 files changed

+48
-14
lines changed

2 files changed

+48
-14
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,7 @@ target/
6565
# others
6666
.DS_Store
6767
test/secret.py
68-
spike/
68+
spike/
69+
70+
71+
upload-test-dir/

cms_netstorage.py

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,16 @@
1515
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1616
# See the License for the specific language governing permissions and
1717
# limitations under the License.
18-
#test Update
19-
18+
#adding folderUpload support
19+
import os
2020
import ast
2121
import optparse, sys
22-
2322
from akamai.netstorage import Netstorage
2423

25-
2624
class NetstorageParser(optparse.OptionParser):
2725
def format_epilog(self, formatter):
2826
return self.epilog
2927

30-
3128
def print_result(response, action):
3229
print("=== Request Header ===")
3330
print(response.request.headers)
@@ -39,14 +36,38 @@ def print_result(response, action):
3936
print("=== Response Content ===")
4037
print(response.text)
4138

39+
# Uploads a single file to NetStorage
40+
def upload_file(ns, local_file, remote_file):
41+
print(f"Uploading {local_file} to {remote_file}")
42+
43+
# If the upload method requires three arguments
44+
ok, res = ns.upload(local_file, remote_file)
45+
46+
# Check if the upload was successful
47+
if ok:
48+
print(f"Upload successful: {local_file} -> {remote_file}")
49+
else:
50+
print(f"Upload failed: {local_file} -> {remote_file}")
51+
52+
return ok, res
53+
54+
# Recursively upload directory contents
55+
def upload_directory(ns, local_dir, remote_dir):
56+
for root, dirs, files in os.walk(local_dir):
57+
for file in files:
58+
local_file_path = os.path.join(root, file)
59+
relative_path = os.path.relpath(local_file_path, local_dir) # Preserve directory structure
60+
remote_file_path = os.path.join(remote_dir, relative_path).replace("\\", "/") # Ensure remote path uses forward slashes
61+
ok, response = upload_file(ns, local_file_path, remote_file_path)
62+
print_result(response, 'upload')
4263

4364
if __name__ == '__main__':
4465
action_list = '''\
4566
dir: to list the contents of the directory /123456
4667
dir /123456
47-
upload: to upload file.txt to /123456 directory
68+
upload: to upload file.txt or a folder to /123456 directory
4869
upload file.txt /123456/ or
49-
upload file.txt /123456/file.txt
70+
upload uploaddir /123456/uploaddir
5071
stat: to display status of /123456/file.txt
5172
stat /123456/file.txt
5273
du: to display disk usage on directory /123456
@@ -90,6 +111,7 @@ def print_result(response, action):
90111
ns = Netstorage(options.hostname, options.keyname, options.key)
91112

92113
try:
114+
skipFinalLog = False
93115
res = None
94116
if options.action == 'delete':
95117
ok, res = ns.delete(args[0])
@@ -120,18 +142,27 @@ def print_result(response, action):
120142
elif options.action == 'symlink':
121143
ok, res = ns.symlink(args[0], args[1])
122144
elif options.action == 'upload':
123-
if len(args) >= 3:
124-
ok, res = ns.upload(args[0], args[1], args[2])
145+
local_path = args[0]
146+
remote_path = args[1]
147+
148+
if os.path.isdir(local_path):
149+
# Upload directory recursively
150+
skipFinalLog = True
151+
upload_directory(ns, local_path, remote_path)
152+
elif os.path.isfile(local_path):
153+
# Upload single file
154+
ok, res = upload_file(ns, local_path, remote_path)
155+
125156
else:
126-
ok, res = ns.upload(args[0], args[1])
157+
print(f"Invalid path: {local_path}. Must be a file or directory.")
127158
elif options.action == 'rename':
128159
ok, res = ns.rename(args[0], args[1])
129160
else:
130161
print("Invalid action.\nUse option -h or --help")
131162
exit()
132-
133-
print_result(res, options.action)
134-
163+
if not skipFinalLog:
164+
print_result(res, options.action)
165+
135166
except IndexError as e:
136167
if options.action == 'download' and args[0]:
137168
ok, res = ns.download(args[0])

0 commit comments

Comments
 (0)