Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
| 3 | # Copyright 2012 OpenStack, LLC |
| 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
Monty Taylor | b2ca5ca | 2013-04-28 18:00:21 -0700 | [diff] [blame] | 18 | import contextlib |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 19 | import os |
| 20 | import re |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 21 | |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 22 | import boto |
Monty Taylor | b2ca5ca | 2013-04-28 18:00:21 -0700 | [diff] [blame] | 23 | import boto.s3.key |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 24 | |
Mitsuhiko Yamazaki | 46818aa | 2013-04-18 17:49:17 +0900 | [diff] [blame^] | 25 | from tempest.common import log as logging |
| 26 | |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 27 | LOG = logging.getLogger(__name__) |
| 28 | |
| 29 | |
| 30 | def s3_upload_dir(bucket, path, prefix="", connection_data=None): |
| 31 | if isinstance(bucket, basestring): |
Monty Taylor | b2ca5ca | 2013-04-28 18:00:21 -0700 | [diff] [blame] | 32 | with contextlib.closing(boto.connect_s3(**connection_data)) as conn: |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 33 | bucket = conn.lookup(bucket) |
| 34 | for root, dirs, files in os.walk(path): |
| 35 | for fil in files: |
Monty Taylor | b2ca5ca | 2013-04-28 18:00:21 -0700 | [diff] [blame] | 36 | with contextlib.closing(boto.s3.key.Key(bucket)) as key: |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 37 | source = root + os.sep + fil |
| 38 | target = re.sub("^" + re.escape(path) + "?/", prefix, source) |
| 39 | if os.sep != '/': |
| 40 | target = re.sub(re.escape(os.sep), '/', target) |
| 41 | key.key = target |
| 42 | LOG.info("Uploading %s to %s/%s", source, bucket.name, target) |
| 43 | key.set_contents_from_filename(source) |