-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenge3.py
72 lines (62 loc) · 2.01 KB
/
challenge3.py
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
import os
import pyrax
import pyrax.exceptions as e
import sys, getopt
import time
from time import sleep
def main():
"""Takes local directory and cloud files container as arguments.
Uploads all files in the local directory to the container
"""
# Get local upload dir and remote container from command line.
local_dir = ''
container = ''
try:
opts, args = getopt.getopt(sys.argv[1:],"hd:c:",["local_dir=","container="])
except getopt.GetoptError as err:
print 'Usage: challenge3.py -d <local_dir> -c <container>'
sys.exit(2)
if len(sys.argv) < 3:
print 'Usage: challenge3.py -d <local_dir> -c <container>'
sys.exit()
for opt, arg in opts:
if opt == '-h':
print 'Usage: challenge3.py -d <local_dir> -c <container>'
sys.exit()
elif opt == "":
print 'Usage: challenge3.py -d <local_dir> -c <container>'
sys.exit()
elif opt in ("-d", "--local_dir"):
local_dir = arg
elif opt in ("-c", "--container"):
container = arg
# Path to credential file.
credential_file = os.path.expanduser("~/.rackspace_cloud_credentials")
print "Authenticating"
try:
pyrax.set_credential_file(credential_file)
except e.AuthenticationFailed:
print "Authentication Failed: The file does not contain valid credendials"
sys.exit()
except e.FileNotFound:
print "Authentication file %s not found" % credential_file
sys.exit()
print "Authenticated Successfully as %s" % pyrax.identity.username
cf = pyrax.cloudfiles
# Do the upload
try:
print "Uploading contents of %s to container %s" % (local_dir, container)
upload_key, total_bytes = cf.upload_folder(local_dir, container=container)
except e.FolderNotFound:
print "ERROR: Local upload directory %s does not exist" % local_dir
sys.exit()
uploaded = 0
while uploaded < total_bytes:
uploaded = pyrax.cloudfiles.get_uploaded(upload_key)
sys.stdout.write("\r" + " Progress: %4.2f%%" % ((uploaded * 100.0) / total_bytes))
sys.stdout.flush()
time.sleep(1)
print
print "Upload complete"
if __name__ == "__main__":
main()