import os.path import boto3 from botocore.config import Config from concurrent.futures import ThreadPoolExecutor class S3: def __init__(self, bucket_name,instance=None): self.bucket_name = bucket_name if instance is None: endpoint = None if "AWS_ENDPOINT_S3" in os.environ: s3_config = Config( s3={'addressing_style': 'virtual'} ) endpoint = os.environ["AWS_ENDPOINT_S3"] self.s3 = boto3.resource('s3', endpoint_url=endpoint, config=s3_config) else: self.s3 = boto3.resource('s3') else: self.s3 = instance def get_file(self, filename): key = filename print('\nget object:', filename) return self.s3.Object(self.bucket_name, key).get()['Body'].read() def get_file_list(self, path): bucket = self.s3.Bucket(self.bucket_name) prefix = path return [obj.key for obj in bucket.objects.filter(Prefix=prefix)] def file_exists(self, path): bucket = self.s3.Bucket(self.bucket_name) prefix = path return any([obj.key == path for obj in bucket.objects.filter(Prefix=prefix)]) def get_files(max_thread,bucket_name,filelist): resultfiles =[] with ThreadPoolExecutor(max_workers=max_thread) as executor: # The order of the exector results is the same as the order of the input, so we can guarantee the chronological order of the returned slowlogs res = executor.map(S3.get_file, [S3(bucket_name,boto3.session.Session().resource('s3'))] * len(filelist), filelist) for one in res: resultfiles.append(one) return resultfiles