billstclair / elm-s3 / S3

Pure Elm client for the AWS Simple Storage Service (S3) or Digital Ocean Spaces.

Types


type alias Request a =
AWS.Http.Request AWS.Http.AWSAppError a

A request that can be turned into a Task by S3.send.

a is the type of the successful Task result from S3.send.

Turning a Request into a Task

send : Types.Account -> Request a -> Task Types.Error a

Create a Task to send a signed request over the wire.

Creating S3 requests

listKeys : Types.Bucket -> Request Types.KeyList

Create a Request to list the keys for an S3 bucket.

getObject : Types.Bucket -> Types.Key -> Request String

Read an S3 object.

The contents will be the successful result of the Task created by S3.send.

getFullObject : Types.Bucket -> Types.Key -> (Http.Metadata -> String -> Result String a) -> Request a

Read an object and process the entire Http Response.

getHeaders : Types.Bucket -> Types.Key -> Request (Dict String String)

Do a HEAD request to get only an object's headers.

getObjectWithHeaders : Types.Bucket -> Types.Key -> Request ( String, Dict String String )

Read an object with its HTTP response headers.

putHtmlObject : Types.Bucket -> Types.Key -> String -> Request String

Write an Html string to S3, with public-read permission.

The string resulting from a successful send isn't interesting.

putPublicObject : Types.Bucket -> Types.Key -> AWS.Http.Body -> Request String

Write an object to S3, with public-read permission.

The string resulting from a successful send isn't interesting.

putObject : Types.Bucket -> Types.Key -> AWS.Http.Body -> Request String

Write an object to S3, with default permissions (private).

The string resulting from a successful send isn't interesting.

deleteObject : Types.Bucket -> Types.Key -> Request String

Delete an S3 object.

The string resulting from a successful send isn't interesting.

Creating Body values

htmlBody : String -> AWS.Http.Body

Create an HTML body for putObject and friends.

jsonBody : Json.Encode.Value -> AWS.Http.Body

Create a JSON body for putObject and friends.

stringBody : Types.Mimetype -> String -> AWS.Http.Body

Create a body with any mimetype for putObject and friends.

stringBody "text/html" "Hello, World!"

Adding queries and headers to a request

addQuery : Types.Query -> Request a -> Request a

Add query parameters to a Request.

addHeaders : Types.Query -> Request a -> Request a

Add headers to a Request.

Reading accounts into Elm

readAccounts : Maybe String -> Task Types.Error (List Types.Account)

Read JSON from a URL and turn it into a list of Accounts.

If Nothing is passed for the first arg (the URL), will use the default of "accounts.json".

You're not going to want to store the secret keys in this JSON in plain text anywhere but your development machine. I'll add support eventually for encryption of the accounts JSON.

Example JSON (the buckets are used only by the example code):

[{"name": "Digital Ocean",
  "region": "nyc3",
  "is-digital-ocean": true,
  "access-key": "<20-character access key>",
  "secret-key": "<40-character secret key>",
  "buckets": ["bucket1","bucket2"]
 },
 {"name": "Amazon S3",
  "region": "us-east-1",
  "access-key": "<20-character access key>",
  "secret-key": "<40-character secret key>",
  "buckets": ["bucket3","bucket4","bucket5"]
 }
]

decodeAccounts : String -> Result Types.Error (List Types.Account)

Decode a JSON string encoding a list of Accounts

accountDecoder : Json.Decode.Decoder Types.Account

A Decoder for the Account type.

encodeAccount : Types.Account -> Json.Encode.Value

Encode an account as a JSON value.

Low-level functions

objectPath : Types.Bucket -> Types.Key -> String

Turn a bucket and a key into an object path.

"/" ++ bucket ++ "/" ++ key

parserRequest : String -> AWS.Http.Method -> AWS.Http.Path -> AWS.Http.Body -> (String -> Result String a) -> Request a

Low-level request creator.

stringRequest : String -> Method -> Path -> Body -> Request String
stringRequest method url body =
    parserRequest
        name
        method
        url
        body
        (identity >> Ok)
        Task.succeed

stringRequest : String -> AWS.Http.Method -> AWS.Http.Path -> AWS.Http.Body -> Request String

Create a Request that returns its response body as a string.

getObject : Bucket -> Key -> Request String
getObject bucket key =
    stringRequest "operation" GET (objectPath bucket key) emptyBody

requestUrl : Types.Account -> Request a -> String

Return the URL string for a request.