Skip to content

Commit

Permalink
API to delete blob
Browse files Browse the repository at this point in the history
  • Loading branch information
nitinprakash96 committed Aug 3, 2024
1 parent 664f623 commit 44e22ff
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions azure-blob-storage/azure-blob-storage.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ library
import: common-options
exposed-modules: Azure.Blob.Types
Azure.GetBlob
Azure.DeleteBlob
Azure.PutBlob
build-depends: azure-auth
, bytestring
Expand Down
91 changes: 91 additions & 0 deletions azure-blob-storage/src/Azure/DeleteBlob.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}

module Azure.DeleteBlob
( deleteBlobObject
, deleteBlobObjectEither
) where

import Azure.Auth (defaultAzureCredential)
import Azure.Blob.Types (AccountName (..), BlobName (..), ContainerName (..))
import Data.Data (Proxy (..))
import Data.Text (Text)
import GHC.Generics (Generic)
import Network.HTTP.Client.TLS (newTlsManager)
import Servant.API
import Servant.Client (BaseUrl (..), ClientM, Scheme (..), client, mkClientEnv, runClientM)
import UnliftIO (MonadIO (..), throwString)

import qualified Azure.Types as Auth
import qualified Data.Text as Text

blobStorageResourceUrl :: Text
blobStorageResourceUrl = "https://storage.azure.com/"

deleteBlobObject ::
MonadIO m =>
DeleteBlob ->
m ()
deleteBlobObject getBlobReq = do
res <- liftIO $ deleteBlobObjectEither getBlobReq
case res of
Left err ->
throwString $ show err
Right _ ->
pure ()

deleteBlobObjectEither ::
MonadIO m =>
DeleteBlob ->
m (Either Text ())
deleteBlobObjectEither getBlobReq = do
res <-
liftIO $
callDeleteBlobClient deleteBlobObjectApi getBlobReq
pure $
case res of
Right _ -> Right ()
Left err -> Left err

data DeleteBlob = DeleteBlob
{ accountName :: !AccountName
, containerName :: !ContainerName
, blobName :: !BlobName
, tokenStore :: !Auth.Token
}
deriving stock (Eq, Generic)

type DeleteBlobApi =
Capture "container-name" ContainerName
:> Capture "blob-name" BlobName
:> Header' '[Required, Strict] "Authorization" Text
:> Header' '[Required, Strict] "x-ms-version" Text
:> DeleteNoContent

deleteBlobObjectApi :: ContainerName -> BlobName -> Text -> Text -> ClientM NoContent
deleteBlobObjectApi = client (Proxy @DeleteBlobApi)

callDeleteBlobClient ::
(ContainerName -> BlobName -> Text -> Text -> ClientM NoContent) ->
DeleteBlob ->
IO (Either Text ())
callDeleteBlobClient action DeleteBlob{accountName, containerName, blobName, tokenStore} = do
Auth.AccessToken{atAccessToken} <- liftIO $ defaultAzureCredential Nothing blobStorageResourceUrl tokenStore
manager <- liftIO newTlsManager
res <-
liftIO $
runClientM
(action containerName blobName ("Bearer " <> atAccessToken) "2020-04-08")
(mkClientEnv manager $ BaseUrl Https mkHostUrl 443 "")
pure $ case res of
Left err -> do
Left . Text.pack $ show err
Right _ -> do
pure ()
where
mkHostUrl = Text.unpack (unAccountName accountName) <> ".blob.core.windows.net"

0 comments on commit 44e22ff

Please sign in to comment.