-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Membrane.AWS.S3.Source #1
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3b6989f
Add Membrane.AWS.S3.Source
Rados13 812a386
Modify tests
Rados13 f3cf859
Working tests
Rados13 db7a7c8
Rename S3.Source option to aws_config
Rados13 8ac1a2a
Add example of usage
Rados13 737f513
Update lib/s3/s3_source.ex
Rados13 16e59fe
Update mix.exs
Rados13 f72718c
Remove unused deps
Rados13 e766a50
Add reference to ExAWS.Config in description of aws_config option
Rados13 e86aba2
Add usage example in README
Rados13 9683d31
Modify how chunks are downloaded
Rados13 4074abb
Fix dialyzer issue
Rados13 2db55df
Remove unnecessary options
Rados13 ab3b755
Update mix.exs
Rados13 1906e42
Update lib/s3/s3_source.ex
Rados13 4cdbae0
Update lib/s3/s3_source.ex
Rados13 8ef09dc
Fixes after review
Rados13 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
12 changes: 0 additions & 12 deletions
12
...ub/ISSUE_TEMPLATE/please--open-new-issues-in-membranefranework-membrane_core.md
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
|
||
.env | ||
compile_commands.json | ||
.gdb_history | ||
bundlex.sh | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,42 @@ | ||
# Membrane Template Plugin | ||
# Membrane AWS Plugin | ||
|
||
[![Hex.pm](https://img.shields.io/hexpm/v/membrane_template_plugin.svg)](https://hex.pm/packages/membrane_template_plugin) | ||
[![API Docs](https://img.shields.io/badge/api-docs-yellow.svg?style=flat)](https://hexdocs.pm/membrane_template_plugin) | ||
[![CircleCI](https://circleci.com/gh/membraneframework/membrane_template_plugin.svg?style=svg)](https://circleci.com/gh/membraneframework/membrane_template_plugin) | ||
[![Hex.pm](https://img.shields.io/hexpm/v/membrane_aws_plugin.svg)](https://hex.pm/packages/membrane_aws_plugin) | ||
[![API Docs](https://img.shields.io/badge/api-docs-yellow.svg?style=flat)](https://hexdocs.pm/membrane_aws_plugin) | ||
[![CircleCI](https://circleci.com/gh/jellyfish-dev/membrane_aws_plugin.svg?style=svg)](https://circleci.com/gh/jellyfish-dev/membrane_aws_plugin) | ||
[![codecov](https://codecov.io/gh/jellyfish-dev/membrane_aws_plugin/branch/main/graph/badge.svg?token=ANWFKV2EDP)](https://codecov.io/gh/jellyfish-dev/membrane_aws_plugin) | ||
|
||
This repository contains a template for new plugins. | ||
This repository contains Membrane element that interacts with AWS. | ||
Currently implemented are: | ||
- `Membrane.AWS.S3.Source` | ||
|
||
Check out different branches for other flavors of this template. | ||
|
||
It's a part of the [Membrane Framework](https://membrane.stream). | ||
|
||
## Installation | ||
|
||
The package can be installed by adding `membrane_template_plugin` to your list of dependencies in `mix.exs`: | ||
The package can be installed by adding `membrane_aws_plugin` to your list of dependencies in `mix.exs`: | ||
|
||
```elixir | ||
def deps do | ||
[ | ||
{:membrane_template_plugin, "~> 0.1.0"} | ||
{:membrane_aws_plugin, "~> 0.1.0"} | ||
] | ||
end | ||
``` | ||
|
||
## Usage | ||
## Usage example | ||
|
||
TODO | ||
The `example/` folder contains an example usage of `Membrane.AWS.S3.Source`. | ||
|
||
This demo downloads a file from S3 and saves it locally. It requires that you set these environment variables: `BUCKET`, `FILE_PATH`, `ACCESS_KEY_ID`, `SECRET_ACCESS_KEY`, `REGION`. | ||
```bash | ||
$ elixir examples/source_example.exs | ||
``` | ||
|
||
## Copyright and License | ||
|
||
Copyright 2020, [Software Mansion](https://swmansion.com/?utm_source=git&utm_medium=readme&utm_campaign=membrane_template_plugin) | ||
Copyright 2024, [Software Mansion](https://swmansion.com/?utm_source=git&utm_medium=readme&utm_campaign=membrane_aws_plugin) | ||
|
||
[![Software Mansion](https://logo.swmansion.com/logo?color=white&variant=desktop&width=200&tag=membrane-github)](https://swmansion.com/?utm_source=git&utm_medium=readme&utm_campaign=membrane_template_plugin) | ||
[![Software Mansion](https://logo.swmansion.com/logo?color=white&variant=desktop&width=200&tag=membrane-github)](https://swmansion.com/?utm_source=git&utm_medium=readme&utm_campaign=membrane_aws_plugin) | ||
|
||
Licensed under the [Apache License, Version 2.0](LICENSE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
Mix.install([ | ||
{:membrane_aws_plugin, path: __DIR__ |> Path.join("..") |> Path.expand()}, | ||
:membrane_core, | ||
:membrane_file_plugin, | ||
:hackney | ||
]) | ||
|
||
# In this example, the pipeline will download file from S3 and save it to a local file | ||
|
||
defmodule Example do | ||
use Membrane.Pipeline | ||
|
||
@impl true | ||
def handle_init(_ctx, options) do | ||
structure = [ | ||
child(:s3_source, %Membrane.AWS.S3.Source{ | ||
bucket: System.fetch_env!("BUCKET"), | ||
path: System.fetch_env!("FILE_PATH"), | ||
aws_config: [ | ||
access_key_id: System.fetch_env!("ACCESS_KEY_ID"), | ||
secret_access_key: System.fetch_env!("SECRET_ACCESS_KEY"), | ||
region: System.fetch_env!("REGION") | ||
] | ||
}) | ||
|> child(:file_sink, %Membrane.File.Sink{location: "source.example"}) | ||
] | ||
|
||
{[spec: structure], %{}} | ||
end | ||
|
||
# Next two functions are only a logic for terminating a pipeline when it's done, you don't need to worry | ||
@impl true | ||
def handle_element_end_of_stream(:file_sink, _pad, _ctx, state) do | ||
{[terminate: :normal], state} | ||
end | ||
|
||
def handle_element_end_of_stream(_element, _pad, _ctx, state) do | ||
{[], state} | ||
end | ||
end | ||
|
||
{:ok, _supervisor, pipeline} = Membrane.Pipeline.start_link(Example) | ||
monitor_ref = Process.monitor(pipeline) | ||
|
||
receive do | ||
{:DOWN, ^monitor_ref, :process, _pid, _reason} -> | ||
:ok | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
defmodule Membrane.AWS.S3.Source do | ||
@moduledoc """ | ||
Element that reads file from a S3 bucket and sends it through the output pad. | ||
""" | ||
use Membrane.Source | ||
|
||
alias Membrane.{Buffer, RemoteStream} | ||
|
||
@default_max_concurrency 8 | ||
@task_timeout_milliseconds 60_000 | ||
|
||
def_options aws_config: [ | ||
spec: Keyword.t(), | ||
description: """ | ||
Config to ExAWS. For more information refer to [`ExAws.Config`](https://github.com/ex-aws/ex_aws/blob/main/lib/ex_aws/config.ex). | ||
""", | ||
default: [] | ||
], | ||
bucket: [ | ||
spec: binary(), | ||
description: "Name of bucket" | ||
], | ||
path: [ | ||
spec: binary(), | ||
description: "Path to file in bucket" | ||
], | ||
opts: [ | ||
spec: [ | ||
max_concurrency: pos_integer(), | ||
chunk_size: pos_integer(), | ||
timeout: pos_integer() | ||
], | ||
description: "File download opts", | ||
default: [] | ||
], | ||
cached_chunks_number: [ | ||
spec: non_neg_integer(), | ||
description: "Number of chunks cached before demand", | ||
default: 10 | ||
] | ||
|
||
def_output_pad :output, accepted_format: %RemoteStream{type: :bytestream}, flow_control: :manual | ||
|
||
@impl true | ||
def handle_init(_context, opts) do | ||
state = | ||
Map.merge(opts, %{ | ||
aws_config: ExAws.Config.new(:s3, opts.aws_config), | ||
chunks_stream: nil, | ||
chunks: :queue.new() | ||
}) | ||
|
||
{[], state} | ||
end | ||
|
||
@impl true | ||
def handle_playing(_ctx, state) do | ||
chunks_stream = ExAws.S3.Download.build_chunk_stream(state, state.aws_config) | ||
|
||
{take_chunks, chunks_stream} = Enum.split(chunks_stream, state.cached_chunks_number) | ||
|
||
downloaded_chunks = | ||
take_chunks | ||
|> Task.async_stream( | ||
&download_chunk(state, &1), | ||
max_concurrency: state.opts[:max_concurrency] || @default_max_concurrency, | ||
timeout: state.opts[:timeout] || @task_timeout_milliseconds | ||
) | ||
|> Enum.map(fn {:ok, chunk} -> chunk end) | ||
|> Enum.reduce(state.chunks, fn chunk, acc -> :queue.in(chunk, acc) end) | ||
|
||
{[stream_format: {:output, %RemoteStream{type: :bytestream}}], | ||
%{state | chunks_stream: chunks_stream, chunks: downloaded_chunks}} | ||
end | ||
|
||
@impl true | ||
def handle_demand(_pad, _size, _unit, _ctx, state) do | ||
{chunks, chunks_stream} = update_chunks(state) | ||
|
||
case :queue.out(chunks) do | ||
{:empty, _chunks} -> | ||
{[end_of_stream: :output], state} | ||
|
||
{{:value, payload}, chunks} -> | ||
{[buffer: {:output, %Buffer{payload: payload}}] ++ [redemand: :output], | ||
%{state | chunks_stream: chunks_stream, chunks: chunks}} | ||
end | ||
end | ||
|
||
defp update_chunks(state) do | ||
{boundaries, chunks_stream} = Enum.split(state.chunks_stream, 1) | ||
|
||
chunks = | ||
case boundaries do | ||
[boundaries] -> | ||
chunk = download_chunk(state, boundaries) | ||
:queue.in(chunk, state.chunks) | ||
|
||
[] -> | ||
state.chunks | ||
end | ||
|
||
{chunks, chunks_stream} | ||
end | ||
|
||
defp download_chunk(state, boundaries) do | ||
{_start_byte, chunk} = ExAws.S3.Download.get_chunk(state, boundaries, state.aws_config) | ||
chunk | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see any example in Readme or type for
aws_config
. I think that it would be nice to know what keywords you have to pass to access a s3 bucket.