-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths3.py
More file actions
78 lines (61 loc) · 1.57 KB
/
Copy paths3.py
File metadata and controls
78 lines (61 loc) · 1.57 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
#!/usr/bin/env python
# Copyright (c) 2015 Nandini Taneja nandini.taneja@gmail.com
# All rights reserved.
import sys, getopt
import boto
from boto.s3.key import Key
# connect to Amazon S3
conn = boto.connect_s3()
def extract_file(fullpath):
pos = fullpath.rfind("/")
path = fullpath[pos+1:]
return path
def create_bucket(b):
# Create a bucket
# Catch exception if the bucket can not be created or already exists
print "Create a bucket:", b, " \n"
try:
conn.create_bucket(b)
except S3CreateError as ce:
print "Exception creating bucket: ", ce.errno
else:
print "Bucket created\n"
def upload_file(b,f):
# Upload a file in the created bucket
b = conn.get_bucket(b)
k = Key(b)
filename = extract_file(f)
k.key = filename
k.set_contents_from_filename(f)
print "Uploaded file ", filename, "\n"
def delete_bucket(b):
# Delete the bucket
#conn.delete_bucket(b)
print "Bucket", b, " deleted\n"
def main(argv):
bucket = ''
filename = ''
if len(argv) < 2:
print 's3.py <bucketname> <absolute filename>'
sys.exit(2)
bucket = argv[0]
filename = argv[1]
print "Bucket to create", bucket
print "File to load ", filename
# List all avaialble buckets in your account
buckets = conn.get_all_buckets()
length = len(buckets)
# Total buckets
print "Total: " , length , "buckets\n"
print "Bucket Names @Amazon AWS and Contents\n"
for b in buckets:
print b.name
print "\n"
objects_in_b = b.list()
for o in objects_in_b:
print o.name
create_bucket(bucket)
upload_file(bucket,filename)
#delete_bucket(bucket)
if __name__ == "__main__":
main(sys.argv[1:])