Openstex v0.4.0 Openstex.Swift.V1.Helpers behaviour View Source
Helper functions for executing more complex multi-step queries for Swift Object Storage.
Link to this section Summary
Callbacks
Deletes an object from a given container
Deletes an object from a given container
Deletes all objects in a pseudofolder effectively deleting the pseudofolder itself
Download a file. The body of the response contains the binary object (file)
Downloading a file. Returns the binary object (file) or raises an error.
See download_file/2
Generates a tempurl for an object
Gets the swift account string for the swift client
Gets the tempurl key - at an account level
Gets the swift endpoint for a given swift client. Returns the publicUrl of the endpoint with the account string removed
Gets the public url (storage_url) for the swift endpoint
Lists all containers
Lists all containers. Returns a list
or raises an error
Lists all objects within a container
Lists objects within a pseudofolder in a container. Optionally list objects in recursively nested pseudofolders
Lists all the objects in a container or raises an error. See list_objects/3
Lists objects within a pseudofolder in a container or raises an error.
Optionally include objects in nested pseudofolders. See list_objects/3
Lists all pseudofolders including nested pseudofolders in a container.
Also, see list_pseudofolders/3
Lists pseudofolders one level deep in a given pseudofolder.
Also, see list_pseudofolders/3
List pseudofolders within a pseudofolder
Lists all pseudofolders including nested pseudofolders in a container. See list_pseudofolders!/3
Lists pseudofolders one level deep from a given pseudofolder.
Also, see list_pseudofolders!/3
Lists all pseudofolders within a pseudofolder or raises an error.
See list_pseudofolders/3
Checks if a pseudofolder exists
Sets the account tempurl key - at an account level
Upload a file
Upload a file. See upload_file/4
. Returns :ok
if upload suceeeded,
otherwise raises an error
Link to this section Callbacks
delete_object(server_object :: String.t(), container :: String.t()) :: {:ok, HTTPipe.Conn.t()} | {:error, HTTPipe.Conn.t()}
Deletes an object from a given container
Example
server_object = "/openstex_tests/nested/test_file.json"
case Client.Swift.delete_object(server_object, "default_container") do
{:ok, conn} -> ...
{:error, conn} -> ...
end
Deletes an object from a given container
Example
server_object = "/openstex_tests/nested/test_file.json"
:ok = Client.Swift.delete_object!(server_object, "default_container") do
Deletes all objects in a pseudofolder effectively deleting the pseudofolder itself.
Arguments
pseudofolder
: The pseudofolder to be deleted.container
: The container in which to delete the pseudofolder.
Notes
- Returns
:ok
on success or if pseudofolder did not exist in the first place. - Returns
{:error, list}
if some objects were not deleted wherelist
represents the objects for which deletion failed.
Example
case Client.delete_pseudofolder(“products/“, “container_name”) do
:ok -> IO.puts("objects deleted")
{:error, items} -> Enum.each(items, &IO.inspect/1)
end
download_file(server_path :: String.t(), container :: String.t()) :: {:ok, HTTPipe.Conn.t()} | {:error, HTTPipe.Conn.t()}
Download a file. The body of the response contains the binary object (file).
Arguments
server_object
: The path of the file on the openstack swift server.container
: The name of the container.
Example
server_object = "/openstex_tests/nested/test_file.json"
container = "default_container"
case Client.Swift.download_file(server_object, container) do
{:ok, conn} -> object = conn.response.body
{:error, conn} -> ...
end
Downloading a file. Returns the binary object (file) or raises an error.
See download_file/2
.
Example
server_object = "/openstex_tests/nested/test_file.json"
container = "default_container"
object = Client.Swift.download_file!(server_object, container)
Generates a tempurl for an object.
Arguments
container
: container in which the object is found.server_object
: filename under which the file will be stored on the openstack object storage server.defaults to theclient_object_pathname
if none given.opts
:temp_url_expires_after
: Sets the length of time for which the signature to the public link will remain valid. Adds thetemp_url_expires
query string to the url. The unix epoch time format is used. Defaults to 5 minutes from current time iftemp_url
is:true
. Otherwise, it can be set by adding the time in seconds from now for which the link should remain valid. Eg 10 days => (10 24 60 * 60)temp_url_filename
: Defaults to:false
. Swift automatically generates filenames for temp_urls but this option will allow custom names to be added. Works by adding thefilename
query string to the url.temp_url_inline
: Defaults to:false
. If set to:true
, the file is not automatically downloaded by the browser and instead the&inline' query string is added to the url. -
temp_url_method: Defaults to"GET"
but can be set to"PUT"
.
Notes
- The client should have a
:account_temp_url_key
option already specified inconfig.exs
since this is needed for generating temp_urls. - Read more about tempurls at this link
https://docs.openstack.org/developer/swift/api/temporary_url_middleware.html
Example
temp_url = Client.Swift.generate_temp_url("test_container", "test_file.txt")
get_account_tempurl_key(key_number :: atom()) :: String.t() | no_return()
Gets the tempurl key - at an account level.
Example
tempurl_key = Client.Swift.get_account_tempurl_key(:key1)
Gets the swift endpoint for a given swift client. Returns the publicUrl of the endpoint with the account string removed.
Example
endpoint = Client.Swift.get_endpoint()
Gets the public url (storage_url) for the swift endpoint.
Example
account = Client.Swift.get_public_url()
list_containers() :: {:ok, list()} | {:error, map()}
Lists all containers.
Example
case Client.Swift.list_containers() do
{:ok, conn} -> containers = conn.response.body
{:error, conn} -> ...
end
Lists all containers. Returns a list
or raises an error.
Example
containers = Client.Swift.list_containers!()
list_objects(container :: String.t()) :: {:ok, list()} | {:error, map()}
Lists all objects within a container.
Arguments
container
: The name of the container.
Example
case Client.Swift.list_objects() do
{:ok, conn} -> objects = conn.response.body
{:error, conn} -> ...
end
list_objects(pseudofolder :: String.t(), container :: String.t(), opts :: list()) :: {:ok, list()} | {:error, HTTPipe.Conn.t()}
Lists objects within a pseudofolder in a container. Optionally list objects in recursively nested pseudofolders.
Arguments
pseudofolder
: The pseudofolder in which to list the objects.container
: The container in which to find the objects.nested
: defaults to:false
. If:true
, returns all objects in recursively nested pseudofolders. If:false
, returns all objects at one level deep and ignores objects within nested pseudofolders.
Notes
- Excludes pseudofolders from the results. Pseudofolders are filtered out of the results and only binary objects are included in the results.
- If no pseudofolder name is entered,
pseudofolder
defaults to""
, thereby getting all objects in the container.
Example - (1)
Return all objects in the "test_folder/"
but not objects in nested pseudofolders.
case Client.Swift.list_objects("test_folder/", "default", [nested: :false]) do
{:ok, conn} -> objects = conn.response.body
{:error, conn} -> ...
end
Example - (2)
Return all objects in the "test_folder/"
and all recursively nested pseudofolders.
case Client.Swift.list_objects("test_folder/", "default", [nested: :true]) do
{:ok, conn} -> objects = conn.response.body
{:error, conn} -> ...
end
Example - (3)
Returns all objects in the "default_container"
container.
case Client.Swift.list_objects("", "default_container", [nested: :true]) do
{:ok, conn} -> objects = conn.response.body
{:error, conn} -> ...
end
list_objects!(container :: String.t()) :: list() | no_return()
Lists all the objects in a container or raises an error. See list_objects/3
.
Example
objects = Client.Swift.list_objects!()
Lists objects within a pseudofolder in a container or raises an error.
Optionally include objects in nested pseudofolders. See list_objects/3
.
Example
Return all objects in the "test_folder/"
but not objects in nested pseudofolders.
objects_first_level_only = Client.Swift.list_objects!("test_folder/", "default", [nested: :false])
list_pseudofolders(container :: String.t()) :: {:ok, list()} | {:error, HTTPipe.Conn.t()}
Lists all pseudofolders including nested pseudofolders in a container.
Also, see list_pseudofolders/3
.
Arguments
container
: The name of the container.
Example
case Client.Swift.list_pseudofolders("default") do
{:ok, conn} -> pseudofolders = conn.response.body
{:error, conn} -> ...
end
list_pseudofolders(pseudofolder :: String.t(), container :: String.t()) :: {:ok, list()} | {:error, HTTPipe.Conn.t()}
Lists pseudofolders one level deep in a given pseudofolder.
Also, see list_pseudofolders/3
.
Arguments
pseudofolder
: The pseudofolder in which to search for one level deep subfolders.container
: The name of the container.
Example
case Client.Swift.list_pseudofolders("products/categories/", "products_container") do
{:ok, conn} -> pseudofolders = conn.response.body
{:error, conn} -> ...
end
List pseudofolders within a pseudofolder.
Arguments
pseudofolder
: The pseudofolder in which to search for other pseudofolderscontainer
: The name of the container.nested
, defaults to :falsenested
: defaults to:false
. If:true
, returns all pseudofolders in recursively nested pseudofolders. If:false
, returns all pseudofolders at one level deep and ignores pseudofolders within nested pseudofolders.
Notes
- If no pseudofolder is entered, then pseudofolder defaults to
""
which in turn results in all pseudofolders being fetched one level deep at the root level. - Traverses as deep as the most nested pseudofolder if
nested: :true
. - Excludes non-pseudofolder objects from the results, in other words, binary objects will be filtered from the results and only pseudofolders are returned.
Example - (1)
Return all pseudofolders in the "test_folder/"
but not objects in nested pseudofolders.
case Client.Swift.list_pseudofolders("test_folder/", "default", [nested: :false]) do
{:ok, conn} -> pseudofolders = conn.response.body
{:error, conn} -> ...
end
Example - (2)
Return all pseudofolders in the "test_folder/"
one level deep. nested
defaults to :false
case Client.Swift.list_pseudofolders("test", "default") do
{:ok, conn} -> pseudofolders = conn.response.body
{:error, conn} -> ...
end
Example - (3)
Gets all the pseudofolders in the container and traverses to the deepest nested pseudofolders.
case Client.Swift.list_pseudofolders("", "default_container", [nested: :true]) do
{:ok, conn} -> pseudofolders = conn.response.body
{:error, conn} -> ...
end
list_pseudofolders!(String.t()) :: list() | no_return()
Lists all pseudofolders including nested pseudofolders in a container. See list_pseudofolders!/3
Arguments
container
: The name of the container in which to list the pseudofolders.
Example
pseudofolders = Client.Swift.list_pseudofolders!("products_container")
Lists pseudofolders one level deep from a given pseudofolder.
Also, see list_pseudofolders!/3
.
Arguments
pseudofolder
: The pseudofolder to be checked for having subfolders.container
: The name of the container.
Example
categories = Client.Swift.list_pseudofolders!("products/cateogores/", "products_container")
Lists all pseudofolders within a pseudofolder or raises an error.
See list_pseudofolders/3
.
Checks if a pseudofolder exists.
Arguments
pseudofolder
: The pseudofolder whose existence is to be checked.container
: The name of the container.
Example
case Client.Swift.pseudofolder_exists?("products/cateogores/", "products_container") do
:true -> Client.Swift.list_pseudofolders!("products/cateogores/", "products_container")
:false -> ...
end
set_account_temp_url_key(key_number :: atom(), key :: String.t()) :: :ok | no_return()
Sets the account tempurl key - at an account level.
Example
:ok = Client.Swift.set_account_temp_url_key(:key1, "SECRET_TEMPURL_KEY")
upload_file(client_path :: String.t(), server_path :: String.t(), container :: String.t(), opts :: list()) :: {:ok, HTTPipe.Conn.t()} | {:error, HTTPipe.Conn.t()}
Upload a file.
Arguments
file
: The path of the file on the client machine.server_object
: The path of the file on the openstack swift servercontainer
: The name of the container.upload_opts
: SeeOpenstex.Swift.V1.create_object/4
Example
file = "/priv/test_file.json"
server_object = "/openstex_tests/nested/test_file.json"
case Client.Swift.upload_file(file, server_object, "default_container") do
{:ok, conn} -> ...
{:error, conn} -> ...
end
Upload a file. See upload_file/4
. Returns :ok
if upload suceeeded,
otherwise raises an error.
Example
file = "/priv/test_file.json"
server_object = "/openstex_tests/nested/test_file.json"
:ok = Client.Swift.upload_file!(file, server_object, "default_container") do