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