Haskell 快速入門指南
適用於 Amazon S3 相容雲端儲存的 MinIO Haskell 用戶端 SDK 


MinIO Haskell 用戶端 SDK 提供簡單的 API 來存取 MinIO 和任何 Amazon S3 相容物件儲存。
本指南假設您具有可用的 Haskell 開發環境。
安裝
新增至您的專案
只需將 minio-hs
新增至您專案的 .cabal
相依性區段,或者如果您使用 hpack,則照常新增至您的 package.yaml
檔案。
在 REPL 中試用
適用於基於 cabal 的環境
下載程式庫來源並變更為解壓縮的目錄
$ cabal get minio-hs
$ cd minio-hs-1.6.0/ # directory name could be different
然後使用程式庫載入 ghci
REPL 環境,並瀏覽可用的 API
$ cabal repl
ghci> :browse Network.Minio
適用於基於 stack 的環境
從您的主資料夾或任何非 Haskell 專案目錄執行
stack install minio-hs
然後啟動一個解譯器會話,並使用以下命令瀏覽可用的 API
$ stack ghci
> :browse Network.Minio
範例
examples 資料夾包含許多您可以試用並用來學習和協助開發您自己專案的範例。
快速入門範例 - 檔案上傳器
這個範例程式會連線到 MinIO 物件儲存伺服器,在伺服器上建立一個儲存桶,然後將一個檔案上傳到該儲存桶。
在這個範例中,我們將使用 https://play.min.io 上運行的 MinIO 伺服器。您可以自由使用此服務進行測試和開發。存取憑證已存在於程式庫中,並且是公開的。
FileUploader.hs
#!/usr/bin/env stack
-- stack --resolver lts-14.11 runghc --package minio-hs --package optparse-applicative --package filepath
--
-- MinIO Haskell SDK, (C) 2017-2019 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 #-}
{-# LANGUAGE ScopedTypeVariables #-}
import Network.Minio
import Data.Monoid ((<>))
import Data.Text (pack)
import Options.Applicative
import System.FilePath.Posix
import UnliftIO (throwIO, try)
import Prelude
-- | The following example uses minio's play server at
-- https://play.min.io. The endpoint and associated
-- credentials are provided via the libary constant,
--
-- > minioPlayCI :: ConnectInfo
--
-- optparse-applicative package based command-line parsing.
fileNameArgs :: Parser FilePath
fileNameArgs = strArgument
(metavar "FILENAME"
<> help "Name of file to upload to AWS S3 or a MinIO server")
cmdParser = info
(helper <*> fileNameArgs)
(fullDesc
<> progDesc "FileUploader"
<> header
"FileUploader - a simple file-uploader program using minio-hs")
main :: IO ()
main = do
let bucket = "my-bucket"
-- Parse command line argument
filepath <- execParser cmdParser
let object = pack $ takeBaseName filepath
res <- runMinio minioPlayCI $ do
-- Make a bucket; catch bucket already exists exception if thrown.
bErr <- try $ makeBucket bucket Nothing
-- If the bucket already exists, we would get a specific
-- `ServiceErr` exception thrown.
case bErr of
Left BucketAlreadyOwnedByYou -> return ()
Left e -> throwIO e
Right _ -> return ()
-- Upload filepath to bucket; object name is derived from filepath.
fPutObject bucket object filepath defaultPutObjectOptions
case res of
Left e -> putStrLn $ "file upload failed due to " ++ show e
Right () -> putStrLn "file upload succeeded."
執行 FileUploader
./FileUploader.hs "path/to/my/file"
貢獻
開發
下載原始碼
$ git clone https://github.com/minio/minio-hs.git
$ cd minio-hs/
建置套件:
使用 cabal
$ # Configure cabal for development enabling all optional flags defined by the package.
$ cabal configure --enable-tests --test-show-details=direct -fexamples -fdev -flive-test
$ cabal build
使用 stack
$ stack build --test --no-run-tests --flag minio-hs:live-test --flag minio-hs:dev --flag minio-hs:examples
執行測試:
預設情況下,部分測試會使用位於 https://play.min.io
的遠端 MinIO Play 伺服器。對於程式庫開發,使用此遠端伺服器可能會很慢。若要針對本地運行的 MinIO 即時伺服器(位於 https://127.0.0.1:9000
),使用憑證 access_key=minio
和 secret_key=minio123
執行測試,只需將環境變數 MINIO_LOCAL
設定為任何值(並取消設定以切換回 Play)。
使用 cabal
$ export MINIO_LOCAL=1 # to run live tests against local MinIO server
$ cabal test
使用 stack
$ export MINIO_LOCAL=1 # to run live tests against local MinIO server
stack test --flag minio-hs:live-test --flag minio-hs:dev
這將會執行所有測試套件。
建置文件:
$ cabal haddock
$ # OR
$ stack haddock