### Start Example App Packager
Source: https://github.com/kuatsu/react-native-cloud-storage/blob/master/CONTRIBUTING.md
Starts the Metro server for the example application. Changes in JavaScript code will be reflected without a rebuild.
```sh
pnpm example start
```
--------------------------------
### Run Development Server
Source: https://github.com/kuatsu/react-native-cloud-storage/blob/master/apps/docs/README.md
Starts the development server for the documentation site from the repository root. Ensures workspace dependencies are resolved correctly.
```sh
pnpm --filter react-native-cloud-storage-docs dev
```
--------------------------------
### Install iOS Pods
Source: https://github.com/kuatsu/react-native-cloud-storage/blob/master/apps/docs/content/docs/installation/react-native.mdx
Navigate to the ios directory and install the necessary pods for the iOS native module.
```bash
cd ios && pod install && cd ..
```
--------------------------------
### Run Example App on Android
Source: https://github.com/kuatsu/react-native-cloud-storage/blob/master/CONTRIBUTING.md
Builds and runs the example application on an Android device or emulator. Native code changes require a rebuild.
```sh
pnpm example android
```
--------------------------------
### Install Expo Cloud Storage
Source: https://github.com/kuatsu/react-native-cloud-storage/blob/master/README.md
Install the library for Expo projects.
```sh
npx expo install react-native-cloud-storage
```
--------------------------------
### Run Example App on iOS
Source: https://github.com/kuatsu/react-native-cloud-storage/blob/master/CONTRIBUTING.md
Builds and runs the example application on an iOS simulator or device. Native code changes require a rebuild.
```sh
pnpm example ios
```
--------------------------------
### Quick Start: Read and Write Files
Source: https://github.com/kuatsu/react-native-cloud-storage/blob/master/README.md
Demonstrates basic file operations (write and read) using the CloudStorage API. Ensure the cloud storage is available and configure provider options if using Google Drive.
```jsx
import React from 'react';
import { Platform, View, Text, Button } from 'react-native';
import { CloudStorage, CloudStorageProvider, useIsCloudAvailable } from 'react-native-cloud-storage';
const App = () => {
const cloudAvailable = useIsCloudAvailable();
React.useEffect(() => {
if (CloudStorage.getProvider() === CloudStorageProvider.GoogleDrive) {
// get access token via @react-native-google-signin/google-signin or similar
CloudStorage.setProviderOptions({ accessToken: 'some-access-token' });
}
}, []);
const writeToCloud = async () => {
await CloudStorage.writeFile('/file.txt', 'Hello, world!');
console.log('Successfully wrote file to cloud');
};
const readFromCloud = async () => {
const value = await CloudStorage.readFile('/file.txt');
console.log('Successfully read file from cloud:', value);
};
return (
{cloudAvailable ? (
<>
>
) : (
The cloud storage is not available. Are you logged in?
)}
);
};
```
--------------------------------
### Install React Native Cloud Storage
Source: https://github.com/kuatsu/react-native-cloud-storage/blob/master/README.md
Install the library for React Native projects and link native dependencies.
```sh
npm install react-native-cloud-storage
cd ios && pod install
```
--------------------------------
### Basic File Read/Write Example
Source: https://github.com/kuatsu/react-native-cloud-storage/blob/master/apps/docs/content/docs/index.mdx
Demonstrates writing and reading a JSON file using the AppData scope. This scope is typically used for app-private storage.
```typescript
import { CloudStorage, CloudStorageScope } from 'react-native-cloud-storage';
// Write a JSON file to the app-private scope (iCloud on iOS, Google Drive elsewhere).
await CloudStorage.writeFile('/state.json', JSON.stringify({ level: 7 }), CloudStorageScope.AppData);
// Read it back later.
if (await CloudStorage.exists('/state.json', CloudStorageScope.AppData)) {
const json = await CloudStorage.readFile('/state.json', CloudStorageScope.AppData);
const { level } = JSON.parse(json);
}
```
--------------------------------
### Local Development Commands
Source: https://github.com/kuatsu/react-native-cloud-storage/blob/master/apps/docs/README.md
Commands for local development: starting the dev server, generating API reference, building for production, and type checking.
```sh
pnpm dev # generate API reference, then start the dev server
pnpm docs:api # (re)generate content/docs/api from the library's TSDoc
pnpm build # production build
pnpm typecheck # generate API + types, then tsc --noEmit
```
--------------------------------
### Google Drive Authentication and File Write with Expo AuthSession
Source: https://github.com/kuatsu/react-native-cloud-storage/blob/master/apps/docs/content/docs/installation/configure-google-drive.mdx
This example demonstrates how to use Expo's AuthSession API to authenticate with Google, obtain an access token, and then use it with react-native-cloud-storage to write a file to Google Drive's AppData scope. It includes UI elements for sign-in and file writing.
```tsx
import { useEffect, useState } from 'react';
import { StyleSheet, Text, View, Button } from 'react-native';
import * as WebBrowser from 'expo-web-browser';
import * as Google from 'expo-auth-session/providers/google';
import { CloudStorage, CloudStorageProvider, CloudStorageScope } from 'react-native-cloud-storage';
WebBrowser.maybeCompleteAuthSession();
const App: React.FC = () => {
const [accessToken, setAccessToken] = useState(null);
const [request, response, promptAsync] = Google.useAuthRequest({
androidClientId: 'GOOGLE_GUID.apps.googleusercontent.com',
// if you're also deploying to web, uncomment the next line
// webClientId: 'GOOGLE_GUID.apps.googleusercontent.com',
scopes: ['https://www.googleapis.com/auth/drive.appdata'],
});
useEffect(() => {
if (response?.type === 'success') {
setAccessToken(response.authentication.accessToken);
}
if (accessToken && CloudStorage.getProvider() === CloudStorageProvider.GoogleDrive) {
CloudStorage.setProviderOptions({ accessToken });
}
}, [response, accessToken]);
const writeFileAsync = () => {
return CloudStorage.writeFile('test.txt', 'Hello World', CloudStorageScope.AppData);
};
return (
{CloudStorage.getProvider() === CloudStorageProvider.GoogleDrive && !accessToken ? (
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
```
--------------------------------
### Initialize iCloud Storage Instances
Source: https://github.com/kuatsu/react-native-cloud-storage/blob/master/apps/docs/content/docs/guides/migrating-icloud-documents.mdx
Create two instances of CloudStorage: one for legacy sandbox documents and one for the user-facing iCloud Documents folder. The legacy instance requires `documentsMode: 'legacy_sandbox'`, while the standard instance uses the default `icloud` mode.
```typescript
import { CloudStorage, CloudStorageProvider, CloudStorageScope } from 'react-native-cloud-storage';
const legacyICloudStorage = new CloudStorage(CloudStorageProvider.ICloud, {
scope: CloudStorageScope.Documents,
documentsMode: 'legacy_sandbox',
});
const iCloudStorage = new CloudStorage(CloudStorageProvider.ICloud, {
scope: CloudStorageScope.Documents,
// `documentsMode` defaults to `icloud`, so we can omit it here
});
```
--------------------------------
### Run Unit Tests
Source: https://github.com/kuatsu/react-native-cloud-storage/blob/master/CONTRIBUTING.md
Executes the project's unit tests. It is recommended to add new tests for any changes made to the codebase.
```sh
pnpm test
```
--------------------------------
### Publish New Versions to npm
Source: https://github.com/kuatsu/react-native-cloud-storage/blob/master/CONTRIBUTING.md
Uses release-it to manage the process of publishing new versions to npm, including version bumping and tag creation.
```sh
pnpm package release
```
--------------------------------
### Write and Read a File
Source: https://github.com/kuatsu/react-native-cloud-storage/blob/master/apps/docs/content/docs/guides/quick-start.mdx
Write data to a file in cloud storage and then read it back. Supports app-private scopes for data storage.
```typescript
import { CloudStorage, CloudStorageScope } from 'react-native-cloud-storage';
// Write a file to the app-private scope.
await CloudStorage.writeFile('/user.json', JSON.stringify({ name: 'Ada' }), CloudStorageScope.AppData);
// Read it back.
if (await CloudStorage.exists('/user.json', CloudStorageScope.AppData)) {
const json = await CloudStorage.readFile('/user.json', CloudStorageScope.AppData);
const user = JSON.parse(json);
}
```
--------------------------------
### Set Google Drive Access Token
Source: https://github.com/kuatsu/react-native-cloud-storage/blob/master/apps/docs/content/docs/guides/quick-start.mdx
Provide an access token for Google Drive if it's the selected provider. This is necessary for authentication when using Google Drive.
```typescript
import { CloudStorage, CloudStorageProvider } from 'react-native-cloud-storage';
if (CloudStorage.getProvider() === CloudStorageProvider.GoogleDrive) {
CloudStorage.setProviderOptions({ accessToken: 'your_access_token' });
}
```
--------------------------------
### Configure Plugin Options in app.json
Source: https://github.com/kuatsu/react-native-cloud-storage/blob/master/apps/docs/content/docs/installation/expo.mdx
Configure the react-native-cloud-storage Expo config plugin with options like iCloudContainerEnvironment and iCloudContainerIdentifier in your app.json.
```json
{
"expo": {
"plugins": [
[
"react-native-cloud-storage",
{
"iCloudContainerEnvironment": "Development",
"iCloudContainerIdentifier": "iCloud.mycompany.icloud"
}
]
]
}
}
```
--------------------------------
### Use useCloudFile Hook
Source: https://github.com/kuatsu/react-native-cloud-storage/blob/master/apps/docs/content/docs/guides/quick-start.mdx
Manage a single file's content within a React component using the `useCloudFile` hook. Provides helpers for writing and removing file content.
```tsx
import { Button, Text, View } from 'react-native';
import { CloudStorageScope, useCloudFile } from 'react-native-cloud-storage';
function Profile() {
const { content, write, remove } = useCloudFile('/user.json', CloudStorageScope.AppData);
return (
{content ?? 'No profile saved yet'} write(JSON.stringify({ name: 'Ada' }))} />
);
}
```
--------------------------------
### Verify TypeScript and Linting
Source: https://github.com/kuatsu/react-native-cloud-storage/blob/master/CONTRIBUTING.md
Runs TypeScript type checking and Oxlint for code quality verification. These checks are performed by pre-commit hooks.
```sh
pnpm typecheck
```
```sh
pnpm lint
```
--------------------------------
### Instantiate CloudStorage for Specific Providers
Source: https://github.com/kuatsu/react-native-cloud-storage/blob/master/apps/docs/content/docs/guides/using-multiple-providers.mdx
Create separate CloudStorage instances for iCloud and Google Drive. This allows your app to offer multiple cloud backup options to the user simultaneously. Note that the iCloud provider is only available on iOS.
```typescript
import { CloudStorageProvider, CloudStorageScope, CloudStorage } from 'react-native-cloud-storage';
// WARNING: This will throw an error if the iCloud provider is not available on the platform (i.e. not on iOS)
const iCloudStorage = new CloudStorage(CloudStorageProvider.ICloud);
// You can pass provider options directly in the constructor, or use `googleDriveStorage.setProviderOptions()`
const googleDriveStorage = new CloudStorage(CloudStorageProvider.GoogleDrive, { accessToken: 'some_access_token' });
// Then, use the provider-specific instances
const handleSaveFileToICloud = async () => {
await iCloudStorage.writeFile('/test.txt', 'Hello, iCloud!');
};
const handleSaveFileToGoogleDrive = async () => {
await googleDriveStorage.writeFile('/test.txt', 'Hello, Google Drive!');
};
```
--------------------------------
### Recursively Migrate Directory Contents
Source: https://github.com/kuatsu/react-native-cloud-storage/blob/master/apps/docs/content/docs/guides/migrating-icloud-documents.mdx
This function recursively reads entries from the legacy iCloud storage and copies them to the new iCloud storage. It handles directory creation and file writing. Ensure the `toChildPath` helper function is defined or replaced with equivalent logic.
```typescript
const toChildPath = (parentPath: string, name: string): string => {
return parentPath === '/' ? `/${name}` : `${parentPath}/${name}`;
};
const migrateDirectory = async (directoryPath = '/'): Promise => {
const entries = await legacyICloudStorage.readdir(directoryPath, CloudStorageScope.Documents);
for (const entry of entries) {
const sourcePath = toChildPath(directoryPath, entry);
const sourceStat = await legacyICloudStorage.stat(sourcePath, CloudStorageScope.Documents);
if (sourceStat.isDirectory()) {
const targetDirectoryExists = await iCloudStorage.exists(sourcePath, CloudStorageScope.Documents);
if (!targetDirectoryExists) {
await iCloudStorage.mkdir(sourcePath, CloudStorageScope.Documents);
}
await migrateDirectory(sourcePath);
continue;
}
const fileContent = await legacyICloudStorage.readFile(sourcePath, CloudStorageScope.Documents);
await iCloudStorage.writeFile(sourcePath, fileContent, CloudStorageScope.Documents);
}
};
```
--------------------------------
### Fix Formatting Errors
Source: https://github.com/kuatsu/react-native-cloud-storage/blob/master/CONTRIBUTING.md
Applies automatic code formatting fixes using Oxlint. This command should be run to ensure code style consistency.
```sh
pnpm lint -- --fix
```
--------------------------------
### Enable Strict Filename Handling for Google Drive
Source: https://github.com/kuatsu/react-native-cloud-storage/blob/master/apps/docs/content/docs/guides/google-drive-files-same-name.mdx
Opt into throwing an error when multiple files with the same name are detected in Google Drive. This setting is specific to Google Drive and does not affect other providers like iCloud. It's recommended to use this only when you need to enforce unique filenames.
```typescript
CloudStorage.setProviderOptions({ strictFilenames: true });
```
--------------------------------
### Add Plugin to app.json
Source: https://github.com/kuatsu/react-native-cloud-storage/blob/master/apps/docs/content/docs/installation/expo.mdx
Add the react-native-cloud-storage library to the 'plugins' section of your app.json to enable the Expo config plugin for native code integration.
```json
{
"expo": {
"plugins": ["react-native-cloud-storage"]
}
}
```
--------------------------------
### Provide Google Drive Access Token
Source: https://github.com/kuatsu/react-native-cloud-storage/blob/master/apps/docs/content/docs/installation/configure-google-drive.mdx
Set the Google Drive access token for react-native-cloud-storage after acquiring it. Ensure the provider is set to Google Drive before calling this function.
```typescript
import { CloudStorage, CloudStorageProvider } from 'react-native-cloud-storage';
if (CloudStorage.getProvider() === CloudStorageProvider.GoogleDrive) {
CloudStorage.setProviderOptions({ accessToken: 'some_access_token' });
}
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.