git.iko.soy/typeable/minio-hs
on master help sign in
Add bucketExists and headBucket APIs (#42)

Also fixed examples to work with lts-8.5

commitb30beecd529f087bdae5139f6a0b0917d2ff6566 authorKrishnan Parthasarathi <krisis@users.noreply.github.com> dateThu 23 Mar 2017, 15:57 (10 years ago) parent3281f2a
17 files changed+115−18
README.md+4−3 docs/API.md+14−1 examples/BucketExists.hs+43 examples/CopyObject.hs+1−1 examples/FileUploader.hs+3−2 examples/GetObject.hs+1−1 examples/HeadObject.hs+1−1 examples/ListBuckets.hs+1−1 examples/ListIncompleteUploads.hs+1−1 examples/ListObjects.hs+1−1 examples/Makebucket.hs+1−1 examples/PutObject.hs+1−1 examples/RemoveObject.hs+3−2 examples/Removebucket.hs+1−1 src/Network/Minio.hs+5 src/Network/Minio/S3API.hs+32−1 test/LiveServer.hs+2
README.md+4−3
@@ -46,17 +46,18 @@ stack haddock
4646### FileUploader.hs
4747``` haskell
4848#!/usr/bin/env stack
49−-- stack --resolver lts-6.27 runghc --package minio-hs --package optparse-applicative --package filepath
49+-- stack --resolver lts-8.5 runghc --package minio-hs --package optparse-applicative --package filepath
5050
5151{-# Language OverloadedStrings, ScopedTypeVariables #-}
5252import Network.Minio
5353
5454import Control.Monad.Catch (catchIf)
5555import Control.Monad.IO.Class (liftIO)
56+import Data.Monoid ((<>))
57+import Data.Text (pack)
5658import Options.Applicative
5759import Prelude
5860import System.FilePath.Posix
59−import Data.Text (pack)
6061
6162-- | The following example uses minio's play server at
6263-- https://play.minio.io:9000. The endpoint and associated
@@ -102,7 +103,7 @@ main = do
102103 Right () -> putStrLn "file upload succeeded."
103104```
104105
105−### Run fileuploader
106+### Run FileUploader
106107
107108``` sh
108109./FileUploader.hs "path/to/my/file"
docs/API.md+14−1
@@ -27,7 +27,7 @@ awsCI { connectAccesskey = "your-access-key"
2727|[`removeBucket`](#removeBucket)|[`fGetObject`](#fGetObject)|
2828|[`listObjects`](#listObjects)|[`fPutObject`](#fPutObject)|
2929|[`listIncompleteUploads`](#listIncompleteUploads)|[`copyObject`](#copyObject)|
30−||[`removeObject`](#removeObject)|
30+|[`bucketExists`](#bucketExists)|[`removeObject`](#removeObject)|
3131
3232## 1. Connecting and running operations on the storage service
3333
@@ -587,6 +587,19 @@ main = do
587587 Right _ -> putStrLn "Removed object successfully"
588588```
589589
590+<a name="BucketExists"></a>
591+### bucketExists :: Bucket -> Minio Bool
592+Checks if a bucket exists.
593+
594+__Parameters__
595+
596+In the expression `bucketExists bucketName` the parameters are:
597+
598+|Param |Type |Description |
599+|:---|:---| :---|
600+| `bucketName` | _Bucket_ (alias for `Text`) | Name of the bucket |
601+
602+
590603<!-- ## 4. Presigned operations -->
591604
592605<!-- TODO -->
examples/BucketExists.hsnew file+43
@@ -0,0 +1,43 @@
1+#!/usr/bin/env stack
2+-- stack --resolver lts-8.5 runghc --package minio-hs
3+
4+--
5+-- Minio Haskell SDK, (C) 2017 Minio, Inc.
6+--
7+-- Licensed under the Apache License, Version 2.0 (the "License");
8+-- you may not use this file except in compliance with the License.
9+-- You may obtain a copy of the License at
10+--
11+-- http://www.apache.org/licenses/LICENSE-2.0
12+--
13+-- Unless required by applicable law or agreed to in writing, software
14+-- distributed under the License is distributed on an "AS IS" BASIS,
15+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+-- See the License for the specific language governing permissions and
17+-- limitations under the License.
18+--
19+
20+{-# Language OverloadedStrings #-}
21+import Network.Minio
22+
23+import Control.Monad.IO.Class (liftIO)
24+import Prelude
25+
26+-- | The following example uses minio's play server at
27+-- https://play.minio.io:9000. The endpoint and associated
28+-- credentials are provided via the libary constant,
29+--
30+-- > minioPlayCI :: ConnectInfo
31+--
32+
33+main :: IO ()
34+main = do
35+ let bucket = "missingbucket"
36+
37+ res1 <- runResourceT $ runMinio minioPlayCI $ do
38+ foundBucket <- bucketExists bucket
39+ liftIO $ putStrLn $ "Does " ++ show bucket ++ " exist? - " ++ show foundBucket
40+
41+ case res1 of
42+ Left e -> putStrLn $ "bucketExists failed." ++ (show e)
43+ Right () -> return ()
examples/CopyObject.hs+1−1
@@ -1,5 +1,5 @@
11#!/usr/bin/env stack
2−-- stack --resolver lts-6.27 runghc --package minio-hs
2+-- stack --resolver lts-8.5 runghc --package minio-hs
33
44--
55-- Minio Haskell SDK, (C) 2017 Minio, Inc.
examples/FileUploader.hs+3−2
@@ -1,5 +1,5 @@
11#!/usr/bin/env stack
2−-- stack --resolver lts-6.27 runghc --package minio-hs --package optparse-applicative --package filepath
2+-- stack --resolver lts-8.5 runghc --package minio-hs --package optparse-applicative --package filepath
33
44--
55-- Minio Haskell SDK, (C) 2017 Minio, Inc.
@@ -23,10 +23,11 @@ import Network.Minio
2323
2424import Control.Monad.Catch (catchIf)
2525import Control.Monad.IO.Class (liftIO)
26+import Data.Monoid ((<>))
27+import Data.Text (pack)
2628import Options.Applicative
2729import Prelude
2830import System.FilePath.Posix
29−import Data.Text (pack)
3031
3132-- | The following example uses minio's play server at
3233-- https://play.minio.io:9000. The endpoint and associated
examples/GetObject.hs+1−1
@@ -1,5 +1,5 @@
11#!/usr/bin/env stack
2−-- stack --resolver lts-6.27 runghc --package minio-hs
2+-- stack --resolver lts-8.5 runghc --package minio-hs
33
44--
55-- Minio Haskell SDK, (C) 2017 Minio, Inc.
examples/HeadObject.hs+1−1
@@ -1,5 +1,5 @@
11#!/usr/bin/env stack
2−-- stack --resolver lts-6.27 runghc --package minio-hs
2+-- stack --resolver lts-8.5 runghc --package minio-hs
33
44--
55-- Minio Haskell SDK, (C) 2017 Minio, Inc.
examples/ListBuckets.hs+1−1
@@ -1,5 +1,5 @@
11#!/usr/bin/env stack
2−-- stack --resolver lts-6.27 runghc --package minio-hs
2+-- stack --resolver lts-8.5 runghc --package minio-hs
33
44--
55-- Minio Haskell SDK, (C) 2017 Minio, Inc.
examples/ListIncompleteUploads.hs+1−1
@@ -1,5 +1,5 @@
11#!/usr/bin/env stack
2−-- stack --resolver lts-6.27 runghc --package minio-hs
2+-- stack --resolver lts-8.5 runghc --package minio-hs
33
44--
55-- Minio Haskell SDK, (C) 2017 Minio, Inc.
examples/ListObjects.hs+1−1
@@ -1,5 +1,5 @@
11#!/usr/bin/env stack
2−-- stack --resolver lts-6.27 runghc --package minio-hs
2+-- stack --resolver lts-8.5 runghc --package minio-hs
33
44--
55-- Minio Haskell SDK, (C) 2017 Minio, Inc.
examples/Makebucket.hs+1−1
@@ -1,5 +1,5 @@
11#!/usr/bin/env stack
2−-- stack --resolver lts-6.27 runghc --package minio-hs
2+-- stack --resolver lts-8.5 runghc --package minio-hs
33
44--
55-- Minio Haskell SDK, (C) 2017 Minio, Inc.
examples/PutObject.hs+1−1
@@ -1,5 +1,5 @@
11#!/usr/bin/env stack
2−-- stack --resolver lts-6.27 runghc --package minio-hs
2+-- stack --resolver lts-8.5 runghc --package minio-hs
33
44--
55-- Minio Haskell SDK, (C) 2017 Minio, Inc.
examples/RemoveObject.hs+3−2
@@ -1,5 +1,5 @@
11#!/usr/bin/env stack
2−-- stack --resolver lts-6.27 runghc --package minio-hs
2+-- stack --resolver lts-8.5 runghc --package minio-hs
33
44--
55-- Minio Haskell SDK, (C) 2017 Minio, Inc.
@@ -20,6 +20,7 @@
2020{-# Language OverloadedStrings #-}
2121
2222import Network.Minio
23+import Prelude
2324
2425main :: IO ()
2526main = do
@@ -31,5 +32,5 @@ main = do
3132 removeObject bucket object
3233
3334 case res of
34− Left e -> putStrLn $ "Failed to remove " ++ show bucket ++ "/" ++ show object
35+ Left _ -> putStrLn $ "Failed to remove " ++ show bucket ++ "/" ++ show object
3536 Right _ -> putStrLn "Removed object successfully"
examples/Removebucket.hs+1−1
@@ -1,5 +1,5 @@
11#!/usr/bin/env stack
2−-- stack --resolver lts-6.27 runghc --package minio-hs
2+-- stack --resolver lts-8.5 runghc --package minio-hs
33
44--
55-- Minio Haskell SDK, (C) 2017 Minio, Inc.
src/Network/Minio.hs+5
@@ -53,6 +53,7 @@ module Network.Minio
5353 ----------------------
5454 , listBuckets
5555 , getLocation
56+ , bucketExists
5657 , makeBucket
5758 , removeBucket
5859
@@ -149,3 +150,7 @@ removeBucket :: Bucket -> Minio ()
149150removeBucket bucket = do
150151 deleteBucket bucket
151152 modify (Map.delete bucket)
153+
154+-- | Query the object store if a given bucket is present.
155+bucketExists :: Bucket -> Minio Bool
156+bucketExists = headBucket
src/Network/Minio/S3API.hs+32−1
@@ -28,6 +28,8 @@ module Network.Minio.S3API
2828 , ListObjectsResult
2929 , listObjects'
3030
31+ -- * Retrieving buckets
32+ , headBucket
3133 -- * Retrieving objects
3234 -----------------------
3335 , getObject'
@@ -64,12 +66,14 @@ module Network.Minio.S3API
6466
6567 ) where
6668
69+import Control.Monad.Catch (catches, Handler(..))
6770import qualified Data.Conduit as C
6871import Data.Default (def)
6972import qualified Network.HTTP.Conduit as NC
7073import qualified Network.HTTP.Types as HT
74+import Network.HTTP.Types.Status (status404)
7175
72−import Lib.Prelude
76+import Lib.Prelude hiding (catches)
7377
7478import Network.Minio.API
7579import Network.Minio.Data
@@ -325,3 +329,30 @@ headObject bucket object = do
325329
326330 maybe (throwM MErrVInvalidObjectInfoResponse) return $
327331 ObjectInfo <$> Just object <*> modTime <*> etag <*> size
332+
333+
334+
335+-- | Query the object store if a given bucket exists.
336+headBucket :: Bucket -> Minio Bool
337+headBucket bucket = headBucketEx `catches`
338+ [ Handler handleNoSuchBucket
339+ , Handler handleStatus404
340+ ]
341+
342+ where
343+ handleNoSuchBucket :: ServiceErr -> Minio Bool
344+ handleNoSuchBucket e | e == NoSuchBucket = return False
345+ | otherwise = throwM e
346+
347+ handleStatus404 :: NC.HttpException -> Minio Bool
348+ handleStatus404 e@(NC.HttpExceptionRequest _ (NC.StatusCodeException res _)) =
349+ if NC.responseStatus res == status404
350+ then return False
351+ else throwM e
352+ handleStatus404 e = throwM e
353+
354+ headBucketEx = do
355+ resp <- executeRequest $ def { riMethod = HT.methodHead
356+ , riBucket = Just bucket
357+ }
358+ return $ (NC.responseStatus resp) == HT.ok200
test/LiveServer.hs+2
@@ -85,6 +85,8 @@ funTestWithBucket t minioTest = testCaseSteps t $ \step -> do
8585 connInfo <- maybe minioPlayCI (const def) <$> lookupEnv "MINIO_LOCAL"
8686 ret <- runResourceT $ runMinio connInfo $ do
8787 liftStep $ "Creating bucket for test - " ++ t
88+ foundBucket <- bucketExists b
89+ liftIO $ foundBucket @?= False
8890 makeBucket b def
8991 minioTest liftStep b
9092 deleteBucket b