15
15
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
16
# See the License for the specific language governing permissions and
17
17
# limitations under the License.
18
- #test Update
19
-
18
+ #adding folderUpload support
19
+ import os
20
20
import ast
21
21
import optparse , sys
22
-
23
22
from akamai .netstorage import Netstorage
24
23
25
-
26
24
class NetstorageParser (optparse .OptionParser ):
27
25
def format_epilog (self , formatter ):
28
26
return self .epilog
29
27
30
-
31
28
def print_result (response , action ):
32
29
print ("=== Request Header ===" )
33
30
print (response .request .headers )
@@ -39,14 +36,38 @@ def print_result(response, action):
39
36
print ("=== Response Content ===" )
40
37
print (response .text )
41
38
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' )
42
63
43
64
if __name__ == '__main__' :
44
65
action_list = '''\
45
66
dir: to list the contents of the directory /123456
46
67
dir /123456
47
- upload: to upload file.txt to /123456 directory
68
+ upload: to upload file.txt or a folder to /123456 directory
48
69
upload file.txt /123456/ or
49
- upload file.txt /123456/file.txt
70
+ upload uploaddir /123456/uploaddir
50
71
stat: to display status of /123456/file.txt
51
72
stat /123456/file.txt
52
73
du: to display disk usage on directory /123456
@@ -90,6 +111,7 @@ def print_result(response, action):
90
111
ns = Netstorage (options .hostname , options .keyname , options .key )
91
112
92
113
try :
114
+ skipFinalLog = False
93
115
res = None
94
116
if options .action == 'delete' :
95
117
ok , res = ns .delete (args [0 ])
@@ -120,18 +142,27 @@ def print_result(response, action):
120
142
elif options .action == 'symlink' :
121
143
ok , res = ns .symlink (args [0 ], args [1 ])
122
144
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
+
125
156
else :
126
- ok , res = ns . upload ( args [ 0 ], args [ 1 ] )
157
+ print ( f"Invalid path: { local_path } . Must be a file or directory." )
127
158
elif options .action == 'rename' :
128
159
ok , res = ns .rename (args [0 ], args [1 ])
129
160
else :
130
161
print ("Invalid action.\n Use option -h or --help" )
131
162
exit ()
132
-
133
- print_result (res , options .action )
134
-
163
+ if not skipFinalLog :
164
+ print_result (res , options .action )
165
+
135
166
except IndexError as e :
136
167
if options .action == 'download' and args [0 ]:
137
168
ok , res = ns .download (args [0 ])
0 commit comments