hlj_upyun v0.1.4 Upyun

This is a simple client library for Upyun.

Notes on configuration

All the APIs need a policy to be passed in. A policy contains the configuration information about the connection detail, such as bucket, operator, and api endpoint.

A typical policy can be:

%Upyun{bucket: "my-bucket", operator: "bob", password: "secret-password", endpoint: :v0}

Examples:

upload

policy = %Upyun{bucket: "my-bucket", operator: "bob", password: "secret-password", endpoint: :v0}
policy |> Upyun.upload("README.md", "/test/README.md")
#=> :ok

Link to this section Summary

Functions

Delete a remote file

Get the content of remote file

Get information of an object

List entries in a path

Create or update remote file with raw content

Upload a file from local to remote

Upload all files recursively in local directory to remote. Thery are uploaded one by one currently. TODO: upload parallelly

Link to this section Types

Link to this type info()
info() :: {:file, integer(), integer()} | {:dir, integer(), integer()}
Link to this type policy()
policy() :: %Upyun{
  bucket: String.t(),
  endpoint: atom(),
  operator: String.t(),
  password: String.t()
}

Link to this section Functions

Link to this function delete(policy, path)
delete(policy(), binary()) :: :ok | {:error, any()}

Delete a remote file.

  • policy - upyun configuration object
  • path - remote object path

Returns :ok if remote file is successfully deleted or does not exist.

Examples

policy |> Upyun.delete("/my/file")
#=> :ok
Link to this function get(policy, path)
get(policy(), binary()) :: binary() | {:error, :file_not_found}

Get the content of remote file.

  • policy - upyun configuration object
  • path - remote object path

Return the raw content string of the file.

Examples

policy |> Upyun.get("/remote/file")
#=> "content of the file..."
Link to this function info(policy, path)
info(policy(), binary()) :: info() | {:error, :not_found} | {:error, any()}

Get information of an object.

  • policy - upyun configuration object
  • path - remote object path

Examples

# for file:
policy |> Upyun.info("hehe.txt")
#=> {:file, 150, 1448958896}

# for folder:
policy |> Upyun.info("empty_dir")
#=> {:dir, 0, 1448958896}`
Link to this function list(policy, path \\ "/")
list(policy(), binary()) :: :ok | {:error, any()}

List entries in a path.

  • policy - upyun configuration object
  • path - remote directory to list

Returns a list of objects.

Examples

policy |> Upyun.list("/")
#=> a list of items, e.g. {:file, "file"} / {:dir, "folder"}
Link to this function put(policy, content, path, opts \\ [])
put(policy(), binary(), binary(), [any()]) :: :ok

Create or update remote file with raw content.

  • policy - upyun configuration object
  • content - content of the file
  • path - remote object path to store the file
  • opts - (optional) options for making the HTTP request by HTTPoison

Returns :ok if successful.

Examples

content = """
  <html>
    <head>
      <title>Hello, world</title>
    </head>
    <body>Nice to see you.</body>
  </html>
"""
policy |> Upyun.put(content, "/remote/path")
#=> :ok
Link to this function upload(policy, local_path, remote_path, opts \\ [])
upload(policy(), binary(), binary(), [any()]) :: :ok | {:error, any()}

Upload a file from local to remote.

  • policy - upyun configuration object
  • local_path - path of the local file to upload
  • remote_path - remote object path to store the file
  • opts - (optional) options for making the HTTP request by HTTPoison

Returns :ok if successful.

Examples

policy = %Upyun{bucket: "my-bucket", operator: "bob", password: "secret-password", endpoint: :v0}
policy |> Upyun.upload("/path/to/local/file", "/path/to/remote/object")
#=> :ok

Sending custom headers

By default, elixir-upyun will automatically send Content-Type header for you. You can send custom headers via options. Like:

policy |> Upyun.upload("/local/path", "/remote/path", headers: [
  {:"Content-Type", "text/plain"},
  {:"X-Foo", "BAR"}
])
Link to this function upload_dir(policy, local_dir, remote_path, opts \\ [])
upload_dir(policy(), binary(), binary(), [any()]) :: [{binary(), :ok}]

Upload all files recursively in local directory to remote. Thery are uploaded one by one currently. TODO: upload parallelly

  • policy - upyun configuration object
  • local_dir - local directory to upload
  • remote_path - remote object path to store the files
  • opts - (optional) options for making the HTTP request by HTTPoison

Returns an list of result object. A result object is a tuple, with local file name first, and the result (:ok) followed.

Examples

policy |> upload_dir("/etc", "/remote/etc")
#=> [{"passwd", :ok}, {"fastab", :ok}, ...]