pgherveou / gulp-awspublish

gulp plugin to publish files to amazon s3

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

awspublish.gzip sets s3.path making it impossible to change it later

Prinzhorn opened this issue · comments

Every plugin that tries to change the file path after awspublish.gzip has been run has no effect.

Example using gulp-rename:

//Correctly uploads "bundle.js" into the "s3-folder-prefix" folder inside the bucket
gulp.src('./dist/bundle.js')
    .pipe(rename(function(path) {
        path.dirname = 's3-folder-prefix';
    }))
    .pipe(awspublish.gzip())
    .pipe(publisher.publish(headers))
    .pipe(awspublish.reporter());
});

//Incorrectly uploads "bundle.js" into the bucket root
gulp.src('./dist/bundle.js')
    .pipe(awspublish.gzip())
    .pipe(rename(function(path) {
        path.dirname = 's3-folder-prefix';
    }))
    .pipe(publisher.publish(headers))
    .pipe(awspublish.reporter());
});

The workaround is to use awspublish.gzip immediately before publisher.publish.

Just got bitten by this pretty hard. +1 to fix please

Maybe we can add a note in the Readme?
Would you mind sending a PR, of if you have a better solution feel free to update the code!

I found this issue really helpful in my searches, so I wanted to update the temporary solution from above with revised code. The below example uses node's path.join to create a path for each file passed in from the source glob. This way, you can have a local sub-directory structure that remains intact as it is moved into a new directory on S3.

var path   = require('path');
var rename = require('gulp-rename');

var NEW_S3_DIRECTORY = 'new/s3/directory-example';

gulp.src('path/to/distribution/directory/**/*')
  .pipe(rename(function(filePath) {
    filePath.dirname = path.join(NEW_S3_DIRECTORY, filePath.dirname);
  }))
  .pipe(awspublish.gzip())
  .pipe(publisher.publish())
  .pipe(publisher.cache())
  .pipe(awspublish.reporter());

@psullivan6 Thanks! Just make sure you don't do .pipe(publisher.sync()) or it will delete everything else in your bucket

@emilymerwin @psullivan6 if you'd like, you should be able to use Publisher.sync's prefix option to only sync the specific created directory. From my experience, that leaves the rest of the bucket intact.

Hello Internet,

If you're here because you're using awspublishRouter, take a look at the following configuration, which works to upload to a particular dir called eve

gulp.task('publish', () => {
  const publisher = awspublish.create({
    region: 'us-east-1',
    params: {
      Bucket: 'my-bucket',
    },
  });
  return gulp.src('./build/unbundled/**/*')
    .pipe(awspublishRouter({
      cache: {cacheTime: 315360000, gzip: true},
      routes: {
        // Cache index and service worker for 5 minutes
        "((\bindex\.html\b)|(\bservice-worker\.js\b))": {cacheTime: 600, key: "eve/$&"},
        //pass everything else through to `eve`
        "^.+$": {key: "eve/$&"}
      }
    }))
    .pipe(publisher.publish())
    .pipe(publisher.cache())
    .pipe(awspublish.reporter());
});