-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupload.py
60 lines (47 loc) · 1.46 KB
/
upload.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
import os
import sys
import json
import boto
import boto.s3.key
# arguments:
# 1: bucket key prefix
# 2: local folder
# 3: s3 credentials json file
# load credentials from a predefined file
CREDENTIALS = json.load(open(sys.argv[3]))
AWS_ACCESS_KEY_ID = CREDENTIALS['access_key']
AWS_SECRET_ACCESS_KEY = CREDENTIALS['secret_key']
BUCKET_NAME = CREDENTIALS['bucket']
def _upload(client, path, location='repo/', remote=''):
if os.path.isfile(path):
print " --- put %s to %s" % (path, location + remote)
k = boto.s3.key.Key(client)
k.key = location + remote
k.set_contents_from_filename(path)
k.make_public()
return
files_in_dir = os.listdir(path)
for f in files_in_dir:
print f
_upload(client, "%s/%s" % (path, f), location=location, remote="%s/%s" % (remote, f))
def upload(prefix='repo', local=None):
"""upload to s3 rpms.
args:
prefix - s3 prefix
local - local directory location
"""
conn = boto.connect_s3(AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY)
bucket = conn.get_bucket(BUCKET_NAME)
print "clean target location..."
bucket_location = prefix
for key in bucket.list(bucket_location):
print " --- delete " + key.key
key.delete()
if not local:
local = prefix
_upload(bucket, local, bucket_location)
def main():
upload(sys.argv[1], sys.argv[2])
if __name__ == '__main__':
main()