minio-hs/docs/API.md
2017-07-17 13:58:22 -07:00

611 lines
17 KiB
Markdown

# Minio Haskell SDK API Reference
## Initialize Minio Client object.
### Minio - for public Play server
```haskell
minioPlayCI :: ConnectInfo
minioPlayCI
```
### AWS S3
```haskell
awsCI :: ConnectInfo
awsCI { connectAccesskey = "your-access-key"
, connectSecretkey = "your-secret-key"
}
```
|Bucket operations|Object Operations|
|:---|:---|
|[`listBuckets`](#listBuckets) |[`getObject`](#getObject)|
|[`makeBucket`](#makeBucket)|[`putObject`](#putObject)|
|[`removeBucket`](#removeBucket)|[`fGetObject`](#fGetObject)|
|[`listObjects`](#listObjects)|[`fPutObject`](#fPutObject)|
|[`listIncompleteUploads`](#listIncompleteUploads)|[`copyObject`](#copyObject)|
|[`bucketExists`](#bucketExists)|[`removeObject`](#removeObject)|
## 1. Connecting and running operations on the storage service
The Haskell Minio SDK provides high-level functionality to perform
operations on a Minio server or any AWS S3-like API compatible storage
service.
### The `ConnectInfo` type
The `ConnectInfo` record-type contains connection information for a
particular server. It is recommended to construct the `ConnectInfo`
value using one of the several smart constructors provided by the
library, documented in the following subsections.
The library automatically discovers the region of a bucket by
default. This is especially useful with AWS, where buckets may be in
different regions. When performing an upload, download or other
operation, the library requests the service for the location of a
bucket and caches it for subsequent requests.
#### awsCI :: ConnectInfo
`awsCI` is a value that provides connection information for AWS
S3. Credentials can be supplied by overriding a couple of fields like
so:
``` haskell
awsConn = awsCI {
connectAccessKey = "my-AWS-access-key"
, connectSecretKey = "my-AWS-secret-key"
}
```
#### awsWithRegionCI :: Region -> Bool -> ConnectInfo
This constructor allows to specify the initial region and a Boolean to
enable/disable the automatic region discovery behaviour.
The parameters in the expression `awsWithRegion region autoDiscover` are:
|Parameter|Type|Description|
|:---|:---|:---|
| `region` | _Region_ (alias for `Text`) | The region to connect to by default for all requests. |
| `autoDiscover` | _Bool_ | If `True`, region discovery will be enabled. If `False`, discovery is disabled, and all requests go the given region only.|
#### minioPlayCI :: ConnectInfo
This constructor provides connection and authentication information to
connect to the public Minio Play server at
`https://play.minio.io:9000/`.
#### minioCI :: Text -> Int -> Bool -> ConnectInfo
Use to connect to a Minio server.
The parameters in the expression `minioCI host port isSecure` are:
|Parameter|Type|Description|
|:---|:---|:---|
| `host` | _Text_ | Hostname of the Minio or other S3-API compatible server |
| `port` | _Int_ | Port number to connect to|
| `isSecure` | _Bool_ | Does the server use HTTPS? |
#### The ConnectInfo fields and Default instance
The following table shows the fields in the `ConnectInfo` record-type:
| Field | Type | Description |
|:---|:---|:---|
| `connectHost` | _Text_ | Host name of the server. Defaults to `localhost`. |
| `connectPort` | _Int_ | Port number on which the server listens. Defaults to `9000`. |
| `connectAccessKey` | _Text_ | Access key to use in authentication. Defaults to `minio`. |
| `connectSecretkey` | _Text_ | Secret key to use in authentication. Defaults to `minio123`. |
| `connectIsSecure` | _Bool_ | Specifies if the server used TLS. Defaults to `False` |
| `connectRegion` | _Region_ (alias for `Text`) | Specifies the region to use. Defaults to 'us-east-1' |
| `connectAutoDiscoverRegion` | _Bool_ | Specifies if the library should automatically discover the region of a bucket. Defaults to `True`|
The `def` value of type `ConnectInfo` has all the above default
values.
### The Minio Monad
This monad provides the required environment to perform requests
against a Minio or other S3 API compatible server. It uses the
connection information from the `ConnectInfo` value provided to it. It
performs connection pooling, bucket location caching, error handling
and resource clean-up actions.
The `runMinio` function performs the provided action in the `Minio`
monad and returns a `IO (Either MinioErr a)` value:
``` haskell
{-# Language OverloadedStrings #-}
import Network.Minio
main :: IO ()
main = do
result <- runMinio def $ do
buckets <- listBuckets
return $ length buckets
case result of
Left e -> putStrLn $ "Failed operation with error: " ++ show e
Right n -> putStrLn $ show n ++ " bucket(s) found."
```
The above performs a `listBuckets` operation and returns the number of
buckets in the server. If there were any errors, they will be returned
as values of type `MinioErr` as a `Left` value.
## 2. Bucket operations
<a name="listBuckets"></a>
### listBuckets :: Minio [BucketInfo]
Lists buckets.
__Return Value__
|Return type |Description |
|:---|:---|
| _Minio [BucketInfo]_| List of buckets |
__BucketInfo record type__
|Field |Type |Description |
|:---|:---| :---|
| `biName` | _Bucket_ (alias of `Text`) | Name of the bucket |
| `biCreationDate` | _UTCTime_ | Creation time of the bucket |
<a name="makeBucket"></a>
### makeBucket :: Bucket -> Maybe Region -> Minio ()
Create a new bucket. If the region is not specified, the region
specified by `ConnectInfo` is used.
__Parameters__
In the expression `makeBucket bucketName region` the arguments are:
| Param | Type | Description |
|---|---|---|
|`bucketName` | _Bucket_ (alias for `Text`) | Name of the bucket |
| `region` | _Maybe Region_ | Region where the bucket is to be created. If not specified, default to the region in `ConnectInfo`.|
__Example__
``` haskell
{-# Language OverloadedStrings #-}
main :: IO ()
main = do
res <- runMinio minioPlayCI $ do
makeBucket bucketName (Just "us-east-1")
case res of
Left err -> putStrLn $ "Failed to make bucket: " ++ (show res)
Right _ -> putStrLn $ "makeBucket successful."
```
<a name="removeBucket"></a>
### removeBucket :: Bucket -> Minio ()
Remove a bucket. The bucket must be empty or an error will be thrown.
__Parameters__
In the expression `removeBucket bucketName` the arguments are:
| Param | Type | Description |
|---|---|---|
|`bucketName` | _Bucket_ (alias for `Text`) | Name of the bucket |
__Example__
``` haskell
{-# Language OverloadedStrings #-}
main :: IO ()
main = do
res <- runMinio minioPlayCI $ do
removeBucket "mybucket"
case res of
Left err -> putStrLn $ "Failed to remove bucket: " ++ (show res)
Right _ -> putStrLn $ "removeBucket successful."
```
<a name="listObjects"></a>
### listObjects :: Bucket -> Maybe Text -> Bool -> C.Producer Minio ObjectInfo
List objects in the given bucket.
__Parameters__
In the expression `listObjects bucketName prefix recursive` the
arguments are:
|Param |Type |Description |
|:---|:---| :---|
| `bucketName` | _Bucket_ (alias for `Text`) | Name of the bucket |
| `prefix` | _Maybe Text_ | Optional prefix that listed objects should have |
| `recursive` | _Bool_ |`True` indicates recursive style listing and `False` indicates directory style listing delimited by '/'. |
__Return Value__
|Return type |Description |
|:---|:---|
| _C.Producer Minio ObjectInfo_ | A Conduit Producer of `ObjectInfo` values corresponding to each incomplete multipart upload |
__ObjectInfo record type__
|Field |Type |Description |
|:---|:---| :---|
|`oiObject` | _Object_ (alias for `Text`) | Name of object |
|`oiModTime` | _UTCTime_ | Last modified time of the object |
|`oiETag` | _ETag_ (alias for `Text`) | ETag of the object |
|`oiSize` | _Int64_ | Size of the object in bytes |
__Example__
``` haskell
{-# Language OverloadedStrings #-}
import Data.Conduit (($$))
import Conduit.Combinators (sinkList)
main :: IO ()
main = do
let
bucket = "test"
-- Performs a recursive listing of all objects under bucket "test"
-- on play.minio.io.
res <- runMinio minioPlayCI $ do
listObjects bucket Nothing True $$ sinkList
print res
```
<a name="listIncompleteUploads"></a>
### listIncompleteUploads :: Bucket -> Maybe Prefix -> Bool -> C.Producer Minio UploadInfo
List incompletely uploaded objects.
__Parameters__
In the expression `listIncompleteUploads bucketName prefix recursive`
the parameters are:
|Param |Type |Description |
|:---|:---| :---|
| `bucketName` | _Bucket_ (alias for `Text`) | Name of the bucket |
| `prefix` | _Maybe Text_ | Optional prefix that listed objects should have. |
| `recursive` | _Bool_ |`True` indicates recursive style listing and `Talse` indicates directory style listing delimited by '/'. |
__Return Value__
|Return type |Description |
|:---|:---|
| _C.Producer Minio UploadInfo_ | A Conduit Producer of `UploadInfo` values corresponding to each incomplete multipart upload |
__UploadInfo record type__
|Field |Type |Description |
|:---|:---| :---|
|`uiKey` | _Object_ |Name of incompletely uploaded object |
|`uiUploadId` | _String_ |Upload ID of incompletely uploaded object |
|`uiSize` | _Int64_ |Size of incompletely uploaded object |
__Example__
```haskell
{-# Language OverloadedStrings #-}
import Data.Conduit (($$))
import Conduit.Combinators (sinkList)
main :: IO ()
main = do
let
bucket = "test"
-- Performs a recursive listing of all incompletely uploaded objects
-- under bucket "test" on play.minio.io.
res <- runMinio minioPlayCI $ do
listIncompleteUploads bucket Nothing True $$ sinkList
print res
```
## 3. Object operations
<a name="getObject"></a>
### getObject :: Bucket -> Object -> Minio (C.ResumableSource Minio ByteString)
Get an object from the service.
__Parameters__
In the expression `getObject 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 |
__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. |
__Example__
```haskell
{-# Language OverloadedStrings #-}
import Network.Minio
import Data.Conduit (($$+-))
import Data.Conduit.Binary (sinkLbs)
import qualified Data.ByteString.Lazy as LB
main :: IO ()
main = do
let
bucket = "mybucket"
object = "myobject"
-- Lists the parts in an incompletely uploaded object identified by
-- bucket, object and upload ID.
res <- runMinio minioPlayCI $ do
source <- getObject bucket object
source $$+- sinkLbs
-- the following the prints the contents of the object.
putStrLn $ either
(("Failed to getObject: " ++) . show)
(("Read an object of length: " ++) . show . LB.length)
res
```
<a name="putObject"></a>
### putObject :: Bucket -> Object -> C.Producer Minio ByteString -> Maybe Int64 -> Minio ()
Uploads an object to a bucket in the service, from the given input
byte stream of optionally supplied length
__Parameters__
In the expression `putObject bucketName objectName inputSrc` the parameters
are:
|Param |Type |Description |
|:---|:---| :---|
| `bucketName` | _Bucket_ (alias for `Text`) | Name of the bucket |
| `objectName` | _Object_ (alias for `Text`) | Name of the object |
| `inputSrc` | _C.Producer Minio ByteString_ | A Conduit Producer of `ByteString` values |
__Example__
```haskell
{-# Language OverloadedStrings #-}
import Network.Minio
import qualified Data.Conduit.Combinators as CC
main :: IO ()
main = do
let
bucket = "mybucket"
object = "myobject"
kb15 = 15 * 1024
res <- runMinio minioPlayCI $ do
putObject bucket object (CC.repeat "a") (Just kb15)
case res of
Left e -> putStrLn $ "Failed to putObject " ++ show bucket ++ "/" ++ show object
Right _ -> putStrLn "PutObject was successful"
```
<a name="fGetObject"></a>
### fGetObject :: Bucket -> Object -> FilePath -> Minio ()
Downloads an object from a bucket in the service, to the given file
__Parameters__
In the expression `fGetObject bucketName objectName inputFile` the parameters
are:
|Param |Type |Description |
|:---|:---| :---|
| `bucketName` | _Bucket_ (alias for `Text`) | Name of the bucket |
| `objectName` | _Object_ (alias for `Text`) | Name of the object |
| `inputFile` | _FilePath_ | Path to the file to be uploaded |
``` haskell
{-# Language OverloadedStrings #-}
import Network.Minio
import Data.Conduit (($$+-))
import Data.Conduit.Binary (sinkLbs)
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 = "my-bucket"
object = "my-object"
localFile = "/etc/lsb-release"
res <- runMinio minioPlayCI $ do
src <- fGetObject bucket object localFile
(src $$+- sinkLbs)
case res of
Left e -> putStrLn $ "fGetObject failed." ++ (show e)
Right _ -> putStrLn "fGetObject succeeded."
```
<a name="fPutObject"></a>
### fPutObject :: Bucket -> Object -> FilePath -> Minio ()
Uploads an object to a bucket in the service, from the given file
__Parameters__
In the expression `fPutObject bucketName objectName inputFile` the parameters
are:
|Param |Type |Description |
|:---|:---| :---|
| `bucketName` | _Bucket_ (alias for `Text`) | Name of the bucket |
| `objectName` | _Object_ (alias for `Text`) | Name of the object |
| `inputFile` | _FilePath_ | Path to the file to be uploaded |
__Example__
```haskell
{-# Language OverloadedStrings #-}
import Network.Minio
import qualified Data.Conduit.Combinators as CC
main :: IO ()
main = do
let
bucket = "mybucket"
object = "myobject"
localFile = "/etc/lsb-release"
res <- runMinio minioPlayCI $ do
fPutObject bucket object localFile
case res of
Left e -> putStrLn $ "Failed to fPutObject " ++ show bucket ++ "/" ++ show object
Right _ -> putStrLn "fPutObject was successful"
```
<a name="copyObject"></a>
### copyObject :: Bucket -> Object -> CopyPartSource -> Minio ()
Copies content of an object from the service to another
__Parameters__
In the expression `copyObject bucketName objectName cps` the parameters
are:
|Param |Type |Description |
|:---|:---| :---|
| `bucketName` | _Bucket_ (alias for `Text`) | Name of the bucket |
| `objectName` | _Object_ (alias for `Text`) | Name of the object |
| `cps` | _CopyPartSource_ | A value representing properties of the source object |
__CopyPartSource record type__
|Field |Type |Description |
|:---|:---| :---|
| `cpSource` | `Text`| Name of source object formatted as "/srcBucket/srcObject" |
| `cpSourceRange` | `Maybe (Int64, Int64)` | Represents the byte range of source object. (0, 9) represents first ten bytes of source object|
| `cpSourceIfMatch` | `Maybe Text` | (Optional) ETag source object should match |
| `cpSourceIfNoneMatch` | `Maybe Text` | (Optional) ETag source object shouldn't match |
| `cpSourceIfUnmodifiedSince` | `Maybe UTCTime` | (Optional) Time since source object wasn't modified |
| `cpSourceIfModifiedSince` | `Maybe UTCTime` | (Optional) Time since source object was modified |
__Example__
```haskell
{-# Language OverloadedStrings #-}
import Network.Minio
main :: IO ()
main = do
let
bucket = "mybucket"
object = "myobject"
srcObject = "/mybucket/srcObject"
res <- runMinio minioPlayCI $ do
copyObject bucket object def { cpSource = srcObject }
case res of
Left e -> putStrLn $ "Failed to copyObject " ++ show srcObject"
Right _ -> putStrLn "copyObject was successful"
```
<a name="removeObject"></a>
### 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 <- runMinio minioPlayCI $ do
removeObject bucket object
case res of
Left e -> putStrLn $ "Failed to remove " ++ show bucket ++ "/" ++ show object
Right _ -> putStrLn "Removed object successfully"
```
<a name="BucketExists"></a>
### bucketExists :: Bucket -> Minio Bool
Checks if a bucket exists.
__Parameters__
In the expression `bucketExists bucketName` the parameters are:
|Param |Type |Description |
|:---|:---| :---|
| `bucketName` | _Bucket_ (alias for `Text`) | Name of the bucket |
<!-- ## 4. Presigned operations -->
<!-- TODO -->
<!-- ## 5. Bucket policy/notification operations -->
<!-- TODO -->
<!-- ## 6. Explore Further -->
<!-- TODO -->