upload-s3.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # Licensed to Elasticsearch under one or more contributor
  2. # license agreements. See the NOTICE file distributed with
  3. # this work for additional information regarding copyright
  4. # ownership. Elasticsearch licenses this file to you under
  5. # the Apache License, Version 2.0 (the "License"); you may
  6. # not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on
  13. # an 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  14. # either express or implied. See the License for the specific
  15. # language governing permissions and limitations under the License.
  16. import os
  17. import sys
  18. import argparse
  19. try:
  20. import boto.s3
  21. except:
  22. raise RuntimeError("""
  23. S3 upload requires boto to be installed
  24. Use one of:
  25. 'pip install -U boto'
  26. 'apt-get install python-boto'
  27. 'easy_install boto'
  28. """)
  29. import boto.s3
  30. def list_buckets(conn):
  31. return conn.get_all_buckets()
  32. def upload_s3(conn, path, key, file, bucket):
  33. print 'Uploading %s to Amazon S3 bucket %s/%s' % \
  34. (file, bucket, os.path.join(path, key))
  35. def percent_cb(complete, total):
  36. sys.stdout.write('.')
  37. sys.stdout.flush()
  38. bucket = conn.create_bucket(bucket)
  39. k = bucket.new_key(os.path.join(path, key))
  40. k.set_contents_from_filename(file, cb=percent_cb, num_cb=100)
  41. if __name__ == '__main__':
  42. parser = argparse.ArgumentParser(description='Uploads files to Amazon S3')
  43. parser.add_argument('--file', '-f', metavar='path to file',
  44. help='the branch to release from', required=True)
  45. parser.add_argument('--bucket', '-b', metavar='B42', default='download.elasticsearch.org',
  46. help='The S3 Bucket to upload to')
  47. parser.add_argument('--path', '-p', metavar='elasticsearch/elasticsearch', default='elasticsearch/elasticsearch',
  48. help='The key path to use')
  49. parser.add_argument('--key', '-k', metavar='key', default=None,
  50. help='The key - uses the file name as default key')
  51. args = parser.parse_args()
  52. if args.key:
  53. key = args.key
  54. else:
  55. key = os.path.basename(args.file)
  56. connection = boto.connect_s3()
  57. upload_s3(connection, args.path, key, args.file, args.bucket);