Websync 就像aws s3 sync ,具有自动 CloudFront 失效等功能。
Websync 同步旨在替代aws s3 sync 。 Websync 与 AWS cli 一样,使用 s3 前缀同步本地目录,反之亦然。 Websync 通过在任何关联的 CloudFront 发行版上自动创建优化的失效,并使用 JSON 或 JavaScript 和编程 API 公开表达性配置系统(在 CLI 界面之上)来扩展这些功能。
# Install global cli, the `websync` command
npm i -g websync
# Install local
npm i websync # Parse configuration from `websync.json` or `websync.js`
websync
# Parse configuration explicitly
websync --config ./myConfig.js
# With command line options
websync ./www s3://mybucket.io
# General
websync [source] [target] [...options]source源容器(本地目录或 S3 存储桶): ./myDirectorytarget目标容器(S3 存储桶或本地目录): s3://my-bucketconfig显式配置文件(JSON 或 JavaScript): --config ./myConfig.jsoninclude Glob 模式来过滤文件(从源)以包含: --include **/*.extexclude Glob 模式来过滤要排除的文件(从源): --exclude **/*.extdiffBy覆盖项目比较的属性( modtime或默认size : modtime ): --diffBy sizewildcardPolicy覆盖通配符策略( majority 、 unanimous或minority ,默认为: majority ): --wildcardPolicy unanimouswildcardAll将通配符附加到所有失效路径(注意:这不会更改失效路径解析),对于使查询字符串路径失效很有用: --wildcardAllinvalidateDeletes使从目标中删除的项目的路径无效。对于您不希望用户能够再访问项目的情况很有用: --invalidateDeletesdistribution一个或多个 CloudFront 分配 ID(注意:这会覆盖分配发现): --distribution <DIST ID> --distribution <ANOTHER DIST ID>yes跳过所有带有“yes”响应的提示(注意:如果发生超过 500 次失效,websync 会警告您,因为这需要付款): --yes注意:配置文件中提供了更多选项
注意:所有命令行参数都会覆盖配置文件选项。此外, source和target是必需的,但可以通过 CLI 或配置文件提供
配置文件可以提供 CLI 中可用的所有选项,并添加modifiers ,这是一个为 S3 put 操作提供显式参数的灵活系统。
配置文件的修饰符对象是一个对象,其中keys为 Glob Patterns, values S3.putObject Params ,或者返回S3.putObject Params或解析S3.putObject Params的 Promise 的函数。请注意,如果提供了一个函数(无论是否异步),它将使用单个Item参数来调用,该参数将表示SOURCE容器中的文件或对象。注意:源文件可以匹配多个修饰符,从而使内容保持干燥。
JavaScript 配置。请参阅示例了解上下文。
const Path = require ( 'path' )
const DOWNLOAD_CONTENT_TYPE = 'application/octet-stream'
const DOWNLOAD_TAG = 'Downloadable'
const REDIRECT_TAG = 'Redirectable'
const makeDispositionName = fileName =>
` ${ Path . basename ( fileName ) . split ( '.' ) [ 0 ] } - ${ Date . now ( ) } ${ Path . extname ( fileName ) } `
module . exports = {
source : './public' ,
target : 's3://websync-complex-example-bucket' ,
modifiers : {
// This matches all files, provides Plain Object
'**/*' : {
Metadata : {
'source-user' : process . env . USER ,
} ,
} ,
// Matches all files in downloads, provides a synchronous function
'downloads/**/*' : item => ( {
ContentType : DOWNLOAD_CONTENT_TYPE ,
ContentDisposition : `attachment; filename=" ${ makeDispositionName ( item . key ) } "` ,
Tagging : DOWNLOAD_TAG ,
} ) ,
// Matches any file with the `.redirect` extension, provides an asynchronous funcion
'*.redirect' : async item => ( {
WebsiteRedirectLocation : ( await item . read ( ) ) . toString ( ) . trim ( ) ,
ContentType : 'text/html' ,
Tagging : REDIRECT_TAG ,
} ) ,
} ,
} JSON 配置。请参阅示例了解上下文。在下面的示例中, !*.*模式匹配任何没有扩展名的项目,即“another-page”,并用text/html覆盖隐含的Content-Type以获得简单静态网站的干净路径。
{
"source" : " ./public " ,
"target" : " s3://websync-basic-example-bucket " ,
"exclude" : " *.exclude " ,
"modifiers" : {
"!*.*" : {
"ContentType" : " text/html "
}
}
} Websync 的Item对象是一个接口,抽象地表示本地文件或S3对象。对于Configuration File ,传递给modifier函数的Item对象始终来自源容器(本地目录或S3 Bucket)。所有Item都遵循以下接口:
interface Item {
// The "key" (path or S3 Object key)
key : string
// Last modification time
modtime : Date
// Size in bytes of the Item
size : number
// Whether item is a symbolic link (always false for S3)
isSymbolicLink : boolean
// Read the *entire* body of the item
read ( ) : Promise < Buffer >
}Websync 的失效系统会自动创建适应所提供的wildcard策略所需的最少量的失效路径。它通过创建目标和source的diff以及两棵树来实现这一点: diff中的一项和target中的所有项。然后,它遍历diff (从根开始)树,并将失效的子级数与未失效的子级数进行比较——这就是wildcard策略产生差异的地方。此外,websync 将检测通配符的给定路径何时应使其所有子路径无效,或仅使其直接子路径无效,从而生成最佳的无效路径。
注意: wildcardAll选项不会更改失效路径的生成,而是将通配符附加到生成的每个路径中。这对于使给定对象的查询字符串路径无效等很有用。
有关失效如何在 CloudFront 上工作的更多信息,请参阅 AWS 文档。
通配符策略确定给定路径何时将被通配,从而使其全部或仅其直接子级无效,以减少生成的无效路径的数量。从最不严格到最严格的三种政策包括minority 、 majority和unanimous 。
当给定路径的少数子路径无效时,该路径将成为通配符。注意:当需要失效时,这总是会导致/*失效路径。
所有目标项目:
//cssmain.cssvendor.css/jsmain.jsindex.html无效项目:
/index.html失效路径:
/*当给定路径的大多数子路径失效时,该路径将成为通配符。
所有目标项目:
//cssmain.cssvendor.css/jsmain.jsindex.html无效项目:
//cssmain.cssvendor.cssindex.html失效路径:
/css/*/index.html当给定路径的所有子路径都失效时,该路径将成为通配符。
所有目标项目:
//cssmain.cssvendor.css/jsmain.jsindex.html无效项目:
//cssmain.css/js/main.jsindex.html失效路径:
/css/main.css/js/*/index.html