in gym_recording/storage_s3.py [0:0]
def download_recording(s3url):
"""
Download the recording saved in s3url to a directory in /tmp. It'll reuse the cached
recording if it's already downloaded. Returns a directory
"""
s3 = boto3.resource('s3')
m = re.match(r's3://([\w\-]+)/([\w\-\.\/]+)', s3url)
if not m:
raise Exception('Invalid s3 URL')
bucket = m.group(1)
bucketdir = m.group(2)
directory = os.path.join('/tmp', bucketdir)
if not os.access(directory, os.R_OK):
directory_tmp = directory + '.tmp{}'.format(os.getpid())
os.makedirs(directory_tmp)
b = s3.Bucket(bucket)
for fn in b.objects.filter(Prefix=bucketdir+'/'):
logger.info('Download %s to %s', fn.key, directory)
localfn = os.path.join(directory_tmp, os.path.basename(fn.key))
b.download_file(Key=fn.key, Filename=localfn)
os.rename(directory_tmp, directory)
return directory