-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3hcp.py
52 lines (42 loc) · 1.66 KB
/
s3hcp.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
import argparse
import os
import glob
import hashlib
import boto3
def main():
parser = argparse.ArgumentParser(description='Read in a file or set of files, and return the result.', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('path', nargs='+', help='Path of a file or a folder of files')
parser.add_argument('-e', '--extension', default='', help='File extension to filter by')
parser.add_argument('-b', '--bucket', default='', help='Destination Bucket, eg. -b mybucket')
args = parser.parse_args()
path = args.path
if path == '':
print ('No bucket specified.')
exit(1)
else:
bucket = args.bucket
print ('Destination bucket: ',bucket)
s3 = boto3.resource( 's3' )
indexfile = open('index.csv', 'w')
indexfile.write('FileName, HFLocation/n')
files = set()
for f in path:
if os.path.isdir(f):
print (f, ' is a folder, skipped.')
else:
try:
hash_prefix = hashlib.md5(open(f, 'rb').read()).hexdigest()
except IOError as e:
errno, strerror = e.args
print("I/O error({0}): {1}".format(errno,strerror))
# e can be printed directly without using .args:
# print(e)
filehash = "/".join([hash_prefix + '/' + f])
print(f)
s3.Object(bucket, filehash).put(Body=open(f, 'rb'))
ContentsIndex = f + ',' + filehash + '\n'
indexfile.write(ContentsIndex)
print('Writing index file.')
s3.Object(bucket, 'index.csv').put(Body=open('index.csv', 'rb'))
if __name__ == '__main__':
main()