diff options
Diffstat (limited to 'pypaste/server/s3/__init__.py')
-rw-r--r-- | pypaste/server/s3/__init__.py | 28 |
1 files changed, 14 insertions, 14 deletions
diff --git a/pypaste/server/s3/__init__.py b/pypaste/server/s3/__init__.py index 6026fc4..23be1ee 100644 --- a/pypaste/server/s3/__init__.py +++ b/pypaste/server/s3/__init__.py @@ -16,7 +16,7 @@ import asyncio import zstandard import aiosqlite -from pypaste.server import Storage, Paste +from pypaste.server import Storage, Paste, Key from pypaste.server.s3.bucket import Bucket from dataclasses import dataclass from typing import Optional @@ -39,28 +39,28 @@ class S3(Storage): self.bucket = Bucket(endpoint, region, bucket, access_key, secret_key) async def setup(self) -> None: - await self.connection.execute("create table if not exists s3(key text)") + await self.connection.execute("create table if not exists s3(key blob)") await self.connection.commit() - async def insert(self, paste: Paste) -> None: + async def insert(self, paste: Paste, key: Key) -> None: def compress(): return zstandard.compress(paste.text.encode()) compressed = await asyncio.to_thread(compress) await self.connection.execute( - "insert into pastes values(?, ?, ?, ?)", - (paste.key, paste.dt.isoformat(), len(compressed), paste.syntax), + "insert into pastes values(?, ?, ?, ?, ?)", + (key.data, key.length, paste.dt.isoformat(), len(compressed), paste.syntax), ) try: - await self.bucket.put(paste.key, compressed) + await self.bucket.put(key.data.hex(), compressed) await self.connection.commit() except Exception as e: await self.connection.rollback() raise e - async def retrieve(self, key: str) -> Optional[Paste]: + async def retrieve(self, key: Key) -> Optional[Paste]: if not await self.exists(key): return None @@ -70,7 +70,7 @@ class S3(Storage): (dt, size, syntax) = row - data = await self.bucket.get(key) + data = await self.bucket.get(key.data.hex()) assert data is not None @@ -79,21 +79,21 @@ class S3(Storage): text = await asyncio.to_thread(decompress) - return Paste(key, dt, syntax, text) + return Paste(dt, syntax, text) - async def delete(self, key: str) -> None: - await self.connection.execute("delete from pastes where key=?", (key,)) + async def delete(self, key: Key) -> None: + await self.connection.execute("delete from pastes where key=?", (key.data,)) try: - await self.bucket.delete(key) + await self.bucket.delete(key.data.hex()) await self.connection.commit() except Exception as e: await self.connection.rollback() raise e - async def exists(self, key: str) -> bool: + async def exists(self, key: Key) -> bool: async with self.connection.execute( - "select 1 from s3 where key=?", (key,) + "select 1 from s3 where key=?", (key.data,) ) as cursor: return await cursor.fetchone() is not None |