/
store.py
62 lines (44 loc) · 1.53 KB
/
store.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
58
59
60
61
62
"""Provides two instances of a filestore.
There is not intended to be any vulnerability contained within this code. This file is provided to
make it easier to test locally without needing access to an S3 bucket.
-OOO
"""
import os
import boto3
import botocore
class NotFound(Exception):
pass
class LocalStore:
def __init__(self):
import tempfile
self.upload_directory = tempfile.mkdtemp()
def read(self, key):
filepath = os.path.join(self.upload_directory, key)
try:
with open(filepath, "rb") as fp:
return fp.read()
except FileNotFoundError:
raise NotFound
def save(self, key, data):
with open(os.path.join(self.upload_directory, key), "wb") as fp:
fp.write(data)
class S3Store:
"""Credentials grant access only to resource s3://BUCKET/* and only for:
* GetObject
* PutObject
"""
def __init__(self):
self.bucket = os.environ["BUCKET"]
self.s3 = boto3.client("s3")
def read(self, key):
try:
response = self.s3.get_object(Bucket=self.bucket, Key=key)
except botocore.exceptions.ClientError as exception:
if exception.response["ResponseMetadata"]["HTTPStatusCode"] == 403:
raise NotFound
# No other exceptions encountered during testing
return response["Body"].read()
def save(self, key, data):
self.s3.put_object(
Body=data, Bucket=self.bucket, ContentType="text/plain", Key=key
)