From be82114f8302ffedecf950c6ca9fecf01ece5573 Mon Sep 17 00:00:00 2001 From: Julius Unverfehrt Date: Fri, 12 Aug 2022 13:35:12 +0200 Subject: [PATCH] Pull request #49: Add exists to storage Merge in RR/pyinfra from add-exists-to-storage to master Squashed commit of the following: commit 48d5e1c9e103702bfebfc115e811576514e115c3 Author: Julius Unverfehrt Date: Fri Aug 12 13:32:40 2022 +0200 refactor commit 711d2c8dbf7c78e26133e3ea3a57670fe829059b Author: Julius Unverfehrt Date: Fri Aug 12 11:45:42 2022 +0200 add method to check if objects exists for azure and s3 --- pyinfra/storage/adapters/azure.py | 5 +++++ pyinfra/storage/adapters/s3.py | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/pyinfra/storage/adapters/azure.py b/pyinfra/storage/adapters/azure.py index 9f451ec..f954e38 100644 --- a/pyinfra/storage/adapters/azure.py +++ b/pyinfra/storage/adapters/azure.py @@ -37,6 +37,11 @@ class AzureStorageAdapter(object): blob_client = container_client.get_blob_client(object_name) blob_client.upload_blob(data, overwrite=True) + def exists(self, bucket_name, object_name): + container_client = self.__provide_container_client(bucket_name) + blob_client = container_client.get_blob_client(object_name) + return blob_client.exists() + @retry(tries=3, delay=5, jitter=(1, 3)) def get_object(self, bucket_name, object_name): logger.debug(f"Downloading '{object_name}'...") diff --git a/pyinfra/storage/adapters/s3.py b/pyinfra/storage/adapters/s3.py index 0dc430f..16e20ae 100644 --- a/pyinfra/storage/adapters/s3.py +++ b/pyinfra/storage/adapters/s3.py @@ -42,6 +42,13 @@ class S3StorageAdapter(object): data = io.BytesIO(data) self._client.put_object(bucket_name, object_name, data, length=data.getbuffer().nbytes) + def exists(self, bucket_name, object_name): + try: + self._client.stat_object(bucket_name, object_name) + return True + except Exception: + return False + @retry(tries=3, delay=5, jitter=(1, 3)) def get_object(self, bucket_name, object_name): logger.debug(f"Downloading '{object_name}'...")