Add getObject and putObject examples

- Use mkRandFile instead of depending on files on disk.
This commit is contained in:
Krishnan Parthasarathi 2017-02-09 14:26:53 +05:30 committed by Aditya Manthramurthy
parent 0509d90ef1
commit fd66d85167
5 changed files with 78 additions and 9 deletions

28
examples/getObject.hs Executable file
View File

@ -0,0 +1,28 @@
#!/usr/bin/env stack
-- stack --resolver lts-6.27 runghc --package minio-hs
{-# Language OverloadedStrings #-}
import Network.Minio
import qualified Data.Conduit as C
import qualified Data.Conduit.Binary as CB
import Prelude
-- | The following example uses minio's play server at
-- https://play.minio.io:9000. The endpoint and associated
-- credentials are provided via the libary constant,
--
-- > minioPlayCI :: ConnectInfo
--
main :: IO ()
main = do
let
bucket = "krisis"
object = "fail.out"
res <- runResourceT $ runMinio minioPlayCI $ do
(_, src) <- getObject bucket object [] []
(src C.$$+- CB.sinkLbs)
print res

37
examples/putObject.hs Executable file
View File

@ -0,0 +1,37 @@
#!/usr/bin/env stack
-- stack --resolver lts-6.27 runghc --package minio-hs
{-# Language OverloadedStrings #-}
import Network.Minio
import qualified Data.Conduit.Combinators as CC
import Prelude
-- | The following example uses minio's play server at
-- https://play.minio.io:9000. The endpoint and associated
-- credentials are provided via the libary constant,
--
-- > minioPlayCI :: ConnectInfo
--
main :: IO ()
main = do
let
bucket = "test"
object = "obj"
mb15 = 15 * 1024 * 1024
-- Eg 1. Upload a stream of repeating "a" using putObjectFromSource.
res1 <- runResourceT $ runMinio minioPlayCI $ do
putObjectFromSource bucket object (CC.repeat "a") (Just mb15)
case res1 of
Left e -> putStrLn $ "putObjectFromSource failed." ++ (show e)
Right () -> putStrLn "putObjectFromSource succeeded."
-- Eg 2. Upload a file using fPutObject.
res2 <- runResourceT $ runMinio minioPlayCI $ do
fPutObject bucket object "path/to/local/file"
case res2 of
Left e -> putStrLn $ "fPutObject failed." ++ (show e)
Right () -> putStrLn "fPutObject succeeded."

View File

@ -2,8 +2,8 @@ module Network.Minio
(
ConnectInfo(..)
, aws
, play
, awsCI
, minioPlayCI
, connect
, Minio
@ -35,6 +35,7 @@ module Network.Minio
, putObjectFromSource
, ObjectData(..)
, getObject
, putObject
, listObjects

View File

@ -30,8 +30,8 @@ instance Default ConnectInfo where
-- |
-- Default aws ConnectInfo. Credentials should be supplied before use.
aws :: ConnectInfo
aws = def {
awsCI :: ConnectInfo
awsCI = def {
connectHost = "s3.amazonaws.com"
, connectPort = 443
, connectAccessKey = ""
@ -41,8 +41,8 @@ aws = def {
-- |
-- Default minio play server ConnectInfo. Credentials are already filled.
play :: ConnectInfo
play = def {
minioPlayCI :: ConnectInfo
minioPlayCI = def {
connectHost = "play.minio.io"
, connectPort = 9000
, connectAccessKey = "Q3AM3UQ867SPQQA43P2F"

View File

@ -113,8 +113,9 @@ liveServerUnitTests = testGroup "Unit tests against a live server"
step "singlepart putObject works"
fPutObject bucket "lsb-release" "/etc/lsb-release"
outFile <- mkRandFile 0
step "simple getObject works"
fGetObject bucket "lsb-release" "/tmp/out"
fGetObject bucket "lsb-release" outFile
step "create new multipart upload works"
uid <- newMultipartUpload bucket "newmpupload" []
@ -242,7 +243,8 @@ liveServerUnitTests = testGroup "Unit tests against a live server"
liftIO $ (T.length uid > 0) @? ("Got an empty multipartUpload Id.")
step "put object parts 1..10"
h <- liftIO $ SIO.openBinaryFile "/tmp/inputfile" SIO.ReadMode
inputFile <- mkRandFile mb15
h <- liftIO $ SIO.openBinaryFile inputFile SIO.ReadMode
forM [1..10] $ \pnum ->
putObjectPart bucket object uid pnum [] $ PayloadH h 0 mb15
@ -292,7 +294,8 @@ liveServerUnitTests = testGroup "Unit tests against a live server"
liftIO $ (T.length uid > 0) @? ("Got an empty multipartUpload Id.")
step "put object parts 1..10"
h <- liftIO $ SIO.openBinaryFile "/tmp/inputfile" SIO.ReadMode
inputFile <- mkRandFile mb15
h <- liftIO $ SIO.openBinaryFile inputFile SIO.ReadMode
forM [1..10] $ \pnum ->
putObjectPart bucket object uid pnum [] $ PayloadH h 0 mb15