-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy paths3.py
57 lines (41 loc) · 1.52 KB
/
s3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import boto3
import re
import yaml
class Client:
def __init__(self):
with open("config.yml", 'r') as ymlfile:
config = yaml.load(ymlfile)['s3']
self.client = boto3.client(
's3',
region_name=config['region'],
aws_access_key_id=config['access_key'],
aws_secret_access_key=config['secret']
)
self.bucket = config['bucket']
def upload(self, source, destination=None):
if destination is None:
destination = source
self.client.upload_file(source, self.bucket, destination)
def download(self, source, destination=None):
if destination is None:
destination = source
self.client.download_file(self.bucket, source, destination)
def list_items(self, prefix=None):
paginator = self.client.get_paginator('list_objects_v2')
page_iterator = paginator.paginate(
Bucket=self.bucket, Prefix=prefix)
items = []
for page in page_iterator:
if "Contents" in page:
for item in page["Contents"]:
key = item["Key"]
items.append(key)
return items
def list_slices(self):
return self.list_items('slices/')
def list_audio_files(self):
return self.list_items('audio/')
def list_existing_spectrograms(self):
items = self.list_items('spectrograms/')
regex = r'/(?<=/)(.*)(?=.png)'
return [re.search(regex, key).group(1) for key in items]