-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhubic_ftp
More file actions
executable file
·199 lines (165 loc) · 6.22 KB
/
hubic_ftp
File metadata and controls
executable file
·199 lines (165 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python
# (c) 2013 Antoine Sirinelli <antoine@monte-stello.com>
import requests
import json
from getpass import getpass
from base64 import b64decode
import sys
from swiftclient import Connection
import cmd
import os
class FTPlike(cmd.Cmd):
prompt = '/> '
def __init__(self, connection):
cmd.Cmd.__init__(self)
self.conn = connection
self.actual_dir = None
def __get_list_containers(self):
out = {}
for c in self.conn.get_container('')[1]:
out[c['name']] = c
return out
def __get_list_objects(self, container):
out = {}
for o in self.conn.get_container(container)[1]:
out[o['name']] = o
return out
def do_ls(self, container):
if not container and self.actual_dir:
container = self.actual_dir
if container == '/':
container = None
if not container:
cont = self.__get_list_containers()
for name in sorted(cont.keys()):
print name, cont[name]['bytes'], cont[name]['count']
elif container in self.__get_list_containers().keys():
objs = self.__get_list_objects(container)
for name in sorted(objs.keys()):
print name, objs[name]['bytes'], objs[name]['content_type']
else:
print "No such container"
def complete_ls(self, text, line, begidx, endidx):
complete = self.__complete_containers(text)
return complete
def __complete_containers(self, text):
choices = sorted(self.__get_list_containers().keys())
if not text:
completion = choices
else:
completion = [c for c in choices if c.startswith(text)]
return completion
def __complete_objects(self, container, text):
choices = sorted(self.__get_list_objects(container).keys())
if not text:
completion = choices
else:
completion = [c for c in choices if c.startswith(text)]
return completion
def do_cd(self, line):
if line == '/' or line == "..":
self.actual_dir = None
self.prompt = '/> '
elif line in self.__get_list_containers().keys():
self.actual_dir = line
self.prompt = '/'+line+'> '
else:
print "No such file or directory"
def complete_cd(self, text, line, begidx, endidx):
return sorted(self.__complete_containers(text))
def do_get(self, ob):
if not self.actual_dir:
print "cd to a container first"
return
if ob in self.__get_list_objects(self.actual_dir).keys():
f=file(ob, 'wb')
info, data_bin = conn.get_object(self.actual_dir, ob, resp_chunk_size=65536)
for b in data_bin:
f.write(b)
f.close()
def complete_get(self, text, line, begidx, endidx):
if self.actual_dir:
return self.__complete_objects(self.actual_dir, text)
else:
return []
def do_put(self, line):
if not self.actual_dir:
print "Move to a container first"
return
try:
f = open(line, 'rb')
except IOError:
print "No such file"
return
self.conn.put_object(self.actual_dir, line, f)
def complete_put(self, text, line, begidx, endidx):
if not self.actual_dir:
return []
choices = sorted(os.listdir('.'))
if not text:
return choices
else:
return [f for f in choices if f.startswith(text)]
def do_rm(self, line):
if not self.actual_dir:
print "TBD"
elif line in self.__get_list_objects(self.actual_dir).keys():
self.conn.delete_object(self.actual_dir, line)
else:
print "No such file"
def complete_rm(self, text, l, b,e):
if not self.actual_dir:
return []
else:
return self.__complete_objects(self.actual_dir, text)
def do_EOF(self, line):
return True
class auth_hubic:
def __init__(self, user, passwd):
self.SessionHandler = 'https://ws.ovh.com/sessionHandler/r4/'
self.hubicws = 'https://ws.ovh.com/hubic/r5/'
r = requests.get(self.SessionHandler + 'rest.dispatcher/' + 'getAnonymousSession')
sessionId = r.json()['answer']['session']['id']
params = { 'sessionId': sessionId,
'email': user}
payload = {'params': json.dumps(params)}
r = requests.get(self.hubicws + 'rest.dispatcher/' + 'getHubics',
params=payload)
hubics = r.json()
self.hubicsId = hubics['answer'][0]['id']
params = { 'login': hubics['answer'][0]['nic'],
'password': passwd,
'context': 'hubic'}
payload = {'params': json.dumps(params)}
r = requests.get(self.SessionHandler + 'rest.dispatcher/' + 'login',
params=payload)
self.sessionId = r.json()['answer']['session']['id']
def get_credentials(self):
params = { 'sessionId': self.sessionId,
'hubicId': self.hubicsId}
payload = {'params': json.dumps(params)}
r = requests.get(self.hubicws + 'rest.dispatcher/' + 'getHubic',
params=payload)
Storage_Url = b64decode(r.json()['answer']['credentials']['username'])
Auth_Token = r.json()['answer']['credentials']['secret']
return Storage_Url, Auth_Token
def logout(self):
params = { 'sessionId': self.sessionId}
payload = {'params': json.dumps(params)}
r = requests.get(self.SessionHandler + 'rest.dispatcher/' + 'logout',
params=payload)
if len(sys.argv) != 2:
print "Please supply your email:"
print sys.argv[0] + " email"
sys.exit(1)
user = sys.argv[1]
passwd = getpass()
hubic = auth_hubic(user, passwd)
storage_url, auth_token = hubic.get_credentials()
options = {'auth_token': auth_token,
'object_storage_url': storage_url}
conn = Connection(os_options=options, auth_version=2)
FTPlike(conn).cmdloop()
hubic.logout()
#print 'OS_STORAGE_URL="'+Storage_Url+'"'
#print 'OS_AUTH_TOKEN='+Auth_Token