From de97837020945462719217f1a564451a8c16e9bc Mon Sep 17 00:00:00 2001 From: Krishnan Parthasarathi Date: Thu, 2 Mar 2017 16:49:27 +0530 Subject: [PATCH] Add removeObject function (#22) * Add an example * Fix minor issues in API.md --- docs/API.md | 56 +++++++++++++++++++++++++++++++++++----- examples/RemoveObject.hs | 35 +++++++++++++++++++++++++ src/Network/Minio.hs | 15 +++++++---- test/LiveServer.hs | 20 +++++++------- 4 files changed, 104 insertions(+), 22 deletions(-) create mode 100755 examples/RemoveObject.hs diff --git a/docs/API.md b/docs/API.md index 69ea1c5..4915023 100644 --- a/docs/API.md +++ b/docs/API.md @@ -120,6 +120,8 @@ The `runMinio` function performs the provided action in the `Minio` monad and returns a `ResourceT IO (Either MinioErr a)` value: ``` haskell +{-# Language OverloadedStrings #-} + import Network.Minio main :: IO () @@ -180,6 +182,7 @@ __Example__ ``` haskell {-# Language OverloadedStrings #-} + main :: IO () main = do res <- runResourceT $ runMinio minioPlayCI $ do @@ -210,6 +213,7 @@ __Example__ ``` haskell {-# Language OverloadedStrings #-} + main :: IO () main = do res <- runResourceT $ runMinio minioPlayCI $ do @@ -256,7 +260,9 @@ __ObjectInfo record type__ __Example__ ``` haskell -import Data.Conduit ($$) +{-# Language OverloadedStrings #-} + +import Data.Conduit (($$)) import Conduit.Combinators (sinkList) main :: IO () @@ -305,7 +311,9 @@ __UploadInfo record type__ __Example__ ```haskell -import Data.Conduit ($$) +{-# Language OverloadedStrings #-} + +import Data.Conduit (($$)) import Conduit.Combinators (sinkList) main :: IO () @@ -342,7 +350,6 @@ __Return Value__ The return value can be incrementally read to process the contents of the object. - |Return type |Description | |:---|:---| | _C.ResumableSource Minio ByteString_ | A Conduit ResumableSource of `ByteString` values. | @@ -350,9 +357,12 @@ the object. __Example__ ```haskell +{-# Language OverloadedStrings #-} -import Data.Conduit ($$+-) +import Network.Minio +import Data.Conduit (($$+-)) import Data.Conduit.Binary (sinkLbs) +import qualified Data.ByteString.Lazy as LB main :: IO () main = do @@ -364,11 +374,13 @@ main = do -- bucket, object and upload ID. res <- runResourceT $ runMinio minioPlayCI $ do source <- getObject bucket object - src $$+- sinkLbs + source $$+- sinkLbs -- the following the prints the contents of the object. - print res - + putStrLn $ either + (("Failed to getObject: " ++) . show) + (("Read an object of length: " ++) . show . LB.length) + res ``` @@ -385,7 +397,37 @@ main = do ### removeObject :: Bucket -> Object -> Minio () +Removes an object from the service +__Parameters__ + +In the expression `removeObject bucketName objectName` the parameters +are: + +|Param |Type |Description | +|:---|:---| :---| +| `bucketName` | _Bucket_ (alias for `Text`) | Name of the bucket | +| `objectName` | _Object_ (alias for `Text`) | Name of the object | + +__Example__ + +```haskell +{-# Language OverloadedStrings #-} +import Network.Minio + +main :: IO () +main = do + let + bucket = "mybucket" + object = "myobject" + + res <- runResourceT $ runMinio minioPlayCI $ do + removeObject bucket object + + case res of + Left e -> putStrLn $ "Failed to remove " ++ show bucket ++ "/" ++ show object + Right _ -> putStrLn "Removed object successfully" +``` diff --git a/examples/RemoveObject.hs b/examples/RemoveObject.hs new file mode 100755 index 0000000..6cb4aed --- /dev/null +++ b/examples/RemoveObject.hs @@ -0,0 +1,35 @@ +#!/usr/bin/env stack +-- stack --resolver lts-6.27 runghc --package minio-hs + +-- +-- Minio Haskell SDK, (C) 2017 Minio, Inc. +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- + +{-# Language OverloadedStrings #-} + +import Network.Minio + +main :: IO () +main = do + let + bucket = "mybucket" + object = "myobject" + + res <- runResourceT $ runMinio minioPlayCI $ do + removeObject bucket object + + case res of + Left e -> putStrLn $ "Failed to remove " ++ show bucket ++ "/" ++ show object + Right _ -> putStrLn "Removed object successfully" diff --git a/src/Network/Minio.hs b/src/Network/Minio.hs index 10c7b48..b1f703d 100644 --- a/src/Network/Minio.hs +++ b/src/Network/Minio.hs @@ -64,6 +64,7 @@ module Network.Minio , fPutObject , putObject , copyObject + , removeObject , getObject , statObject @@ -121,10 +122,18 @@ putObject bucket object src sizeMay = copyObject :: Bucket -> Object -> CopyPartSource -> Minio () copyObject bucket object cps = void $ copyObjectInternal bucket object cps +-- | Remove an object from the object store. +removeObject :: Bucket -> Object -> Minio () +removeObject = deleteObject + -- | Get an object from the object store as a resumable source (conduit). getObject :: Bucket -> Object -> Minio (C.ResumableSource Minio ByteString) getObject bucket object = snd <$> getObject' bucket object [] [] +-- | Get an object's metadata from the object store. +statObject :: Bucket -> Object -> Minio ObjectInfo +statObject bucket object = headObject bucket object + -- | Creates a new bucket in the object store. The Region can be -- optionally specified. If not specified, it will use the region -- configured in ConnectInfo, which is by default, the US Standard @@ -135,11 +144,7 @@ makeBucket bucket regionMay= do putBucket bucket region modify (Map.insert bucket region) --- | Get an object's metadata from the object store. -statObject :: Bucket -> Object -> Minio ObjectInfo -statObject bucket object = headObject bucket object - -removeBucket :: Bucket -> Minio() +removeBucket :: Bucket -> Minio () removeBucket bucket = do deleteBucket bucket modify (Map.delete bucket) diff --git a/test/LiveServer.hs b/test/LiveServer.hs index 2c51af4..ce161e2 100644 --- a/test/LiveServer.hs +++ b/test/LiveServer.hs @@ -160,7 +160,7 @@ liveServerUnitTests = testGroup "Unit tests against a live server" "Wrong file size of put file after getting" step $ "Cleanup actions" - deleteObject bucket object + removeObject bucket object -- putObject test (conduit source, no size specified) let obj = "mpart" @@ -194,7 +194,7 @@ liveServerUnitTests = testGroup "Unit tests against a live server" "Wrong file size of put file after getting" step $ "Cleanup actions" - deleteObject bucket obj + removeObject bucket obj step "Prepare for putObjectInternal with large file as source." step "upload large object" @@ -202,7 +202,7 @@ liveServerUnitTests = testGroup "Unit tests against a live server" Just $ 1024*1024*100) step "cleanup" - deleteObject bucket "big" + removeObject bucket "big" , funTestWithBucket "Listing Test" $ \step bucket -> do @@ -274,7 +274,7 @@ liveServerUnitTests = testGroup "Unit tests against a live server" step "Cleanup actions" forM_ expected $ - \obj -> deleteObject bucket obj + \obj -> removeObject bucket obj step "High-level listIncompleteUploads Test" let object = "newmpupload" @@ -336,8 +336,8 @@ liveServerUnitTests = testGroup "Unit tests against a live server" "Copied object did not match expected." step "cleanup actions" - deleteObject bucket object - deleteObject bucket objCopy + removeObject bucket object + removeObject bucket objCopy step "copyObjectPart basic tests" let srcObj = "XXX" @@ -370,8 +370,8 @@ liveServerUnitTests = testGroup "Unit tests against a live server" liftIO $ (s == mb15) @? "Size failed to match" step $ "Cleanup actions" - deleteObject bucket srcObj - deleteObject bucket copyObj + removeObject bucket srcObj + removeObject bucket copyObj step "copyObject basic tests" let srcs = ["XXX", "XXXL"] @@ -391,7 +391,7 @@ liveServerUnitTests = testGroup "Unit tests against a live server" liftIO $ (sizes == uploadedSizes) @? "Uploaded obj sizes failed to match" - forM_ (concat [srcs, copyObjs]) (deleteObject bucket) + forM_ (concat [srcs, copyObjs]) (removeObject bucket) step "copyObject with offset test " let src = "XXX" @@ -412,5 +412,5 @@ liveServerUnitTests = testGroup "Unit tests against a live server" liftIO $ (cSize == 10 * 1024 * 1024) @? "Uploaded obj size mismatched!" - forM_ [src, copyObj] (deleteObject bucket) + forM_ [src, copyObj] (removeObject bucket) ]