Current section
5 Versions
Jump to
Current section
5 Versions
Compare versions
5
files changed
+41
additions
-139
deletions
| @@ -1,18 +1,43 @@ | |
| 1 1 | # QCloud |
| 2 2 | |
| 3 | - **TODO: Add description** |
| 4 | - |
| 5 3 | ## Installation |
| 6 4 | |
| 5 | + > 注意:文档不详细,因为项目比较忙,暂时先写这么多 |
| 6 | + |
| 7 7 | If [available in Hex](https://hex.pm/docs/publish), the package can be installed |
| 8 8 | by adding `qcloud` to your list of dependencies in `mix.exs`: |
| 9 9 | |
| 10 10 | ```elixir |
| 11 | + ## step 1 -> mix.exs |
| 11 12 | def deps do |
| 12 13 | [ |
| 13 14 | {:qcloud, "~> 0.1"} |
| 14 15 | ] |
| 15 16 | end |
| 17 | + |
| 18 | + ## step 2 |
| 19 | + |
| 20 | + mix deps.get |
| 21 | + |
| 22 | + ## step 3 -> config/xxx.exs |
| 23 | + config :qcloud, :apps, |
| 24 | + hello_world: %{ # your app name |
| 25 | + cos: %{ |
| 26 | + app_id: "xxx", # your config |
| 27 | + host: "xxx", # your config |
| 28 | + bucket: "xxx", # your config |
| 29 | + secret_id: "xxx", # your config |
| 30 | + secret_key: "xxx" # your config |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + ## step 4 for use |
| 35 | + |
| 36 | + ### eg. |
| 37 | + QCloud.COS.get_object(:hello_world, "files/helloworld.jpg") |
| 38 | + QCloud.COS.put_object(:hello_world, file, "image/jpeg", "files/helloworld.jpg") |
| 39 | + QCloud.COS.delete_object(:hello_world, "files/helloworld.jpg") |
| 40 | + QCloud.COS.head_object(:hello_world, "files/helloworld.jpg") |
| 16 41 | ``` |
| 17 42 | |
| 18 43 | Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc) |
| @@ -5,11 +5,11 @@ | |
| 5 5 | {<<"files">>, |
| 6 6 | [<<"lib">>,<<"lib/.DS_Store">>,<<"lib/qcloud.ex">>,<<"lib/qcloud">>, |
| 7 7 | <<"lib/qcloud/.DS_Store">>,<<"lib/qcloud/cos">>,<<"lib/qcloud/cos/cli.ex">>, |
| 8 | - <<"lib/qcloud/secure_random.ex">>,<<"config">>, |
| 9 | - <<"config/test.secret.exs.default">>,<<"config/dev.secret.exs">>, |
| 10 | - <<"config/test.secret.exs">>,<<"config/dev.secret.exs.default">>, |
| 11 | - <<"config/dev.exs">>,<<"config/config.exs">>,<<"config/test.exs">>, |
| 12 | - <<"mix.exs">>,<<"README.md">>]}. |
| 8 | + <<"config">>,<<"config/test.secret.exs.default">>, |
| 9 | + <<"config/dev.secret.exs">>,<<"config/test.secret.exs">>, |
| 10 | + <<"config/dev.secret.exs.default">>,<<"config/dev.exs">>, |
| 11 | + <<"config/config.exs">>,<<"config/test.exs">>,<<"mix.exs">>, |
| 12 | + <<"README.md">>]}. |
| 13 13 | {<<"licenses">>,[<<"MIT">>]}. |
| 14 14 | {<<"links">>, |
| 15 15 | [{<<"GitHub">>,<<"https://github.com/dev800/qcloud-elixir.git">>}]}. |
| @@ -40,4 +40,4 @@ | |
| 40 40 | {<<"optional">>,false}, |
| 41 41 | {<<"repository">>,<<"hexpm">>}, |
| 42 42 | {<<"requirement">>,<<">= 0.0.0">>}]]}. |
| 43 | - {<<"version">>,<<"0.1.2">>}. |
| 43 | + {<<"version">>,<<"0.1.3">>}. |
| @@ -19,7 +19,7 @@ defmodule QCloud.COS do | |
| 19 19 | def head_object(app, path) do |
| 20 20 | config = app |> get_config() |
| 21 21 | host = config |> Map.get(:host) |
| 22 | - gmt_date = Timex.now() |> Timex.format!("%a, %d %b %Y %H:%M:%S GMT", :strftime) |
| 22 | + gmt_date = _generate_gmt_date() |
| 23 23 | |
| 24 24 | "http://#{host}/#{path}" |
| 25 25 | |> HTTPoison.head([ |
| @@ -45,7 +45,7 @@ defmodule QCloud.COS do | |
| 45 45 | def delete_object(app, path) do |
| 46 46 | config = app |> get_config() |
| 47 47 | host = config |> Map.get(:host) |
| 48 | - gmt_date = Timex.now() |> Timex.format!("%a, %d %b %Y %H:%M:%S GMT", :strftime) |
| 48 | + gmt_date = _generate_gmt_date() |
| 49 49 | |
| 50 50 | "http://#{host}/#{path}" |
| 51 51 | |> HTTPoison.delete([ |
| @@ -71,7 +71,7 @@ defmodule QCloud.COS do | |
| 71 71 | def get_object(app, path) do |
| 72 72 | config = app |> get_config() |
| 73 73 | host = config |> Map.get(:host) |
| 74 | - gmt_date = Timex.now() |> Timex.format!("%a, %d %b %Y %H:%M:%S GMT", :strftime) |
| 74 | + gmt_date = _generate_gmt_date() |
| 75 75 | |
| 76 76 | "http://#{host}/#{path}" |
| 77 77 | |> HTTPoison.get([ |
| @@ -218,4 +218,8 @@ defmodule QCloud.COS do | |
| 218 218 | defp _parse_response({:error, %HTTPoison.Error{reason: reason}}) do |
| 219 219 | {:error, reason} |
| 220 220 | end |
| 221 | + |
| 222 | + defp _generate_gmt_date do |
| 223 | + Timex.now() |> Timex.format!("%a, %d %b %Y %H:%M:%S GMT", :strftime) |
| 224 | + end |
| 221 225 | end |
| @@ -1,127 +0,0 @@ | |
| 1 | - defmodule QCloud.SecureRandom do |
| 2 | - use Bitwise |
| 3 | - |
| 4 | - @moduledoc """ |
| 5 | - Takes my favorite hits from Ruby's SecureRandom and brings em to elixir. |
| 6 | - Mostly a convienance wrapper around Erlangs Crypto library, converting |
| 7 | - Crypto.strong_rand_bytes/1 into a string. |
| 8 | - |
| 9 | - ## Examples |
| 10 | - |
| 11 | - iex> SecureRandom.base64 |
| 12 | - "xhTcitKZI8YiLGzUNLD+HQ==" |
| 13 | - |
| 14 | - iex> SecureRandom.urlsafe_base64(4) |
| 15 | - "pLSVJw" |
| 16 | - |
| 17 | - iex> SecureRandom.uuid |
| 18 | - "a18e8302-c417-076d-196a-71dfbd5b1e03" |
| 19 | - |
| 20 | - """ |
| 21 | - |
| 22 | - @default_length 16 |
| 23 | - |
| 24 | - @doc """ |
| 25 | - Returns random Base64 encoded string. |
| 26 | - |
| 27 | - ## Examples |
| 28 | - |
| 29 | - iex> SecureRandom.base64 |
| 30 | - "rm/JfqH8Y+Jd7m5SHTHJoA==" |
| 31 | - |
| 32 | - iex> SecureRandom.base64(8) |
| 33 | - "2yDtUyQ5Xws=" |
| 34 | - |
| 35 | - """ |
| 36 | - def base64(n \\ @default_length) do |
| 37 | - n |
| 38 | - |> random_bytes() |
| 39 | - |> Base.encode64(case: :lower) |
| 40 | - end |
| 41 | - |
| 42 | - @doc """ |
| 43 | - Generates a random hexadecimal string. |
| 44 | - |
| 45 | - The argument n specifies the length, in bytes, of the random number to be generated. The length of the resulting hexadecimal string is twice n. |
| 46 | - |
| 47 | - If n is not specified, 16 is assumed. It may be larger in future. |
| 48 | - |
| 49 | - The result may contain 0-9 and a-f. |
| 50 | - |
| 51 | - ## Examples |
| 52 | - |
| 53 | - iex> SecureRandom.hex(6) |
| 54 | - "34fb5655a231" |
| 55 | - """ |
| 56 | - def hex(n \\ @default_length) do |
| 57 | - n |
| 58 | - |> random_bytes() |
| 59 | - |> Base.encode16(case: :lower) |
| 60 | - end |
| 61 | - |
| 62 | - @doc """ |
| 63 | - Returns random urlsafe Base64 encoded string. |
| 64 | - |
| 65 | - ## Examples |
| 66 | - |
| 67 | - iex> SecureRandom.urlsafe_base64 |
| 68 | - "xYQcVfWuq6THMY_ZVmG0mA" |
| 69 | - |
| 70 | - iex> SecureRandom.urlsafe_base64(8) |
| 71 | - "8cN__l-6wNw" |
| 72 | - |
| 73 | - """ |
| 74 | - def urlsafe_base64(n \\ @default_length) do |
| 75 | - n |
| 76 | - |> base64() |
| 77 | - |> Base.url_encode64(case: :lower, padding: true) |
| 78 | - end |
| 79 | - |
| 80 | - @doc """ |
| 81 | - Returns UUID v4 string. I have lifted most of this straight from Ecto's implementation. |
| 82 | - |
| 83 | - ## Examples |
| 84 | - |
| 85 | - iex> SecureRandom.uuid |
| 86 | - "e1d87f6e-fbd5-6801-9528-a1d568c1fd02" |
| 87 | - """ |
| 88 | - def uuid do |
| 89 | - bigenerate() |> encode |
| 90 | - end |
| 91 | - |
| 92 | - @doc """ |
| 93 | - Returns random bytes. |
| 94 | - |
| 95 | - ## Examples |
| 96 | - |
| 97 | - iex> SecureRandom.random_bytes |
| 98 | - <<202, 104, 227, 197, 25, 7, 132, 73, 92, 186, 242, 13, 170, 115, 135, 7>> |
| 99 | - |
| 100 | - iex> SecureRandom.random_bytes(8) |
| 101 | - <<231, 123, 252, 174, 156, 112, 15, 29>> |
| 102 | - |
| 103 | - """ |
| 104 | - def random_bytes(n \\ @default_length) do |
| 105 | - :crypto.strong_rand_bytes(n) |
| 106 | - end |
| 107 | - |
| 108 | - defp bigenerate do |
| 109 | - <<u0::48, _::4, u1::12, _::2, u2::62>> = random_bytes(16) |
| 110 | - <<u0::48, 4::4, u1::12, 2::2, u2::62>> |
| 111 | - end |
| 112 | - |
| 113 | - defp encode(<<u0::32, u1::16, u2::16, u3::16, u4::48>>) do |
| 114 | - hex_pad(u0, 8) <> |
| 115 | - "-" <> |
| 116 | - hex_pad(u1, 4) <> "-" <> hex_pad(u2, 4) <> "-" <> hex_pad(u3, 4) <> "-" <> hex_pad(u4, 12) |
| 117 | - end |
| 118 | - |
| 119 | - defp hex_pad(hex, count) do |
| 120 | - hex = Integer.to_string(hex, 16) |
| 121 | - lower(hex, :binary.copy("0", count - byte_size(hex))) |
| 122 | - end |
| 123 | - |
| 124 | - defp lower(<<h, t::binary>>, acc) when h in ?A..?F, do: lower(t, acc <> <<h + 32>>) |
| 125 | - defp lower(<<h, t::binary>>, acc), do: lower(t, acc <> <<h>>) |
| 126 | - defp lower(<<>>, acc), do: acc |
| 127 | - end |
| @@ -4,7 +4,7 @@ defmodule QCloud.Mixfile do | |
| 4 4 | def project do |
| 5 5 | [ |
| 6 6 | app: :qcloud, |
| 7 | - version: "0.1.2", |
| 7 | + version: "0.1.3", |
| 8 8 | elixir: "~> 1.4", |
| 9 9 | build_embedded: Mix.env() == :prod, |
| 10 10 | start_permanent: Mix.env() == :prod, |