Last active
March 3, 2017 10:31
-
-
Save donatello/2579c2b1116214ebfcc6e6c4e368845e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env stack | |
-- stack --resolver lts-6.27 runghc --package minio-hs --package optparse-applicative --package filepath | |
-- | |
-- Mar. 3 2017 -- using github.com/minio/minio-hs (master) to test | |
-- copyobjectpart fix | |
{-# Language OverloadedStrings, ScopedTypeVariables, NoImplicitPrelude, RankNTypes #-} | |
import Protolude | |
import Network.Minio | |
import qualified Control.Monad.Trans.Resource as R | |
import Crypto.Hash (SHA256(..), MD5(..), hashWith, Digest) | |
import Crypto.Hash.Conduit (sinkHash) | |
import Data.ByteArray (ByteArrayAccess, convert) | |
import Data.ByteArray.Encoding (convertToBase, Base(Base16)) | |
import qualified Data.ByteString as BS | |
import qualified Data.Conduit as C | |
import qualified Data.Conduit.Binary as CB | |
import Data.Default (def) | |
import qualified Data.Text as T | |
import qualified Data.Text.Format as TF | |
import Data.Text.Format.Params (Params) | |
import qualified Data.Text.Lazy as LT | |
import System.Directory (getTemporaryDirectory) | |
import qualified Test.QuickCheck as Q | |
format :: Params ps => TF.Format -> ps -> Text | |
format f args = LT.toStrict $ TF.format f args | |
digestToBase16 :: ByteArrayAccess a => a -> ByteString | |
digestToBase16 = convertToBase Base16 | |
randomDataSrc :: MonadIO m => Int64 -> C.Producer m ByteString | |
randomDataSrc s' = genBS s' | |
where | |
oneMiB = 1024*1024 + 7 | |
concatIt bs n = BS.concat $ replicate (fromIntegral q) bs ++ | |
[BS.take (fromIntegral r) bs] | |
where (q, r) = n `divMod` fromIntegral (BS.length bs) | |
genBS s = do | |
w8s <- liftIO $ Q.generate $ Q.vectorOf 67 (Q.choose (0, 255)) | |
let byteArr64 = BS.pack w8s | |
if s < oneMiB | |
then C.yield $ concatIt byteArr64 s | |
else do C.yield $ concatIt byteArr64 oneMiB | |
genBS (s - oneMiB) | |
mkRandFile :: R.MonadResource m => Int64 -> m FilePath | |
mkRandFile size = do | |
dir <- liftIO $ getTemporaryDirectory | |
randomDataSrc size C.$$ CB.sinkTempFile dir "miniohstest.random" | |
hashSHA256FromSource :: Monad m => C.Producer m ByteString -> m ByteString | |
hashSHA256FromSource src = do | |
digest <- src C.$$ sinkSHA256Hash | |
return $ digestToBase16 digest | |
where | |
-- To help with type inference | |
sinkSHA256Hash :: Monad m => C.Consumer ByteString m (Digest SHA256) | |
sinkSHA256Hash = sinkHash | |
fileEquality :: R.MonadResource m => FilePath -> FilePath -> m Bool | |
fileEquality fp1 fp2 = do | |
h1 <- hashSHA256FromSource (CB.sourceFile fp1) | |
h2 <- hashSHA256FromSource (CB.sourceFile fp2) | |
return $ h1 == h2 | |
main :: IO () | |
main = do | |
let size = 512 * 1024 * 1024 | |
res <- runResourceT $ runMinio def $ do | |
-- create a large file with random content to upload of 512 MiB. | |
sourcePath <- mkRandFile size | |
print "Created random file" | |
print sourcePath | |
-- upload it to bucket/sourceObject | |
fPutObject "bucket" "sourceObject" sourcePath | |
print "Created source object" | |
-- copy it to bucket/copiedObject (since the object is more than | |
-- 64MiB, the library will use copyobjectpart) | |
let cps = def { | |
cpSource = "/bucket/sourceObject" | |
} | |
copyObject "bucket" "copiedObject" cps | |
print "Copied the object" | |
-- now read copied object into a new file. | |
fGetObject "bucket" "copiedObject" "/tmp/copiedFile" | |
print "downloaded the copied object" | |
eqVal <- fileEquality sourcePath "/tmp/copiedFile" | |
print $ T.concat ["Files are equal:", show eqVal] | |
print res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Most util. functions are taken from minio-hs library itself.