@amazon-devices/expo-file-system provides access to file system of the device. It is also capable of downloading and uploading files from network URLs.
Installation
-
Add the JavaScript library dependency in the
package.jsonfile."dependencies": { ... "@amazon-devices/expo-file-system": "~2.0.1", "expo": "~50.0.0", ... } -
Reinstall dependencies using the
npm installcommand.
Examples
This example demonstrates how to write text to a file.import React, {useEffect, useState} from 'react';
import {Alert, Button, StyleSheet, Text, View} from 'react-native';
import * as FileSystem from '@amazon-devices/expo-file-system';
export const App = () => {
const [fileContent, setFileContent] = useState<string | null>(null);
const fileUri = FileSystem.documentDirectory + 'sample.txt';
useEffect(() => {
(async () => {
const fileInfo = await FileSystem.getInfoAsync(fileUri);
if (fileInfo.exists) {
const content = await FileSystem.readAsStringAsync(fileUri);
setFileContent(content);
} else {
setFileContent('File does not exist - you need to write to file');
}
})();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const writeFile = async () => {
try {
await FileSystem.writeAsStringAsync(fileUri, 'Hello, file system!');
Alert.alert('File written successfully!');
} catch (error) {
console.error('Error writing the file:', error);
}
};
const readFile = async () => {
try {
const content = await FileSystem.readAsStringAsync(fileUri);
setFileContent(content);
} catch (error) {
console.error('Error reading the file:', error);
}
};
return (
<View style={styles.container}>
<Text style={styles.text}>File Content: {fileContent}</Text>
<Button title="Write to File" onPress={writeFile} />
<Button title="Read from File" onPress={readFile} />
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'white',
},
text: {
fontSize: 30,
textAlign: 'center',
},
});
import * as FileSystem from '@amazon-devices/expo-file-system';
import {DownloadOptions, FileSystemUploadOptions} from '@amazon-devices/expo-file-system';
import React, {useState} from 'react';
import {Button, ScrollView, StyleSheet, Text, View} from 'react-native';
export const App = () => {
const downloadAsyncArgs: {
uri: string;
fileUri: string;
options: DownloadOptions;
} = {
uri: 'https://httpbin.org/get',
fileUri: FileSystem.documentDirectory + 'downloadResponse.json',
options: {
md5: true,
cache: false,
headers: {
Accept: 'application/json',
},
},
};
const uploadAsyncArgs: {
url: string;
fileUri: string;
options: FileSystemUploadOptions;
} = {
url: 'https://httpbin.org/patch',
fileUri: FileSystem.documentDirectory + 'downloadResponse.json',
options: {
fieldName: 'data',
httpMethod: 'PATCH',
uploadType: FileSystem.FileSystemUploadType.BINARY_CONTENT,
headers: {
Accept: 'application/json',
},
},
};
const [downloadResponse, setDownloadResponse] =
useState<FileSystem.FileSystemDownloadResult>();
const [uploadResponse, setUploadResponse] =
useState<FileSystem.FileSystemUploadResult>();
const handleDownloadAsync = async () => {
const {uri, fileUri, options} = downloadAsyncArgs;
FileSystem.downloadAsync(uri, fileUri, options)
.then(setDownloadResponse)
.catch((error) => setDownloadResponse(error.message));
};
const handleUploadAsync = async () => {
const {url, fileUri, options} = uploadAsyncArgs;
FileSystem.uploadAsync(url, fileUri, options)
.then(setUploadResponse)
.catch((error) => setUploadResponse(error.message));
};
return (
<ScrollView style={styles.container}>
<Button title="Download file" onPress={handleDownloadAsync} />
<View style={styles.cell}>
<Text style={styles.cellText}>
{JSON.stringify(downloadResponse, null, 2)}
</Text>
</View>
<Button title="Upload file" onPress={handleUploadAsync} />
<View style={styles.cell}>
<Text style={styles.cellText}>
{JSON.stringify(uploadResponse, null, 2)}
</Text>
</View>
</ScrollView>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 20,
backgroundColor: 'white',
},
cell: {
flex: 1,
padding: 10,
borderWidth: 2,
justifyContent: 'center',
borderColor: 'white',
},
cellText: {
fontSize: 24,
color: 'black',
},
});
API reference
See the documentation page for information about this library and API reference: Official Expo documentation for expo-file-system.Constants
These constants are URIs pointing to a directory.| Constant | Description |
|---|---|
documentDirectory | Where user documents for this app are stored, for example /data/. |
cacheDirectory | Where temporary files used by this app are stored, for example /cache/. |
bundleDirectory | Where assets bundled with the application are stored, for example /pkg/bundle/index.hermes.bundle. |
Methods
| Method | Description |
|---|---|
copyAsync | Creates a copy of a file or directory |
deleteAsync | Deletes a file or directory |
downloadAsync | Downloads the contents at a remote URI to a file in the app’s file system |
getContentUriAsync | Takes a file:// URI and converts it into a content URI (content://) so that it can be accessed by other applications outside of Expo |
getFreeDiskStorageAsync | Gets the available internal disk storage size, in bytes |
getInfoAsync | Gets metadata information about a file, directory or external content/asset |
getTotalDiskCapacityAsync | Gets total internal disk storage size, in bytes |
makeDirectoryAsync | Creates a new empty directory |
moveAsync | Moves a file or directory to a new location |
readAsStringAsync | Reads the entire contents of a file as a string |
readDirectoryAsync | Enumerates the contents of a directory |
uploadAsync | Uploads the contents of the file pointed by fileUri to the remote url |
writeAsStringAsync | Writes the entire contents of a file as a string |
Classes
The following classes aren’t supported on Vega.| Class | Description | Supported |
|---|---|---|
DownloadResumable | Expo documentation: https://docs.expo.dev/versions/v53.0.0/sdk/filesystem/#classes | ❌ No |
FileSystemCancellableNetworkTask | Expo documentation: https://docs.expo.dev/versions/v53.0.0/sdk/filesystem/#filesystemcancellablenetworktask | ❌ No |
UploadTask | Expo documentation: https://docs.expo.dev/versions/v53.0.0/sdk/filesystem/#uploadtask | ❌ No |
Supported URI schemes
In this table, you can see what type of URI can be handled by each method.| Method | Supported URI |
|---|---|
getInfoAsync | No scheme, for example /data/file.txt |
readAsStringAsync | No scheme |
writeAsStringAsync | No scheme |
deleteAsync | No scheme |
moveAsync | Source: no scheme; Destination: no scheme |
copyAsync | Source: no scheme; Destination: no scheme |
makeDirectoryAsync | No scheme |
readDirectoryAsync | No scheme |
downloadAsync | Source: http://, https://; Destination: no scheme |
uploadAsync | Source: no scheme; Destination: http://, https:// |
createDownloadResumable | Source: http://, https://; Destination: no scheme |
Known issues and limitations
On Vega,getTotalDiskCapacityAsync always returns 0.
Supported versions
| Package Version | Based On | @amazon-devices/react-native-kepler version |
|---|---|---|
| 2.0.x | 15.8.0 | 2.0.x |

