as

Settings
Sign out
Notifications
Alexa
亚马逊应用商店
AWS
文档
Support
Contact Us
My Cases
新手入门
设计和开发
应用发布
参考
支持

@amazon-devices/kepler-file-system

@amazon-devices/kepler-file-system

Kepler文件系统API通过启用文件系统查询、遍历、读取和写入操作为Kepler提供支持。

备注

应用在沙盒环境中运行,只能访问设备文件系统的部分内容。Kepler在沙盒中提供以下特定于应用的目录。

路径 可写 描述
/pkg 应用程序包的根目录。
/pkg/assets 通过程序包安装的资产。
/pkg/lib 由程序包安装的应用入口点组件和任何其他库。
/data 应用数据的可写入位置。在设备重启和程序包升级期间保持不变,不在应用或服务之间共享。应用卸载时,此目录不会保留。
/tmp 每个应用都有自己的临时存储空间,不能与任何其他应用或服务共享。不会保留,会在设备重启期间删除。
/proc 应用只可使用/proc/self

开始使用

设置

  1. 将以下库依赖项添加到package.json文件的dependencies部分。

    已复制到剪贴板。

     "@amazon-devices/kepler-file-system": "~0.0.1"
    
  2. 使用npm install命令重新生成package-lock.json文件。

用法

以下示例使用async/await来处理FileSystem方法返回的Promise。这种模式为处理异步操作提供了一种更简洁的方式。

打开一个文件

此示例使用async/await来处理FileSystem.openFile() 返回的Promise。

已复制到剪贴板。

import { KeplerFileSystem as FileSystem } from '@amazon-devices/kepler-file-system';

const handleOpenFile = async () => {
  FileSystem.openFile('/data/fileSystemTestFile', 0)
    .then((response: any) => console.log(response))
    .catch((error: any) => console.error(error));
};

删除一个文件

此示例使用async/await来处理FileSystem.removeFile() 返回的Promise。

已复制到剪贴板。

import { KeplerFileSystem as FileSystem } from '@amazon-devices/kepler-file-system';

const handleRemoveFile = async () => {
  FileSystem.removeFile('/data/fileSystemTestFile')
    .then((response: any) => console.log(response))
    .catch((error: any) => console.error(error));
};

删除一个目录

此示例使用async/await来处理FileSystem.removeDir() 返回的Promise。

已复制到剪贴板。

import { KeplerFileSystem as FileSystem } from '@amazon-devices/kepler-file-system';

const handleRemoveDir = async () => {
  FileSystem.removeDir('/data')
    .then((response: any) => console.log(response))
    .catch((error: any) => console.error(error));
};

递归地删除一个目录

此示例使用async/await来处理FileSystem.removeDirAll() 返回的Promise。

已复制到剪贴板。

import { KeplerFileSystem as FileSystem } from '@amazon-devices/kepler-file-system';

const handleRemoveDirAll = async () => {
  FileSystem.removeDirAll('/data')
    .then((response: any) => console.log(response))
    .catch((error: any) => console.error(error));
};

将文件读取为字符串

此示例使用async/await来处理FileSystem.readFileAsString() 返回的Promise。

已复制到剪贴板。

import { KeplerFileSystem as FileSystem } from '@amazon-devices/kepler-file-system';

const handleReadFileAsString = async () => {
  FileSystem.readFileAsString('/data/fileToReadWrite', 'UTF-8')
    .then((response: any) => console.log(response))
    .catch((error: any) => console.error(error));
};

将字符串写入文件

此示例使用async/await来处理FileSystem.writeStringToFile() 返回的Promise。

已复制到剪贴板。

import { KeplerFileSystem as FileSystem } from '@amazon-devices/kepler-file-system';

const handleWriteStringToFile = async () => {
  FileSystem.writeStringToFile('/data/fileToReadWrite', 'Hello world!', 'UTF-8')
    .then((response: any) => console.log(response))
    .catch((error: any) => console.error(error));
};

访问文件元数据

此示例使用async/await来处理FileSystem.getMetadata() 返回的Promise。

已复制到剪贴板。

import { KeplerFileSystem as FileSystem } from '@amazon-devices/kepler-file-system';

export const handleGetMetadata = async () => {
  FileSystem.getMetadata('/data/fileSystemTestFile', false)
    .then((response: any) => console.log(response))
    .catch((error: any) => console.error(error));
};

访问目录条目

此示例使用async/await来处理FileSystem.getEntries() 返回的Promise。

已复制到剪贴板。

import { KeplerFileSystem as FileSystem } from '@amazon-devices/kepler-file-system';

const handleGetEntries = async () => {
  FileSystem.getEntries('/data')
    .then((response: any) => console.log(response))
    .catch((error: any) => console.error(error));
};

模块


Last updated: 2025年10月2日