blob: a309a12c5a13be92670c2a73b04d642d93888ddd [file] [log] [blame]
Attila Fazekasa23f5002012-10-23 19:32:45 +02001# 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 Taylorb2ca5ca2013-04-28 18:00:21 -070018import contextlib
Attila Fazekasa23f5002012-10-23 19:32:45 +020019import os
20import re
Attila Fazekasa23f5002012-10-23 19:32:45 +020021
Matthew Treinisha83a16e2012-12-07 13:44:02 -050022import boto
Monty Taylorb2ca5ca2013-04-28 18:00:21 -070023import boto.s3.key
Attila Fazekasa23f5002012-10-23 19:32:45 +020024
Mitsuhiko Yamazaki46818aa2013-04-18 17:49:17 +090025from tempest.common import log as logging
26
Attila Fazekasa23f5002012-10-23 19:32:45 +020027LOG = logging.getLogger(__name__)
28
29
30def s3_upload_dir(bucket, path, prefix="", connection_data=None):
31 if isinstance(bucket, basestring):
Monty Taylorb2ca5ca2013-04-28 18:00:21 -070032 with contextlib.closing(boto.connect_s3(**connection_data)) as conn:
Attila Fazekasa23f5002012-10-23 19:32:45 +020033 bucket = conn.lookup(bucket)
34 for root, dirs, files in os.walk(path):
35 for fil in files:
Monty Taylorb2ca5ca2013-04-28 18:00:21 -070036 with contextlib.closing(boto.s3.key.Key(bucket)) as key:
Attila Fazekasa23f5002012-10-23 19:32:45 +020037 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)