CloudStorage
File storage for iCloud and Google Drive.
File storage for iCloud and Google Drive.
Constructors
Constructor
new CloudStorage(
provider?: CloudStorageProvider,
options?: CloudStorageProviderOptionsValue,
): RNCloudStorage;Creates a new RNCloudStorage instance for the given provider.
Parameters
| Parameter | Type | Description |
|---|---|---|
provider? | CloudStorageProvider | The provider to create the instance for. Defaults to the default provider for the current platform. |
options? | CloudStorageProviderOptionsValue | - |
Returns
RNCloudStorage
Methods
appendFile()
appendFile(
path: string,
data: string,
scope?: CloudStorageScope,
): Promise<void>;Appends the data to the file at the given path, creating the file if it doesn't exist.
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | The file to append to. |
data | string | The data to append. |
scope? | CloudStorageScope | The directory scope the path is in. Defaults to the default scope set for the current provider. |
Returns
Promise<void>
A promise that resolves when the data has been appended.
downloadFile()
Overload 1 (deprecated)
downloadFile(
path: string,
scope?: CloudStorageScope,
): Promise<void>;Requests an iCloud file download. Does not have any effect on Google Drive.
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | The file to request a download for. |
scope? | CloudStorageScope | The directory scope the path is in. Defaults to set default scope set for the current provider. |
Returns
Promise<void>
A promise that resolves once the download has been requested.
Deprecated
Use triggerSync instead.
Overload 2
downloadFile(
remotePath: string,
localPath: string,
scope?: CloudStorageScope,
): Promise<void>;Downloads the cloud file at the given remote path to the given local path.
Parameters
| Parameter | Type | Description |
|---|---|---|
remotePath | string | The remote path of the file to download from the cloud. |
localPath | string | The local path to download the cloud file to. |
scope? | CloudStorageScope | The directory scope the path is in. Defaults to set default scope set for the current provider. |
Returns
Promise<void>
exists()
exists(
path: string,
scope?: CloudStorageScope,
): Promise<boolean>;Tests whether or not the file at the given path exists.
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | The path to test. |
scope? | CloudStorageScope | The directory scope the path is in. Defaults to set default scope set for the current provider. |
Returns
Promise<boolean>
A promise that resolves to true if the path exists, false otherwise.
getProvider()
getProvider(): CloudStorageProvider;Gets the current CloudStorageProvider.
Returns
The current CloudStorageProvider.
getProviderOptions()
getProviderOptions(): CloudStorageProviderOptionsValue;Gets the current options for the current provider.
Returns
CloudStorageProviderOptionsValue
The current options for the current provider.
isCloudAvailable()
isCloudAvailable(): Promise<boolean>;Checks for an iCloud identity or a Google Drive access token. iCloud may be unavailable right after app launch or if the user is not signed in.
Returns
Promise<boolean>
A promise that resolves to true if an identity or token is present, false otherwise.
mkdir()
mkdir(path: string, scope?: CloudStorageScope): Promise<void>;Creates a new directory at the given path.
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | The directory to create. |
scope? | CloudStorageScope | The directory scope the path is in. Defaults to set default scope set for the current provider. |
Returns
Promise<void>
A promise that resolves when the directory has been created.
readdir()
readdir(
path: string,
scope?: CloudStorageScope,
): Promise<string[]>;Lists the contents of the directory at the given path.
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | The directory to list. |
scope? | CloudStorageScope | The directory scope the path is in. Defaults to set default scope set for the current provider. |
Returns
Promise<string[]>
A promise that resolves to an array of file names, excluding '.' and '..'.
readFile()
Overload 1
readFile(
path: string,
options?: ReadTextOptions,
): Promise<string>;Reads the contents of the file at the given path.
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | The file to read. |
options? | ReadTextOptions | The read options. Reads text in the provider's default scope if omitted. |
Returns
Promise<string>
A promise that resolves to the contents of the file.
Overload 2
readFile(
path: string,
options: ReadBytesOptions,
): Promise<Uint8Array<ArrayBufferLike>>;Reads bytes from a file. Holds the full file in memory.
Parameters
| Parameter | Type |
|---|---|
path | string |
options | ReadBytesOptions |
Returns
Promise<Uint8Array<ArrayBufferLike>>
Overload 3
readFile(
path: string,
options?: ReadFileOptions,
): Promise<string | Uint8Array<ArrayBufferLike>>;Reads text or bytes according to the format option.
Parameters
| Parameter | Type |
|---|---|
path | string |
options? | ReadFileOptions |
Returns
Promise<string | Uint8Array<ArrayBufferLike>>
Overload 4 (deprecated)
readFile(
path: string,
scope?: CloudStorageScope,
): Promise<string>;Parameters
| Parameter | Type |
|---|---|
path | string |
scope? | CloudStorageScope |
Returns
Promise<string>
Deprecated
Pass the scope in an options object: { scope }.
rmdir()
rmdir(
path: string,
options?: object,
scope?: CloudStorageScope,
): Promise<void>;Deletes the directory at the given path.
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | The directory to delete. |
options? | { recursive?: boolean; } | Options for the delete operation. Defaults to { recursive: false }. |
options.recursive? | boolean | - |
scope? | CloudStorageScope | The directory scope the path is in. Defaults to set default scope set for the current provider. |
Returns
Promise<void>
A promise that resolves when the directory has been deleted.
setProvider()
setProvider(provider: CloudStorageProvider): void;Sets the current CloudStorageProvider.
Parameters
| Parameter | Type | Description |
|---|---|---|
provider | CloudStorageProvider | The provider to set. |
Returns
void
setProviderOptions()
setProviderOptions(
options: CloudStorageProviderOptionsValue,
): void;Sets the options for the current provider.
Parameters
| Parameter | Type | Description |
|---|---|---|
options | CloudStorageProviderOptionsValue | The options to set for the provider. |
Returns
void
stat()
stat(
path: string,
scope?: CloudStorageScope,
): Promise<CloudStorageFileStat>;Gets the size, creation time, and modification time of the file at the given path.
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | The file to stat. |
scope? | CloudStorageScope | The directory scope the path is in. Defaults to set default scope set for the current provider. |
Returns
Promise<CloudStorageFileStat>
A promise that resolves to the CloudStorageFileStat object.
subscribeToCloudAvailability()
subscribeToCloudAvailability(
listener: (available: boolean) => void,
): void;Parameters
| Parameter | Type |
|---|---|
listener | (available: boolean) => void |
Returns
void
triggerSync()
triggerSync(
path: string,
scope?: CloudStorageScope,
): Promise<void>;Requests an iCloud file download. Does not have any effect on Google Drive.
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | The file to request a download for. |
scope? | CloudStorageScope | The directory scope the path is in. Defaults to set default scope set for the current provider. |
Returns
Promise<void>
A promise that resolves once the download has been requested.
unlink()
unlink(path: string, scope?: CloudStorageScope): Promise<void>;Deletes the file at the given path.
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | The file to delete. |
scope? | CloudStorageScope | The directory scope the path is in. Defaults to set default scope set for the current provider. |
Returns
Promise<void>
A promise that resolves when the file has been deleted.
unsubscribeFromCloudAvailability()
unsubscribeFromCloudAvailability(
listener: (available: boolean) => void,
): void;Parameters
| Parameter | Type |
|---|---|
listener | (available: boolean) => void |
Returns
void
uploadFile()
uploadFile(
remotePath: string,
localPath: string,
options: object,
scope?: CloudStorageScope,
): Promise<void>;Uploads the file at the given local path to the given path, creating it if it doesn't exist or overwriting it if it does.
Parameters
| Parameter | Type | Description |
|---|---|---|
remotePath | string | The remote path to upload to. |
localPath | string | The local path of the file to upload. |
options | { mimeType: string; } | The options for the upload. Must contain a mimeType property. |
options.mimeType | string | - |
scope? | CloudStorageScope | The directory scope the path is in. Defaults to set default scope set for the current provider. |
Returns
Promise<void>
A promise that resolves after the local iCloud container copy or the Google Drive upload completes. iOS uploads the iCloud copy asynchronously, the promise therefore does not confirm delivery.
writeFile()
Overload 1
writeFile(
path: string,
data: string,
options?: WriteFileOptions,
): Promise<void>;Writes to the file at the given path, creating it if it doesn't exist or overwriting it if it does.
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | The file to write to. |
data | string | The data to write. |
options? | WriteFileOptions | The write options. Uses the provider's default scope if omitted. |
Returns
Promise<void>
A promise that resolves when the file has been written.
Overload 2
writeFile(
path: string,
data: Uint8Array,
options: WriteFileOptions & object,
): Promise<void>;Writes bytes to a file. Requires a MIME type and holds the full file in memory.
Parameters
| Parameter | Type |
|---|---|
path | string |
data | Uint8Array |
options | WriteFileOptions & object |
Returns
Promise<void>
Overload 3 (deprecated)
writeFile(
path: string,
data: string,
scope?: CloudStorageScope,
): Promise<void>;Parameters
| Parameter | Type |
|---|---|
path | string |
data | string |
scope? | CloudStorageScope |
Returns
Promise<void>
Deprecated
Pass the scope in an options object: { scope }.
appendFile()
static appendFile(
path: string,
data: string,
scope?: CloudStorageScope,
): Promise<void>;Appends the data to the file at the given path in the provider of the default static instance, creating the file if it doesn't exist.
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | The file to append to. |
data | string | The data to append. |
scope? | CloudStorageScope | The directory scope the path is in. Defaults to the default scope set for the default static instance. |
Returns
Promise<void>
A promise that resolves when the data has been appended.
downloadFile()
Overload 1 (deprecated)
static downloadFile(
path: string,
scope?: CloudStorageScope,
): Promise<void>;Requests an iCloud file download on the default instance. Does not have any effect on Google Drive.
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | The file to request a download for. |
scope? | CloudStorageScope | The directory scope the path is in. Defaults to set default scope set for the current provider. |
Returns
Promise<void>
A promise that resolves once the download has been requested.
Deprecated
Use triggerSync instead.
Overload 2
static downloadFile(
remotePath: string,
localPath: string,
scope?: CloudStorageScope,
): Promise<void>;Downloads the cloud file at the given remote path to the given local path.
Parameters
| Parameter | Type | Description |
|---|---|---|
remotePath | string | The remote path of the file to download from the cloud. |
localPath | string | The local path to download the cloud file to. |
scope? | CloudStorageScope | The directory scope the path is in. Defaults to set default scope set for the current provider. |
Returns
Promise<void>
exists()
static exists(
path: string,
scope?: CloudStorageScope,
): Promise<boolean>;Tests whether or not the file at the given path exists in the provider of the default static instance.
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | The path to test. |
scope? | CloudStorageScope | The directory scope the path is in. Defaults to set default scope set for the current provider. |
Returns
Promise<boolean>
A promise that resolves to true if the path exists, false otherwise.
getDefaultInstance()
static getDefaultInstance(): RNCloudStorage;Returns
RNCloudStorage
getDefaultProvider()
static getDefaultProvider(): CloudStorageProvider;Gets the default CloudStorageProvider for the current platform.
Returns
The default CloudStorageProvider.
getProvider()
static getProvider(): CloudStorageProvider;Gets the current provider of the default static instance.
Returns
The current provider of the default static instance.
getProviderOptions()
static getProviderOptions(): CloudStorageProviderOptionsValue;Gets the current options for the provider of the default static instance.
Returns
CloudStorageProviderOptionsValue
The current options for the provider of the default static instance.
getSupportedProviders()
static getSupportedProviders(): CloudStorageProvider[];Gets the list of supported CloudStorageProviders on the current platform.
Returns
An array of supported CloudStorageProviders.
isCloudAvailable()
static isCloudAvailable(): Promise<boolean>;Checks for an iCloud identity or a Google Drive access token on the default instance. iCloud may be unavailable right after app launch or if the user is not signed in.
Returns
Promise<boolean>
A promise that resolves to true if an identity or token is present, false otherwise.
mkdir()
static mkdir(path: string, scope?: CloudStorageScope): Promise<void>;Creates a new directory at the given path in the provider of the default static instance.
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | The directory to create. |
scope? | CloudStorageScope | The directory scope the path is in. Defaults to the default scope set for the default static instance. |
Returns
Promise<void>
A promise that resolves when the directory has been created.
readdir()
static readdir(
path: string,
scope?: CloudStorageScope,
): Promise<string[]>;Lists the contents of the directory at the given path in the provider of the default static instance.
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | The directory to list. |
scope? | CloudStorageScope | The directory scope the path is in. Defaults to the default scope set for the default static instance. |
Returns
Promise<string[]>
A promise that resolves to an array of file names, excluding '.' and '..'.
readFile()
Overload 1
static readFile(
path: string,
options?: ReadTextOptions,
): Promise<string>;Reads the contents of the file at the given path in the provider of the default static instance.
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | The file to read. |
options? | ReadTextOptions | The read options. Uses the provider's default scope if omitted. |
Returns
Promise<string>
A promise that resolves to the contents of the file.
Overload 2
static readFile(
path: string,
options: ReadBytesOptions,
): Promise<Uint8Array<ArrayBufferLike>>;Reads bytes from a file. Holds the full file in memory.
Parameters
| Parameter | Type |
|---|---|
path | string |
options | ReadBytesOptions |
Returns
Promise<Uint8Array<ArrayBufferLike>>
Overload 3
static readFile(
path: string,
options?: ReadFileOptions,
): Promise<string | Uint8Array<ArrayBufferLike>>;Reads text or bytes according to the format option.
Parameters
| Parameter | Type |
|---|---|
path | string |
options? | ReadFileOptions |
Returns
Promise<string | Uint8Array<ArrayBufferLike>>
Overload 4 (deprecated)
static readFile(
path: string,
scope?: CloudStorageScope,
): Promise<string>;Parameters
| Parameter | Type |
|---|---|
path | string |
scope? | CloudStorageScope |
Returns
Promise<string>
Deprecated
Pass the scope in an options object: { scope }.
rmdir()
static rmdir(
path: string,
options?: object,
scope?: CloudStorageScope,
): Promise<void>;Deletes the directory at the given path in the provider of the default static instance.
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | The directory to delete. |
options? | { recursive?: boolean; } | Options for the delete operation. Defaults to { recursive: false }. |
options.recursive? | boolean | - |
scope? | CloudStorageScope | The directory scope the path is in. Defaults to the default scope set for the default static instance. |
Returns
Promise<void>
A promise that resolves when the directory has been deleted.
setProvider()
static setProvider(provider: CloudStorageProvider): void;Sets the provider of the default static instance.
Parameters
| Parameter | Type | Description |
|---|---|---|
provider | CloudStorageProvider | The provider to set. |
Returns
void
setProviderOptions()
static setProviderOptions(
options: CloudStorageProviderOptionsValue,
): void;Sets the options for the provider of the default static instance.
Parameters
| Parameter | Type | Description |
|---|---|---|
options | CloudStorageProviderOptionsValue | The options to set for the provider of the default static instance. |
Returns
void
stat()
static stat(
path: string,
scope?: CloudStorageScope,
): Promise<CloudStorageFileStat>;Gets the size, creation time, and modification time of the file at the given path in the provider of the default static instance.
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | The file to stat. |
scope? | CloudStorageScope | The directory scope the path is in. Defaults to the default scope set for the default static instance. |
Returns
Promise<CloudStorageFileStat>
A promise that resolves to the CloudStorageFileStat object.
triggerSync()
static triggerSync(
path: string,
scope?: CloudStorageScope,
): Promise<void>;Requests an iCloud file download on the default instance. Does not have any effect on Google Drive.
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | The file to request a download for. |
scope? | CloudStorageScope | The directory scope the path is in. Defaults to the default scope set for the default static instance. |
Returns
Promise<void>
A promise that resolves once the download has been requested.
unlink()
static unlink(path: string, scope?: CloudStorageScope): Promise<void>;Deletes the file at the given path in the provider of the default static instance.
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | The file to delete. |
scope? | CloudStorageScope | The directory scope the path is in. Defaults to the default scope set for the default static instance. |
Returns
Promise<void>
A promise that resolves when the file has been deleted.
uploadFile()
static uploadFile(
remotePath: string,
localPath: string,
options: object,
scope?: CloudStorageScope,
): Promise<void>;Uploads the file at the given local path to the given path in the provider of the default static instance, creating it if it doesn't exist or overwriting it if it does.
Parameters
| Parameter | Type | Description |
|---|---|---|
remotePath | string | The remote path to upload to. |
localPath | string | The local path of the file to upload. |
options | { mimeType: string; } | The options for the upload. Must contain a mimeType property. |
options.mimeType | string | - |
scope? | CloudStorageScope | The directory scope the path is in. Defaults to set default scope set for the current provider. |
Returns
Promise<void>
A promise that resolves after the local iCloud container copy or the Google Drive upload completes. iOS uploads the iCloud copy asynchronously, this promise therefore does not confirm delivery.
writeFile()
Overload 1
static writeFile(
path: string,
data: string,
options?: WriteFileOptions,
): Promise<void>;Writes to the file at the given path in the provider of the default static instance, creating it if it doesn't exist or overwriting it if it does.
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | The file to write to. |
data | string | The data to write. |
options? | WriteFileOptions | The write options. Uses the provider's default scope if omitted. |
Returns
Promise<void>
A promise that resolves when the file has been written.
Overload 2
static writeFile(
path: string,
data: Uint8Array,
options: WriteFileOptions & object,
): Promise<void>;Writes bytes to a file. Requires a MIME type and holds the full file in memory.
Parameters
| Parameter | Type |
|---|---|
path | string |
data | Uint8Array |
options | WriteFileOptions & object |
Returns
Promise<void>
Overload 3 (deprecated)
static writeFile(
path: string,
data: string,
scope?: CloudStorageScope,
): Promise<void>;Parameters
| Parameter | Type |
|---|---|
path | string |
data | string |
scope? | CloudStorageScope |
Returns
Promise<void>
Deprecated
Pass the scope in an options object: { scope }.