Path: blob/develop/tests/integration/customizations/s3/__init__.py
1567 views
# Copyright 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.1#2# Licensed under the Apache License, Version 2.0 (the "License"). You3# may not use this file except in compliance with the License. A copy of4# the License is located at5#6# http://aws.amazon.com/apache2.0/7#8# or in the "license" file accompanying this file. This file is9# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF10# ANY KIND, either express or implied. See the License for the specific11# language governing permissions and limitations under the License.12from botocore.exceptions import ClientError13from awscli.testutils import create_bucket141516def make_s3_files(session, key1='text1.txt', key2='text2.txt', size=None):17"""18Creates a randomly generated bucket in s3 with the files text1.txt and19another_directory/text2.txt inside. The directory is manually created20as it tests the ability to handle directories when generating s3 files.21"""22region = 'us-west-2'23bucket = create_bucket(session)2425if size:26string1 = "*" * size27string2 = string128else:29string1 = "This is a test."30string2 = "This is another test."3132client = session.create_client('s3', region_name=region)33client.put_object(Bucket=bucket, Key=key1, Body=string1)34if key2 is not None:35client.put_object(Bucket=bucket, Key='another_directory/')36client.put_object(Bucket=bucket, Key='another_directory/%s' % key2,37Body=string2)38return bucket394041def s3_cleanup(bucket, session):42"""43Function to cleanup generated s3 bucket and files.44"""45region = 'us-west-2'46client = session.create_client('s3', region_name=region)47try:48client.head_bucket(Bucket=bucket)49except ClientError:50return51response = client.list_objects(Bucket=bucket)52contents = response.get('Contents', {})53keys = [content['Key'] for content in contents]54for key in keys:55client.delete_object(Bucket=bucket, Key=key)56client.delete_bucket(Bucket=bucket)575859