ReqS3
Req plugin for Amazon S3 and S3-compatible services.
ReqS3 handles a custom s3://
url scheme that supports these URLs:
s3://<bucket> # GET bucket items list
s3://<bucket>/<item> # GET/PUT bucket item
Usage
Mix.install([
{:req, "~> 0.5.0"},
{:req_s3, "~> 0.2.0"}
])
req = Req.new() |> ReqS3.attach()
Req.get!(req, url: "s3://ossci-datasets").body
#=>
# %{
# "ListBucketResult" => %{
# "Contents" => [
# %{"Key" => "mnist/", ...},
# %{"Key" => "mnist/t10k-images-idx3-ubyte.gz", ...},
# ...
# ],
# "Name" => "ossci-datasets",
# ...
# }
# }
body = Req.get!(req, url: "s3://ossci-datasets/mnist/t10k-images-idx3-ubyte.gz").body
<<_::32, n_images::32, n_rows::32, n_cols::32, _body::binary>> = body
{n_images, n_rows, n_cols}
#=> {10_000, 28, 28}
ReqS3 can be also used to presign URLs:
options = [
access_key_id: System.fetch_env!("AWS_ACCESS_KEY_ID"),
secret_access_key: System.fetch_env!("AWS_SECRET_ACCESS_KEY")
]
req = Req.new() |> ReqS3.attach(aws_sigv4: options)
%{status: 200} = Req.put!(req, url: "s3://bucket1/key1", body: "Hello, World!")
presigned_url = ReqS3.presign_url([url: "s3://bucket1/key1"] ++ options)
#=> "https://s3.amazonaws.com/bucket1/key1?X-Amz-Algorithm=AWS4-HMAC-SHA256&..."
Req.get!(presigned_url).body
#=> "Hello, World!"
and form uploads:
form = ReqS3.presign_form([bucket: "bucket1", key: "key1"] ++ options)
Req.post!(form.url, form_multipart: [file: "Hello, World!"] ++ form.fields)
#=> "Hello, World!"
TODO
- Add Phoenix Playground example.
License
Copyright (c) 2021 Wojtek Mach
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.