Purpose
This module allows a Lua script to authenticate and access Amazon cloud Drive. It allows browsing and managing the files and folders on Amazon cloud Drive.Dependencies
Installation
Either copy the amazondrive.lua file in a directory in the Lua package.path or use LuaRocks to install it like:Basic Usage
The demo.lua file gets access to google drive and then prints out the response to the request to get the root directory listing in the drive.To make the example code work below follow these steps to get the credentials file from Google API for your script:
- Log in to the developer.amazon.com and go to the Drive API page. (Usually from Services and APIs ->Amazon Drive)
- Here you need to create an App. Create its security profile (Amazon needs a web page of the privacy policy before it lets you proceed when I did this).
- In the Apps security profile copy the data to create the creds file. All you need is the client ID, client secret and redirect URIs. The redirect URI can be https://localhost if you don't have a website to get the code. The access code can then be copied from the URL, Note that Amazon also requires that the redirect URI be on https and be registered in the app security profile in the Web Settings. With this the code is the part of the URL in between ?code= and &scope.
- Now execute the code
Example code from demo.lua in the test subdirectory:
API Reference
Module
API
The amazondrive module exposes just 1 function:new
To create a new amazondrive object.Syntax:
Usage:
Inputs:
config table to configure the new amazondrive object. The configuration table can have all the same keys as required by the oauth2 module config input but all are optional except the credential keys:
- creds_file: [OPTIONAL] Full path and name for the credentials file. This parameter if not given then the credentials must be provided in the creds table described below.
- creds: [OPTIONAL] Table containing the connection credentials. This table must have the following keys:
- client_id: String key for the client id.
- client_secret: String key for the client secret.
- redirect_uris: [OPTIONAL] Redirect URIs provided by the service
- auth_uri or auth_url: [OPTIONAL] URL string for requesting authorization. NOTE: If this is given it will overwrite the auth_uri or auth_url given in the main config table
- token_uri or token_url: [OPTIONAL] URL string for requesting token. NOTE: If this is given it will overwrite the token_uri or token_url given in the main config table
- endpoints_file: [OPTIONAL] Full path and name for the endpoints file. If this is given then the endpoints fetched from Amazon will be saved in that file otherwise they will only exist in memory in the config table.
- endPoints: [OPTIONAL] Table containing the connection endpoints. This table should not be changed.
Returns:
- nil and the error message in case of failure to create object and a second error message as the response from the server.
- The amazondrive connection object
amazondrive object
API
item
To check if an item exist on the drive and to get the item object to access and manipulate the item.Syntax:
Usage:
Inputs:
- amazondrive object. This can be the calling amazondrive object using the colon operator
- name - This is the name of the object with the full path from the root that we want to check or obtain the object of.
"/folder1/folder1a"
,"\folder1\folder1a"
,"folder1/folder1a"
,"folder1\folder1a"
are all valid names of the same folder1a object inside folder1 in root. - type - This is a string specifying whether we are specifying a 'file' or a 'folder'. Default is 'file'
- nil and a error message if error followed by the response given by the server
- false if such a item does not exist
- item object for the file or folder item if success
mkdir
To make a directory hierarchy on the drive and get its object for accessing and manipulating the directory. If the folder already exist then it will return that folder's object and not create anything.Syntax:
Usage:
Inputs:
- amazondrive object. This can be the calling amazondrive object using the colon operator
- directory name - This is the name of the folder with the full path from the root that we want to create.
"/folder1/folder1a/folder1aa"
,"\folder1\folder1a\folder1aa"
,"folder1/folder1a/folder1aa"
,"folder1\folder1a\folder1aa"
are all valid names of the same folder1aa object inside folder1a in folder1 in root. - type - This is a string specifying whether we are specifying a 'file' or a 'folder'. Default is 'file'
- nil and a error message if error followed by the response given by the server
- folder object if success
Objects
config
This table contains all the configuration parameters of the oAuth 2.0 object. This table structure is the same as the config structure described in the config object input to the new function.mimeType
This table contains definitions of types of items that can reside on the Amazon drive. The table below shows its members:This can be used to test the type of item object you have like:
tokenUpdated
This is a boolean flag. It is set to true whenever the access token is refreshed by the oAuth 2.0 protocol. If you want to detect when the token is updated set this to false and then poll it periodically or after each request to see if the token was updated. Note the token can be accessed by accessinggd.oauth2.token
root
This is an item object for the root folder for the Amazon drive. See the item object documentation below for more information on item objects.oauth2
This is an oauth2 object used for the connection by the amazondrive object. For more information on this object look hereacquireToken
This is a table array containing 2 elements:- The URL string to access to get the access code from the Amazon Drive service so that the access token can be generated.
- The function to be called with the access code as the parameter to generate the access token and setup the connection
ad.root
object exists. If it does not then go ahead and acquire the token.item Object
API
getProperty
Gets the value of any property name for the item objectSyntax:
Usage:
Inputs:
- item object. This can be the calling item object using the colon operator
- property name - The property name can be any string from the list of data available for an item. The full list can be seen here
- nil and a error message if error followed by the response by the server
- property object which may be a string or a table depending on the property on success
setProperty
Sets the value of any property name for an item objectSyntax:
Usage:
Inputs:
- item object. This can be the calling item object using the colon operator
- property name - The property name can be any string from the list of data available for an item. The full list can be seen here
- value: This may either be a string or a table depending on the property whose value is being set
- nil and a error message if error followed by the response by the server
- true if success setting the property
list
Gets the list of items inside a folder item. This function should only be called for a folder item otherwise it will return nilSyntax:
Usage:
Inputs:
- item object. This can be the calling item object using the colon operator
- number of entries on page - This tells the number of entries on the dirList object page. Default is 100
- nil and a error message if error
- dirList object with all the item objects in the 1st page of the listing on success.
item
To check if an item exist in the folder and to get the item object to access and manipulate the item. This should be called for folder items otherwise it will return a nil.Syntax:
Usage:
Inputs:
- item object. This can be the calling folder item object using the colon operator
- name - This is the name of the object that we want to retrieve or check
- type - This is a string specifying whether we are specifying a 'file' or a 'folder'. Default is 'file'
- nil and a error message if error followed by the server response
- false if such a item does not exist
- item object for the file or folder item if success
mkdir
To make a directory in the folder and get its object for accessing and manipulating it. If the folder already exist then it will return that folder's object and not create anything.Syntax:
Usage:
Inputs:
- item object. This can be the calling folder item object using the colon operator
- directory name - This is the name of the folder that we want to create.
- nil and a error message if error followed by the server response
- folder object if success
upload
Function to upload file to the Amazon drive folderSyntax:
Usage:
Inputs:
- item object. This can be the calling folder item object using the colon operator
- file name - This is the name of the file that is being uploaded.
- source - is a function which on each call returns the next chunk of data
- force - It will not upload if the file already exists unless force is true
- nil and a error message if error followed by the server response
- file object of the uploaded file if success
move
Function to move an item from current parents to the given parent objectSyntax:
Usage:
Inputs:
- item object. This can be the calling folder item object using the colon operator
- destination object - This is the destination folder object
- force - It will not upload if the file already exists unless force is true
- nil and a error message if error followed by the server response
- true if success
rename
Function to rename an item to a new nameSyntax:
Usage:
Inputs:
- item object. This can be the calling folder item object using the colon operator
- new name - This is the new name for the item
- force - It will not rename if another item with the same name exists in that folder unless force is true. In which case the old item is deleted.
- nil and a error message if error followed by the server response
- true if success
copyto
Function to copy an item from current parents to the given parent objectSyntax:
Usage:
Inputs:
- item object. This can be the calling folder item object using the colon operator
- destination object - This is the destination folder object
- force - It will not copy if the file with same name already exists in the destination folder unless force is true
- nil and a error message if error followed by the server response
- copied item object if success
delete
Function to delete an item from the Amazon DriveSyntax:
Usage:
Inputs:
- item object. This can be the calling folder item object using the colon operator
- nil and a error message if error followed by the server response
- true if success
download
Function to download a file or a portion of itSyntax:
Usage:
Inputs:
- item object. This can be the calling folder item object using the colon operator
- sink function - This is a function which is called and passed the file data to be used.
- starting byte - The starting byte of the file data from where to start downloading
- stopping byte - The stopping byte of the file data up till where to download
- nil and a error message if error followed by the server response
- the return value of the sink function after passing data to it.
dirList Object
This object allows browsing a directory listing from Amazon DriveAPI
next
This function gets the next listing page if all the results did not fit in this pageSyntax:
Usage:
Inputs:
- dirList object. This should be the calling directory list object using the colon operator
- nil and a error message if error followed by the response from the server
- directory list object for the next page