Gulp Upload file to S3

For uploading file on S3 we use gulp-s3 Module. To use this module we must have gulp and gulp-cli already installed.
First we need to create a aws.json file for credentials.
The format for file is like

{
  "key": "AKIAI3Z7CUAFHG53DMJA",
  "secret": "acYxWRu5RRa6CwzQuhdXEfTpbQA+1XQJ7Z1bGTCx",
  "bucket": "dev.example.com",
  "region": "eu-west-1"
}

Now we first need to install gulp-s3 using gulp i –save gulp-s3 Command.
Once module is installed we will create a task for it.

var gulp = require('gulp'),
    s3 = require("gulp-s3"),
    inject = require('gulp-inject'),
    fs = require('fs'),
    s3Url = "https://s3.amazonaws.com/eventuositydev/";

const DIR = 'build/';

var transformUploadToS3 = function(filepath){
		filepath = filepath.replace(DIR,s3Url);
		arguments[0] = filepath + '?v=' + time;
	return inject.transform.apply(inject.transform, arguments);
}

gulp.task("moveToS3",function(){
	var aws = JSON.parse(fs.readFileSync('./aws.json'));
	gulp.src(DIR+'static.min.js')
		.pipe(s3(aws));
});

gulp.task("create-s3-index", function(){
	var	target = gulp.src('./src/index.html'),
		filesources = [DIR+"static.min.js"];

	return 	target.pipe(inject(gulp.src(filesources,{read: false}),
			{
  				addRootSlash:false,
				transform: transformUploadToS3
			}))
    	.pipe(gulp.dest('./'))
    	.pipe(debug());
});

In the above code after creating the minified files (js/css) we are uploading the files to s3 using moveToS3 command and after that we are injecting the s3 file path url to index.html file create-s3-index command.

3 thoughts on “Gulp Upload file to S3

  • September 28, 2017 at 11:42 am
    Permalink

    My family members every time say that I am killing my time here at web, except I know I am getting familiarity every day by reading thes good articles or reviews.

    Reply
  • September 28, 2017 at 7:43 pm
    Permalink

    I got this site from my pal who informed me about this website and at the moment this time I am visiting this web page and reading very informative articles or reviews at this place.

    Reply
  • October 4, 2017 at 11:01 am
    Permalink

    It’s great that you are getting thoughts from this post as well as from our argument made at this time.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *