-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.py
62 lines (44 loc) · 1.72 KB
/
storage.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
import os, time, logging
from azure.storage.blob import (
BlobServiceClient,
BlobClient,
ContainerClient,
ContentSettings,
)
logger = logging.getLogger("listhub-data-capture")
class BlobName:
def __init__(self, listing_key, media_key):
self.media_key = media_key
self.listing_key = listing_key
def __call__(self):
return f"{self.listing_key}/{self.media_key}.jpg"
class MetaData:
def __init__(self, listing_key, media_key):
self.listing_key = listing_key
self.media_key = media_key
def __call__(self):
return {"ListingKey": self.listing_key, "MediaKey": self.media_key}
class StorageGateway:
def __init__(self, blob_service_client, container_name):
self.blob_service_client = blob_service_client
self.container_name = container_name
@classmethod
def create(cls):
connect_str = os.getenv("AZURE_STORAGE_CONNECTION_STRING")
container_name = os.getenv("AZURE_CONTAINER_NAME")
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
return cls(blob_service_client, container_name)
def save(self, listing_key, media_key, content):
blob_name = BlobName(listing_key, media_key)
metadata = MetaData(listing_key, media_key)
blob_client = self.blob_service_client.get_blob_client(
container=self.container_name, blob=blob_name()
)
# Assume everything is an image for now
settings = ContentSettings(content_type="image/jpeg")
blob_client.upload_blob(
content, overwrite=True, content_settings=settings, metadata=metadata()
)
logger.info(
f"storage.py Uploaded {media_key}"
)