在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):pgherveou/gulp-awspublish开源软件地址(OpenSource Url):https://github.com/pgherveou/gulp-awspublish开源编程语言(OpenSource Language):JavaScript 100.0%开源软件介绍(OpenSource Introduction):gulp-awspublish
UsageFirst, install npm install --save-dev gulp-awspublish Then, add it to your var awspublish = require("gulp-awspublish");
gulp.task("publish", function() {
// create a new publisher using S3 options
// http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#constructor-property
var publisher = awspublish.create(
{
region: "your-region-id",
params: {
Bucket: "..."
}
},
{
cacheFileName: "your-cache-location"
}
);
// define custom headers
var headers = {
"Cache-Control": "max-age=315360000, no-transform, public"
// ...
};
return (
gulp
.src("./public/*.js")
// gzip, Set Content-Encoding headers and add .gz extension
.pipe(awspublish.gzip({ ext: ".gz" }))
// publisher will add Content-Length, Content-Type and headers specified above
// If not specified it will set x-amz-acl to public-read by default
.pipe(publisher.publish(headers))
// create a cache file to speed up consecutive uploads
.pipe(publisher.cache())
// print upload updates to console
.pipe(awspublish.reporter())
);
});
// output
// [gulp] [create] file1.js.gz
// [gulp] [create] file2.js.gz
// [gulp] [update] file3.js.gz
// [gulp] [cache] file3.js.gz
// ...
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["s3:ListBucket"],
"Resource": ["arn:aws:s3:::BUCKETNAME"]
},
{
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:PutObjectAcl",
"s3:GetObject",
"s3:GetObjectAcl",
"s3:DeleteObject",
"s3:ListMultipartUploadParts",
"s3:AbortMultipartUpload"
],
"Resource": ["arn:aws:s3:::BUCKETNAME/*"]
}
]
} Bucket permissionsBy default, the plugin works only when public access to the bucket is not blocked:
When dealing with a private bucket, make sure to pass the option publisher.publish({}, { noAcl: true });
publisher.publish({ "x-amz-acl": "something" }); Testing
{
"params": {
"Bucket": "<test-bucket-name>"
},
"credentials": {
"accessKeyId": "<your-access-key-id>",
"secretAccessKey": "<your-secret-access-key>",
"signatureVersion": "v3"
}
} APIawspublish.gzip(options)create a through stream, that gzip file and add Content-Encoding header.
Available options:
awspublish.create(AWSConfig, cacheOptions)Create a Publisher.
The AWSConfig object is used to create an The cacheOptions object allows you to define the location of the cached hash digests. By default, they will be saved in your projects root folder in a hidden file called '.awspublish-' + 'name-of-your-bucket'. Adjusting upload timeoutThe AWS client has a default timeout which may be too low when pushing large files (> 50mb).
To adjust timeout, add CredentialsBy default, gulp-awspublish uses the credential chain specified in the AWS docs. Here are some example credential configurations: Hardcoded credentials (Note: We recommend you not hard-code credentials inside an application. Use this method only for small personal scripts or for testing purposes.): var publisher = awspublish.create({
region: "your-region-id",
params: {
Bucket: "..."
},
credentials: {
accessKeyId: "akid",
secretAccessKey: "secret"
}
}); Using a profile by name from var AWS = require("aws-sdk");
var publisher = awspublish.create({
region: "your-region-id",
params: {
Bucket: "..."
},
credentials: new AWS.SharedIniFileCredentials({ profile: "myprofile" })
}); Instead of putting anything in the configuration object, you can also provide the following environment variables: Publisher.publish([headers], [options])Create a through stream, that push files to s3.
Files that go through the stream receive extra properties:
publisher.cache()Create a through stream that create or update a cache file using file s3 path and file etag. Consecutive runs of publish will use this file to avoid reuploading identical files. Cache file is save in the current working dir and is named Publisher.sync([prefix], [whitelistedFiles])create a transform stream that delete old files from the bucket.
e.g. // only directory bar will be synced
// files in folder /foo/bar and file baz.txt will not be removed from the bucket despite not being in your local folder
gulp
.src("./public/*")
.pipe(publisher.publish())
.pipe(publisher.sync("bar", [/^foo\/bar/, "baz.txt"]))
.pipe(awspublish.reporter());
// this will publish and sync bucket files with the one in your public directory
gulp
.src("./public/*")
.pipe(publisher.publish())
.pipe(publisher.sync())
.pipe(awspublish.reporter());
// output
// [gulp] [create] file1.js
// [gulp] [update] file2.js
// [gulp] [delete] file3.js
// ... Publisher.clientThe awspublish.reporter([options])Create a reporter that logs s3.path and s3.state (delete, create, update, cache, skip). Available options:
// this will publish,sync bucket files and print created, updated and deleted files
gulp
.src("./public/*")
.pipe(publisher.publish())
.pipe(publisher.sync())
.pipe(
awspublish.reporter({
states: ["create", "update", "delete"]
})
); ExamplesRename file & directoryYou can use // see examples/rename.js
gulp
.src("examples/fixtures/*.js")
.pipe(
rename(function(path) {
path.dirname += "/s3-examples";
path.basename += "-s3";
})
)
.pipe(publisher.publish())
.pipe(awspublish.reporter());
// output
// [gulp] [create] s3-examples/bar-s3.js
// [gulp] [create] s3-examples/foo-s3.js Upload file in parallelYou can use var parallelize = require("concurrent-transform");
gulp
.src("examples/fixtures/*.js")
.pipe(parallelize(publisher.publish(), 10))
.pipe(awspublish.reporter()); Upload both gzipped and plain files in one streamYou can use the var merge = require("merge-stream");
var gzip = gulp.src("public/**/*.js").pipe(awspublish.gzip());
var plain = gulp.src(["public/**/*", "!public/**/*.js"]);
merge(gzip, plain)
.pipe(publisher.publish())
.pipe(publisher.sync())
.pipe(awspublish.reporter()); Pluginsgulp-awspublish-routerA router for defining file-specific rules https://www.npmjs.org/package/gulp-awspublish-router gulp-cloudfront-invalidate-aws-publishInvalidate cloudfront cache based on output from awspublish https://www.npmjs.com/package/gulp-cloudfront-invalidate-aws-publish License |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论