## Classes
Cloud Files Abstraction
Promise.<Files>Initializes and returns the cloud files SDK.
To use the SDK you must either provide provide your
OpenWhisk credentials in
credentials.ow or your own
Azure blob storage credentials in credentials.azure.
OpenWhisk credentials can also be read from environment variables (__OW_NAMESPACE and __OW_API_KEY).
ObjectRead, Write, Delete permission enum
Objectexternal, internal URL type enum
objectAn object holding the OpenWhisk credentials
objectSAS Azure credentials. The sdk needs two SAS credentials to allow access to
two already existing containers, a private and a public one (with access=blob).
objectAzure account credentials. Must have the permission to create containers.
stringa string to the remote path. If the path ends with a / it will
be treated as a directory, if not it will be treated as a file.
objectFile properties
ErrorobjectFiles lib custom errors.
e.sdkDetails provides additional context for each error (e.g. function parameter)
Promise.<Array.<RemoteFileProperties>>*
* *[.delete(filePath, [options])](#Files+delete) ⇒ Promise.<Array.<string>>*
* *[.createReadStream(filePath, [options])](#Files+createReadStream) ⇒ Promise.<NodeJS.ReadableStream>*
* *[.createWriteStream(filePath)](#Files+createWriteStream) ⇒ Promise.<NodeJS.WritableStream>*
* *[.read(filePath, [options])](#Files+read) ⇒ Promise.<Buffer>*
* *[.write(filePath, content)](#Files+write) ⇒ Promise.<number>*
* *[.getProperties(filePath)](#Files+getProperties) ⇒ [Promise.<RemoteFileProperties>](#RemoteFileProperties)*
* *[.copy(srcPath, destPath, [options])](#Files+copy) ⇒ Promise.<{key: string}>*
* *[.generatePresignURL(filePath, options)](#Files+generatePresignURL) ⇒ Promise.<string>*
* *[.revokeAllPresignURLs()](#Files+revokeAllPresignURLs) ⇒ void*
### *files.list([filePath]) ⇒ Promise.<Array.<RemoteFileProperties>>*
Lists files. Depending on the input the behavior is different:
- If a path has a trailing '/' it is considered as a directory and
list returns files recursively contained below that path. If the
directory is empty, an empty array is returned.
- If a path has no trailing '/' it will ALWAYS be considered a file and we
will return the file (and its properties) in an array. If that file
doesn't exist we return an empty array EVEN if listing a directory with the
same name would return some files (directories are subpaths, not entities per se).
There is a max limit of 1000 items listed.
**Kind**: instance method of [Files](#Files)
**Returns**: Promise.<Array.<RemoteFileProperties>> - resolves to array of
[RemoteFileProperties](#RemoteFileProperties)
**Access**: public
| Param | Type | Description |
| --- | --- | --- |
| [filePath] | [RemotePathString](#RemotePathString) | [RemotePathString](#RemotePathString) Use a trailing '/' otherwise this is considered as a file. If not specified list all files. |
### *files.delete(filePath, [options]) ⇒ Promise.<Array.<string>>*
Deletes a remote file or directory
There is a max limit of 1000 items deleted in a directory.
**Kind**: instance method of [Files](#Files)
**Returns**: Promise.<Array.<string>> - resolves to array of deleted paths
**Access**: public
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| filePath | [RemotePathString](#RemotePathString) | | [RemotePathString](#RemotePathString) |
| [options] | object | {} | remoteDeleteOptions |
| [options.progressCallback] | function | | cb(RemoteFile) is called after the operation completed on each file |
### *files.createReadStream(filePath, [options]) ⇒ Promise.<NodeJS.ReadableStream>*
***NodeJS only (streams). Does not work on directories.***
Creates a read stream
**Kind**: instance method of [Files](#Files)
**Returns**: Promise.<NodeJS.ReadableStream> - a readable stream
**Access**: public
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| filePath | [RemotePathString](#RemotePathString) | | [RemotePathString](#RemotePathString) |
| [options] | object | {} | createReadStreamOptions |
| [options.position] | number | | read start position of the file. By default is set to 0. If set to bigger than size, throws an ERROR_OUT_OF_RANGE error |
| [options.length] | number | | number of bytes to read. By default reads everything since starting position. If set to bigger than file size, reads until end. |
### *files.createWriteStream(filePath) ⇒ Promise.<NodeJS.WritableStream>*
**[UNSTABLE] please prefer using `write(Files](#Files)
**Returns**: Promise.<NodeJS.WritableStream> - a writable stream
**Access**: public
| Param | Type | Description |
| --- | --- | --- |
| filePath | [RemotePathString](#RemotePathString) | [RemotePathString](#RemotePathString) |
### *files.read(filePath, [options]) ⇒ Promise.<Buffer>*
***Does not work on directories.***
Reads a remote file content
**Kind**: instance method of [Files](#Files)
**Returns**: Promise.<Buffer> - buffer holding content
**Access**: public
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| filePath | [RemotePathString](#RemotePathString) | | [RemotePathString](#RemotePathString) |
| [options] | object | {} | remoteReadOptions |
| [options.position] | number | | read start position of the file. By default is set to 0. If set to bigger than size, throws an ERROR_OUT_OF_RANGE error |
| [options.length] | number | | number of bytes to read. By default reads everything since starting position. If set to bigger than file size, reads until end. |
### *files.write(filePath, content) ⇒ Promise.<number>*
***Does not work on directories.***
Writes content to a file
**Kind**: instance method of [Files](#Files)
**Returns**: Promise.<number> - resolves to number of bytes written
**Access**: public
| Param | Type | Description |
| --- | --- | --- |
| filePath | [RemotePathString](#RemotePathString) | [RemotePathString](#RemotePathString) |
| content | string \| Buffer \| NodeJS.ReadableStream | to be written, `ReadableStream` input works for **NodeJS only** |
### *files.getProperties(filePath) ⇒ [Promise.<RemoteFileProperties>](#RemoteFileProperties)*
Reads properties of a file or directory
**Kind**: instance method of [Files](#Files)
**Returns**: [Promise.<RemoteFileProperties>](#RemoteFileProperties) - resolves [RemoteFileProperties](#RemoteFileProperties)
| Param | Type | Description |
| --- | --- | --- |
| filePath | [RemotePathString](#RemotePathString) | [RemotePathString](#RemotePathString) |
### *files.copy(srcPath, destPath, [options]) ⇒ Promise.<{key: string}>*
***NodeJS only (streams + fs).***
A utility function to copy files and directories across remote and local Files. This
is comparable to the `scp` command
Rules for copy files are:
1. Remote => Remote
- a/ (dir) => b/: b/a/
- a (file) => b/: b/a *does nothing if b/a exists and noOverwrite=true*
- a (file) => b : b *does nothing if b exists and noOverwrite=true*
- a/ (dir) => b : b/ *always allowed: in remote Files we can have both b and b/*
2. Remote => Local
- a/ => b/: b/a/
- a => b/: b/a *does nothing if b/a exists and noOverwrite=true*
- a => b : b *does nothing if b exists and noOverwrite=true*
- a/ => b : b/ *throws an error if b exists and is a file: cannot copy a remote
dir to a local file*
3. Local => Remote
- a/ => b/: b/a/
- a => b/: b/a *does nothing if b/a exists and noOverwrite=true*
- a => b : b *does nothing if b exists and noOverwrite=true*
- a/ => b: b/ *always allowed: in remote Files we can have both b and b/*
4. Local => Local
- not supported
There is a max limit of 1000 items copied.
**Kind**: instance method of [Files](#Files)
**Returns**: Promise.<{key: string}> - returns a promise resolving to an object
containing all copied files from src to dest `{ srcFilePath: destFilePath }`
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| srcPath | [RemotePathString](#RemotePathString) | | copy source path to a file or directory. If srcPath points to a local file set `options.localSrc` to true |
| destPath | [RemotePathString](#RemotePathString) | | copy destination path to a file or directory. If destPath points to a local file set `options.localDest` to true |
| [options] | object | {} | remoteCopyOptions |
| [options.localSrc] | boolean | false | Set this option to true to copy files from the local file system. Cannot be combined with localDest. |
| [options.localDest] | boolean | false | Set this option to true to copy files to the local file system. Cannot be combined with localSrc. |
| [options.noOverwrite] | boolean | false | set to true to not overwrite existing dest files |
| [options.progressCallback] | function | | a function that will be called every time the operation completes on a single file,the srcPath and destPath to the copied file are passed as argument to the callback `progressCallback(srcPath, destPath)` |
### *files.generatePresignURL(filePath, options) ⇒ Promise.<string>*
Generate pre-sign URLs for a private file
**Kind**: instance method of [Files](#Files)
**Returns**: Promise.<string> - Presign URL for the given file
| Param | Type | Description |
| --- | --- | --- |
| filePath | [RemotePathString](#RemotePathString) | [RemotePathString](#RemotePathString) |
| options | object | Options to generate presign URL |
| options.expiryInSeconds | number | presign URL expiry duration |
| options.permissions | string | permissions for presigned URL (any combination of rwd) |
| options.urlType | string | default 'external', type of URL to return 'internal' or 'external' |
### *files.revokeAllPresignURLs() ⇒ void*
Revoke all generated pre-sign URLs
**Kind**: instance method of [Files](#Files)
## init([config]) ⇒ [Promise.<Files>](#Files)
Initializes and returns the cloud files SDK.
To use the SDK you must either provide provide your
[OpenWhisk credentials](#OpenWhiskCredentials) in
`credentials.ow` or your own
[Azure blob storage credentials](#AzureCredentialsAccount) in `credentials.azure`.
OpenWhisk credentials can also be read from environment variables (`__OW_NAMESPACE` and `__OW_API_KEY`).
**Kind**: global function
**Returns**: [Promise.<Files>](#Files) - A Files instance
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [config] | object | {} | configuration used to init the sdk |
| [config.ow] | [OpenWhiskCredentials](#OpenWhiskCredentials) | | [OpenWhiskCredentials](#OpenWhiskCredentials). Set those if you want to use ootb credentials to access the state management service. OpenWhisk namespace and auth can also be passed through environment variables: `__OW_NAMESPACE` and `__OW_API_KEY` |
| [config.azure] | [AzureCredentialsAccount](#AzureCredentialsAccount) \| [AzureCredentialsSAS](#AzureCredentialsSAS) | | bring your own [Azure SAS credentials](#AzureCredentialsSAS) or [Azure storage account credentials](#AzureCredentialsAccount) |
| [config.tvm] | object | | tvm configuration, applies only when passing OpenWhisk credentials |
| [config.tvm.apiUrl] | string | | alternative tvm api url. |
| [config.tvm.cacheFile] | string | | alternative tvm cache file, set to `false` to disable caching of temporary credentials. |
## FilePermissions : Object
Read, Write, Delete permission enum
**Kind**: global typedef
## UrlType : Object
external, internal URL type enum
**Kind**: global typedef
## OpenWhiskCredentials : object
An object holding the OpenWhisk credentials
**Kind**: global typedef
**Properties**
| Name | Type | Description |
| --- | --- | --- |
| namespace | string | user namespace |
| auth | string | auth key |
## AzureCredentialsSAS : object
SAS Azure credentials. The sdk needs two SAS credentials to allow access to
two already existing containers, a private and a public one (with access=`blob`).
**Kind**: global typedef
**Properties**
| Name | Type | Description |
| --- | --- | --- |
| sasURLPrivate | string | sas url to existing private azure blob container |
| sasURLPublic | string | sas url to existing public (with access=`blob`) azure blob container |
## AzureCredentialsAccount : object
Azure account credentials. Must have the permission to create containers.
**Kind**: global typedef
**Properties**
| Name | Type | Description |
| --- | --- | --- |
| storageAccount | string | name of azure storage account |
| storageAccessKey | string | access key for azure storage account |
| containerName | string | name of container to store files. Another `${containerName}-public` will also be used for public files. |
| [hostName] | string | custom domain for returned URLs |
## RemotePathString : string
a string to the remote path. If the path ends with a `/` it will
be treated as a directory, if not it will be treated as a file.
**Kind**: global typedef
## RemoteFileProperties : object
File properties
**Kind**: global typedef
**Properties**
| Name | Type | Description |
| --- | --- | --- |
| name | string | unique name of this file, it is the full path |
| creationTime | string | utc datetime string when file was created |
| lastModified | string | utc datetime string when file last modified |
| etag | string | unique ( per modification ) etag for the asset |
| contentLength | number | size, in bytes |
| contentType | string | mime/type |
| isDirectory | boolean | true if file is a directory |
| isPublic | boolean | true if file is public |
| url | string | remote file URL with URI encoded path, use decodeURIComponent to decode the URL. |
| internalUrl | string | remote file URL which allows file access only from Adobe I/O Runtime actions. |
## FilesLibError : Error
**Kind**: global typedef
## FilesLibErrors : object
Files lib custom errors.
`e.sdkDetails` provides additional context for each error (e.g. function parameter)
**Kind**: global typedef
**Properties**
| Name | Type | Description |
| --- | --- | --- |
| ERROR_BAD_ARGUMENT | [FilesLibError](#FilesLibError) | this error is thrown when an argument is missing or has invalid type |
| ERROR_NOT_IMPLEMENTED | [FilesLibError](#FilesLibError) | this error is thrown when a method is not implemented or when calling methods directly on the abstract class (Files). |
| ERROR_BAD_CREDENTIALS | [FilesLibError](#FilesLibError) | this error is thrown when the supplied init credentials are invalid. |
| ERROR_INTERNAL | [FilesLibError](#FilesLibError) | this error is thrown when an unknown error is thrown by the underlying provider or TVM server for credential exchange. More details can be found in `e.sdkDetails._internal`. |
| ERROR_FILE_NOT_EXISTS | [FilesLibError](#FilesLibError) | this error is thrown when the filePath does not exists for operations that need the file to exists (e.g. read) |
| ERROR_BAD_FILE_TYPE | [FilesLibError](#FilesLibError) | this error is thrown when the filePath is not the expected type for operations that need the file to be of a specific type, e.g. write on a dir would fail |