All Projects → pgherveou → Gulp Awspublish

pgherveou / Gulp Awspublish

Licence: mit
gulp plugin to publish files to amazon s3

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Gulp Awspublish

Gulp Remember
A plugin for gulp that remembers and recalls files passed through it
Stars: ✭ 149 (-62.56%)
Mutual labels:  gulp, stream
Gulp Run
Pipe to shell commands in gulp
Stars: ✭ 148 (-62.81%)
Mutual labels:  gulp, stream
Iostreams
IOStreams is an incredibly powerful streaming library that makes changes to file formats, compression, encryption, or storage mechanism transparent to the application.
Stars: ✭ 84 (-78.89%)
Mutual labels:  s3, stream
lead
Sink your streams.
Stars: ✭ 14 (-96.48%)
Mutual labels:  gulp, stream
Gulp Tutorial
Code examples for my Gulp.js tutorial series
Stars: ✭ 383 (-3.77%)
Mutual labels:  gulp
Node Elgato Stream Deck
A Node.js library for interfacing with the Elgato Stream Deck.
Stars: ✭ 359 (-9.8%)
Mutual labels:  stream
Commuter
🚎 Notebook sharing hub
Stars: ✭ 353 (-11.31%)
Mutual labels:  s3
Nodb
NoDB isn't a database.. but it sort of looks like one.
Stars: ✭ 353 (-11.31%)
Mutual labels:  s3
King Admin
king-admin是一个超酷的前后端分离的基础权限管理后台,前端:angularJs+bootstrap+gulp,后端:spring-boot+mybatis-plus(分java版和kotlin版)
Stars: ✭ 397 (-0.25%)
Mutual labels:  gulp
Movienight
Single instance video streaming server with integrated chat.
Stars: ✭ 387 (-2.76%)
Mutual labels:  stream
Helm S3
Helm plugin that allows to set up a chart repository in AWS S3.
Stars: ✭ 372 (-6.53%)
Mutual labels:  s3
Tbox
🎁 A glib-like multi-platform c library
Stars: ✭ 3,800 (+854.77%)
Mutual labels:  stream
Generator Webapp
A gulp.js generator for modern webapps
Stars: ✭ 3,784 (+850.75%)
Mutual labels:  gulp
Edgefs
EdgeFS - decentralized, scalable data fabric platform for Edge/IoT Computing and Kubernetes apps
Stars: ✭ 358 (-10.05%)
Mutual labels:  s3
Kafka Connect Ui
Web tool for Kafka Connect |
Stars: ✭ 388 (-2.51%)
Mutual labels:  s3
Onyx
An android library that uses technologies like artificial Intelligence, machine learning, and deep learning to make developers understand the content that they are displaying in their app.
Stars: ✭ 352 (-11.56%)
Mutual labels:  stream
Infinit
The Infinit policy-based software-defined storage platform.
Stars: ✭ 363 (-8.79%)
Mutual labels:  s3
Vinyl Ftp
Blazing fast vinyl adapter for FTP
Stars: ✭ 385 (-3.27%)
Mutual labels:  gulp
Clickhouse Backup
Tool for easy ClickHouse backup and restore with cloud storages support
Stars: ✭ 359 (-9.8%)
Mutual labels:  s3
News.laravel China.org
Source Code of the https://news.laravel-china.org/ website, build on top of Laravel 5.1. Laravel 资讯网站源代码,使用 Laravel 5.1 构建
Stars: ✭ 363 (-8.79%)
Mutual labels:  gulp

gulp-awspublish

NPM version Build Status Dependency Status Install size

awspublish plugin for gulp

Usage

First, install gulp-awspublish as a development dependency:

npm install --save-dev gulp-awspublish

Then, add it to your gulpfile.js:

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
// ...
  • Note: If you follow the aws-sdk suggestions for providing your credentials you don't need to pass them in to create the publisher.

  • Note: In order for publish to work on S3, your policy has to allow the following S3 actions:

{
  "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 permissions

By default, the plugin works only when public access to the bucket is not blocked:

  • Block all public access: Off
    • Block public access to buckets and objects granted through new access control lists (ACLs): Off
    • Block public access to buckets and objects granted through any access control lists (ACLs): Off
    • Block public access to buckets and objects granted through new public bucket policies: Off
    • Block public and cross-account access to buckets and objects through any public bucket policies: Off

When dealing with a private bucket, make sure to pass the option { noAcl: true } or a value for the x-amz-acl header:

publisher.publish({}, { noAcl: true });
publisher.publish({ "x-amz-acl": "something" });

Testing

  1. Create an S3 bucket which will be used for the tests. Optionally create an IAM user for running the tests.
  2. Set the buckets Permission, so it can be edited by the IAM user who will run the tests.
  3. Add an aws-credentials.json file to the project directory with the name of your testing buckets and the credentials of the user who will run the tests.
  4. Run npm test
{
  "params": {
    "Bucket": "<test-bucket-name>"
  },
  "credentials": {
    "accessKeyId": "<your-access-key-id>",
    "secretAccessKey": "<your-secret-access-key>",
    "signatureVersion": "v3"
  }
}

API

awspublish.gzip(options)

create a through stream, that gzip file and add Content-Encoding header.

  • Note: Node version 0.12.x or later is required in order to use awspublish.gzip. If you need an older node engine to work with gzipping, you can use v2.0.2.

Available options:

  • ext: file extension to add to gzipped file (eg: { ext: '.gz' })
  • smaller: gzip files only when result is smaller
  • Any options that can be passed to zlib.gzip

awspublish.create(AWSConfig, cacheOptions)

Create a Publisher. The AWSConfig object is used to create an aws-sdk S3 client. At a minimum you must pass a Bucket key, to define the site bucket. You can find all available options in the AWS SDK documentation.

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 timeout

The AWS client has a default timeout which may be too low when pushing large files (> 50mb). To adjust timeout, add httpOptions: { timeout: 300000 } to the AWSConfig object.

Credentials

By 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: "..."
  },
  accessKeyId: "akid",
  secretAccessKey: "secret"
});

Using a profile by name from ~/.aws/credentials:

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: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN, AWS_PROFILE. You can also define a [default] profile in ~/.aws/credentials which the SDK will use transparently without needing to set anything.

Publisher.publish([headers], [options])

Create a through stream, that push files to s3.

  • header: hash of headers to add or override to existing s3 headers.
  • options: optional additional publishing options
    • force: bypass cache / skip
    • noAcl: do not set x-amz-acl by default
    • simulate: debugging option to simulate s3 upload
    • createOnly: skip file updates

Files that go through the stream receive extra properties:

  • s3.path: s3 path
  • s3.etag: file etag
  • s3.date: file last modified date
  • s3.state: publication state (create, update, delete, cache or skip)
  • s3.headers: s3 headers for this file. Defaults headers are:
    • x-amz-acl: public-read
    • Content-Type
    • Content-Length

Note: publish will never delete files remotely. To clean up unused remote files use sync.

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 .awspublish-<bucket>. The cache file is flushed to disk every 10 files just to be safe.

Publisher.sync([prefix], [whitelistedFiles])

create a transform stream that delete old files from the bucket.

  • prefix: prefix to sync a specific directory
  • whitelistedFiles: array that can contain regular expressions or strings that match against filenames that should never be deleted 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());

warning sync will delete files in your bucket that are not in your local folder unless they're whitelisted.

// 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.client

The aws-sdk S3 client is exposed to let you do other s3 operations.

awspublish.reporter([options])

Create a reporter that logs s3.path and s3.state (delete, create, update, cache, skip).

Available options:

  • states: list of state to log (default to all)
// 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"]
    })
  );

Examples

Rename file & directory

You can use gulp-rename to rename your files on s3

// 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 parallel

You can use concurrent-transform to upload files in parallel to your amazon bucket

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 stream

You can use the merge-stream plugin to upload two streams in parallel, allowing sync to work with mixed file types

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());

Plugins

gulp-awspublish-router

A router for defining file-specific rules https://www.npmjs.org/package/gulp-awspublish-router

gulp-cloudfront-invalidate-aws-publish

Invalidate cloudfront cache based on output from awspublish https://www.npmjs.com/package/gulp-cloudfront-invalidate-aws-publish

License

MIT License

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].