|
1 | 1 | # bucket
|
2 | 2 |
|
| 3 | +Gleam S3 API client, suitable for AWS S3, Garage, Minio, Storj, |
| 4 | +Backblaze B2, Cloudflare R2, Ceph, Wasabi, and so on! |
| 5 | + |
3 | 6 | [](https://hex.pm/packages/bucket)
|
4 | 7 | [](https://hexdocs.pm/bucket/)
|
5 | 8 |
|
| 9 | +This package uses the _sans-io_ approach, meaning it does not send HTTP requests |
| 10 | +itself, instead it gives you functions for creating HTTP requests for and |
| 11 | +decoding HTTP responses from an S3 API, and you send the requests with a HTTP |
| 12 | +client of your choosing. |
| 13 | + |
| 14 | +This HTTP client independence gives you full control over HTTP, and means this |
| 15 | +library can be used on both the Erlang and JavaScript runtimes. |
| 16 | + |
6 | 17 | ```sh
|
7 | 18 | gleam add bucket@1
|
8 | 19 | ```
|
9 | 20 | ```gleam
|
10 | 21 | import bucket
|
| 22 | +import bucket/get_object.{Found} |
| 23 | +import gleam/bit_array |
| 24 | +import gleam/http.{Https} |
| 25 | +import gleam/io |
| 26 | +import httpc |
11 | 27 |
|
| 28 | +/// This program downloads an object and prints the string contents. |
| 29 | +/// |
| 30 | +/// It uses `let assert` to handle errors, but in a real program you'd most |
| 31 | +/// likely want to use pattern matching or the `gleam/result` module to handle |
| 32 | +/// the error values gracefully. |
| 33 | +/// |
12 | 34 | pub fn main() {
|
13 |
| - // TODO: An example of the project in use |
14 |
| -} |
15 |
| -``` |
| 35 | + let creds = bucket.credentials( |
| 36 | + host: "s3-api-host.example.com", |
| 37 | + access_key_id: "YOUR_ACCESS_KEY", |
| 38 | + secret_access_key: "YOUR_SECRET_ACCESS_KEY", |
| 39 | + ) |
16 | 40 |
|
17 |
| -Further documentation can be found at <https://hexdocs.pm/bucket>. |
| 41 | + // Create a HTTP request to download an object |
| 42 | + let request = |
| 43 | + get_object.request(bucket: "my-bucket", key: "my/key.txt") |
| 44 | + |> get_object.build() |
18 | 45 |
|
19 |
| -## Development |
| 46 | + // Send the HTTP request |
| 47 | + let assert Ok(response) = httpc.send_bits(request) |
20 | 48 |
|
21 |
| -```sh |
22 |
| -gleam run # Run the project |
23 |
| -gleam test # Run the tests |
| 49 | + // Decode the response from the API |
| 50 | + let assert Ok(Found(object)) = get_object.response(response) |
| 51 | +
|
| 52 | + // Print the string contents |
| 53 | + let assert Ok(text) = bit_array.from_string(object) |
| 54 | + io.println(text) |
| 55 | +} |
24 | 56 | ```
|
| 57 | + |
| 58 | +Further documentation can be found at <https://hexdocs.pm/bucket>. |
0 commit comments