Adobe I/O Lib 文件
云blob存储上方的节点JavaScript抽象化会暴露出像API这样的文件系统.
您可以用您的 Adobe I/ O 运行时间( a. k. a OpenWhisk) 初始化 SDK 证书.
或者,你可以带自己的云存储键。 但请注意,作为: 现在我们只支持Azure Blob存储器.
请注意,你目前必须是 Adobe 开发者 App 构建器 要使用此图书馆。 App Builder是一个完整的框架,使企业开发者能够构建并部署自定义的网络应用程序来扩展Adobe体验云解决方案并运行在Adobe基础设施上.
安装
npm install @adobe/aio-lib-files
使用
const filesLib = require('@adobe/aio-lib-files')
// init
// init sdk using OpenWhisk credentials
const files = await filesLib.init({ ow: { namespace, auth } })
// init when env vars __OW_API_KEY and __OW_NAMESPACE are set (e.g. when running in an OpenWhisk action)
const files = await filesLib.init()
// or if you want to use your own cloud storage account
const files = await filesLib.init({ azure: { storageAccount, storageAccessKey, containerName } })
// write private file
await files.write('mydir/myfile.txt', 'some private content')
// write publicly accessible file
await files.write('public/index.html', '<h1>Hello World!</h1>')
// get file url
const props = await files.getProperties('public/index.html')
console.log('props = ', props)
/*
props = { name: 'public/index.html',
creationTime: 2020-12-09T19:49:58.000Z,
lastModified: 2020-12-09T19:49:58.000Z,
etag: '"0x8D89C7B9BB75A6F"',
contentLength: 21,
contentType: 'text/html',
isDirectory: false,
isPublic: true,
url:
'https://jestaiotest.blob.core.windows.net/readme-public/public%2Findex.html' }
*/
// list all files
await files.list('mydir/') // with a trailing slash lists a directory, recursively. Returns [] if no files are recursively stored under mydir/.
await files.list('myfile') // without a trailing slash list returns [fileProps] or [] if it doesn't exist.
await files.list('/')
/*
list = [ { name: 'mydir/myfile.txt',
creationTime: 2020-12-09T19:49:57.000Z,
lastModified: 2020-12-09T19:49:57.000Z,
etag: '0x8D89C7B9BB165F8',
contentLength: 20,
contentType: 'text/plain',
isDirectory: false,
isPublic: false,
url:
'https://jestaiotest.blob.core.windows.net/readme/mydir%2Fmyfile.txt' },
{ name: 'public/index.html',
creationTime: 2020-12-09T19:49:58.000Z,
lastModified: 2020-12-09T19:49:58.000Z,
etag: '0x8D89C7B9BB75A6F',
contentLength: 21,
contentType: 'text/html',
isDirectory: false,
isPublic: true,
url:
'https://jestaiotest.blob.core.windows.net/readme-public/public%2Findex.html' } ]
*/
// read
const buffer = await files.read('mydir/myfile.txt')
buffer.toString() // 'some private content'
// pipe read stream to local file (consider using copy below)
const rdStream = await files.createReadStream('mydir/myfile.txt')
const stream = rdStream.pipe(fs.createWriteStream('my-local-file.txt'))
stream.on('finish', () => console.log('done!'))
// write read stream to remote file (consider using copy below)
const rdStream = fs.createReadStream('my-local-file.txt')
await files.write('my/remote/file.txt', rdStream)
// delete files in 'my/remote/' dir
await files.delete('my/remote/')
// delete all public files
await files.delete('public/')
// delete all files including public
await files.delete('/')
// copy - higher level utility (works likes scp)
// works for files and directories both remotely and locally, uses streams under the hood
/// upload a single file
await files.copy('my-static-app/index.html', 'public/my-static-app/index.html', { localSrc: true })
/// upload local directory recursively
await files.copy('my-static-app/', 'public/', { localSrc: true })
/// download to local directory recursively (works for files as well)
await files.copy('public/my-static-app/', 'my-static-app-copy', { localDest: true })
/// copy remote directories around (works for files as well)
await files.copy('public/my-static-app/', 'my/private/folder')
// Share private files
const presignUrl = await files.generatePresignURL('mydir/myfile.txt', { expiryInSeconds: 60 })
//Share private files with read, write, delete permissions
const rwdPresignUrl = await files.generatePresignURL('mydir/myfile.txt', { expiryInSeconds: 60, permissions: 'rwd' })
指定 URL 类型和使用
文件 SDK 支持两种预签名的 URL 访问文件
- 外部 - 基于 CDN 的 URL , 可以从任何地方访问, 假设私人文件的右前缀权限 。 您可以使用此类型的 URL 为远程计算使用大小写提供对外部系统和 API 文件的访问 .
- 内部 - 文件存储的直接URL. 这些 URL 只有在 Adobe I/ O 运行时动作中使用时才有效 。 您可以使用这种类型的 URL 来链接正在从不同的运行时命名空间处理相同文件的工人动作 。 由于没有 CDN 间接, 使用这种类型的 URL 将改善您运行时动作的性能 .
文件. get 财产 返回给定文件路径的 URL 类型( 外部为 url, 内部为 内部 Url) 文件. generate PresignURL 支持 UrlType 作为生成指定类型的预签名 URL 的选项
参见下面的用法示例 -
const { init, UrlType } = require('@adobe/aio-lib-files')
const files = await init()
// getProperties will return both internal and external URLs in return Object
const props = files.getProperties('public/my-static-app/index.html')
//generate presign URL with internal URLType
const internalPresignUrl = await files.generatePresignURL('mydir/myfile.txt', { expiryInSeconds: 60, permissions: 'rwd', urltype: UrlType.internal })
探索
goto API 密码
调试
设定 DEBUG=@adobe/aio-lib-files* 以查看调试日志.
Adobe I/O 文件存储一致性保证
强有力的一致性 保证所有操作以及文件sdk(由 filesLib.init()) (中文(简体) ).
解决问题
"[StateLib:ERROR_INTERNAL] unknown error response from provider with status: unknown"
- 使用时
@adobe/aio-lib-files与 网络软件包 请确保关闭 minific化 并能够解析 Es6 模块 。 在您的网络包配置中添加以下行 :
optimization: {
minimize: false
},
resolve: {
extensions: ['.js'],
mainFields: ['main']
}
捐款
欢迎捐款! 读取 贡献指南 更多信息.
许可证发放
这个项目根据Apache V2许可证获得许可. 见 自由主义 更多信息.