"""COPIED FROM garcon-contrib 0.4.4rc0.""" from boto.s3.connection import Bucket from boto.s3.connection import S3Connection from garcon import task from feed_sender.garcon_contrib.aws.utils import garcon_s3 as s3_utils @task.decorate() def remove_files_from_path(activity, path, return_deleted_files): """Removes all the objects from a given bucket path. Deleted objects include anything that starts with the path. Args: activity (ActivityWorker): the swf activity worker. path (str): S3 URL formatted like 's3://bucket_name/folder1/folder2/' return_deleted_files (bool): whether return deleted file names or not Return: dict: One element dict with a list of deleted files. """ bucket_name, bucket_path = s3_utils.extract_bucket_path(path) b = Bucket(S3Connection(), bucket_name) file_keys = [key.name for key in b.list(prefix=bucket_path)] result = b.delete_keys(file_keys) if not return_deleted_files: return_deleted_files = False if len(result.errors) > 0: activity.logger.error( 'Error while deleting objects from {path}'.format(path=path)) raise Exception('Not all files in s3 have been removed.') if len(result.deleted) > 0: activity.logger.info( 'Deleted all S3 objects from {path}'.format(path=path)) if return_deleted_files is True: return { 's3.files_removed': file_keys } return {}