Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

gulp-s3-upload

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gulp-s3-upload - npm Package Compare versions

Comparing version 0.8.7 to 1.0.0

docs/changelog.md

155

index.js

@@ -5,4 +5,5 @@ var through = require('through2')

, mime = require('mime')
, helper = require('./src/methods.js')
, helper = require('./src/helper.js')
, PluginError = gutil.PluginError
, gulpPrefixer
;

@@ -12,15 +13,16 @@

function gulpPrefixer(AWS) {
/*
options = {
bucket: "bucket-name", // required
acl: "" // optional, defaults to 'public-read'
}
gulpPrefixer = function(AWS) {
var file_count = 0;
/*
`putObjectParams` now takes in the S3 putObject parameters:
http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property
- will have a catch for `bucket` vs `Bucket`
- Will filter out `Body` and `Key` because that is handled by the script and keyTransform
*/
return function(options, s3_params) {
return function(options) {
var stream, keyname_transform;
var _s3 = new AWS.S3();
var stream, _s3 = new AWS.S3(),
the_bucket = options.Bucket || options.bucket;
if(!options.bucket) {
if(!the_bucket) {
throw new PluginError(PLUGIN_NAME, "Missing S3 bucket name!");

@@ -30,23 +32,34 @@ }

stream = through.obj(function(file, enc, callback) {
var keyname, keyparts, filename, mimetype, mimeLookupName;
var _stream = this,
keyTransform, keyname, keyparts, filename,
mimetype, mime_lookup_name;
if(file.isNull()) {
// Do nothing if no contents
return callback(null, file);
// callback(null, file);
return callback(null);
}
if (file.isStream()) {
return callback(new gutil.PluginError(PLUGIN_NAME, 'No stream support'));
return callback(new gutil.PluginError(PLUGIN_NAME, 'No stream support.'));
}
// Plugin Transforms & Look-ups
// File Name transform
if(options.name_transform) {
// ===== Method Transforms & Look-ups
// === Key transform
// Allow for either keyTransform or nameTransform.
// We're using Key to be consistent with AWS-S3.
keyTransform = options.keyTransform || options.nameTransform;
if(keyTransform) {
// allow the transform function to take the complete path
// in case the user wants to change the path of the file, too.
keyname = options.name_transform(file.relative);
keyname = keyTransform(file.relative);
} else {
// otherwise keep it exactly parallel
keyparts = helper.parsePath(file.relative);
keyname = helper.buildName(keyparts.dirname, keyparts.basename + keyparts.extname);
keyname = helper.buildName(keyparts.dirname, keyparts.basename + keyparts.extname);
}

@@ -56,58 +69,66 @@

// Mime Lookup
mimeLookupName = options.mime_type_lookup ? options.mime_type_lookup(keyname) : keyname;
mimetype = mime.lookup(mimeLookupName);
// === Mime Lookup/Transform
_s3.getObject({
Bucket: options.bucket
, Key: keyname
}, function(getErr, getData) {
if (getErr && getErr.statusCode !== 404) {
return callback(new gutil.PluginError(PLUGIN_NAME, "S3 Error: " + getErr.message));
}
mime_lookup_name = keyname;
var objectOptions = {
Bucket: options.bucket,
ACL: options.acl || 'public-read',
Key: keyname,
Body: file.contents,
ContentType: mimetype
};
if(options.mimeTypeLookup) {
mime_lookup_name = options.mimeTypeLookup(keyname);
}
mimetype = mime.lookup(mime_lookup_name);
if(options.gzip) {
objectOptions.ContentEncoding = 'gzip';
}
if(options.cache) {
objectOptions.CacheControl = 'max-age=' + options.cache;
// === metadataMap
// New in V1: Map your files (using the keyname) to a metadata object.
// ONLY if options.Metadata is undefined.
if(!options.Metadata && options.metadataMap) {
if(helper.isMetadataMapFn(options.metadataMap)) {
options.Metadata = options.metadataMap(keyname);
} else {
options.Metadata = options.metadataMap;
}
}
// options.Metdata is not filtered out later.
if(options.meta) {
objectOptions.Metadata = options.meta;
_s3.getObject({
Bucket: the_bucket
, Key: keyname
}, function(getErr, getData) {
var objOpts;
if (getErr && getErr.statusCode !== 404) {
return callback(new gutil.PluginError(PLUGIN_NAME, "S3 Error: " + getErr.message));
}
if(s3_params) {
objectOptions = helper.mergeOptions(objectOptions, s3_params);
}
objOpts = helper.filterOptions(options);
_s3.putObject(objectOptions, function(err, data) {
if(err) {
return callback(new gutil.PluginError(PLUGIN_NAME, "S3 Error: " + err.message));
}
objOpts.Bucket = the_bucket;
objOpts.Key = keyname;
objOpts.Body = file.contents;
objOpts.ContentType = mimetype;
if(options.uploadNewFilesOnly && !getData || !options.uploadNewFilesOnly) {
_s3.putObject(objOpts, function(err, data) {
if(err) {
return callback(new gutil.PluginError(PLUGIN_NAME, "S3 Error: " + err.message));
}
if(getData) {
file_count++;
if(getData.ETag !== data.ETag) {
gutil.log(gutil.colors.cyan("Updated..."), keyname);
} else {
gutil.log(gutil.colors.gray("No Change..."), keyname);
if(getData) {
if(getData.ETag !== data.ETag) {
gutil.log(gutil.colors.cyan("Updated....."), keyname);
} else {
gutil.log(gutil.colors.gray("No Change..."), keyname);
}
} else { // doesn't exist in bucket, it's new
gutil.log(file_count, gutil.colors.cyan ("Uploaded...."), keyname);
}
} else { // doesn't exist in bucket, it's new
gutil.log(gutil.colors.cyan("Uploaded..."), keyname);
}
callback(null, file);
});
// callback(null, file);
callback(null);
});
}
});

@@ -120,12 +141,14 @@ });

// Exporting the plugin main function
// ===== Exporting the plugin main function
// `config` now takes the paramters from the AWS-SDK constructor:
// http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html#constructor-property
module.exports = function(config) {
var aws_config = config || {};
aws_config.accessKeyId = config.key;
aws_config.secretAccessKey = config.secret;
aws_config.accessKeyId = config.accessKeyId || config.key;
aws_config.secretAccessKey = config.secretAccessKey || config.secret;
if(!aws_config.accessKeyId || !aws_config.secretAccessKey) {
throw new PluginError(PLUGIN_NAME, "Missing AWS Key & secret.");
throw new PluginError(PLUGIN_NAME, "Missing AWS Key & Secret.");
}

@@ -132,0 +155,0 @@

{
"name": "gulp-s3-upload",
"version": "0.8.7",
"version": "1.0.0",
"description": "A gulp task to upload/update assets to an S3 account.",

@@ -19,3 +19,3 @@ "main": "index.js",

],
"author": "Caroline Amaba <cline.amaba@gmail.com>",
"author": "Caroline Amaba <her@carolineamaba.com>",
"license": "ISC",

@@ -27,4 +27,4 @@ "bugs": {

"dependencies": {
"aws-sdk": "2.1.4",
"gulp-util": "3.0.1",
"aws-sdk": "^2.1.4",
"gulp-util": "^3.0.1",
"mime": "1.2.11",

@@ -31,0 +31,0 @@ "through2": "1.1.1",

@@ -8,5 +8,6 @@ # gulp-s3-upload

[NPM](https://www.npmjs.com/package/gulp-s3-upload) / [Changelog](changelog.md)
[NPM](https://www.npmjs.com/package/gulp-s3-upload) / [Changelog](docs/changelog.md)
## Install
npm install gulp-s3-upload

@@ -16,14 +17,13 @@

Put in your AWS Developer key/secret. Region is optional.
Put in your AWS Developer key/secret. These are required, or else we don't have access to the bucket you want to upload to.
var gulp = require('gulp');
var s3 = require('gulp-s3-upload')({
key: "YOUR DEV ID",
secret: "YOUR SECRET",
region: "us-west-2" // optional
accessKeyId: "YOUR DEV ID",
secretAccessKey: "YOUR SECRET"
});
The other options not mentioned above available in the [AWS Config constructor](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html#constructor-property) are also available, though by default are undefined.
The other options not mentioned above (like `region`) available in the [AWS Config Constructor](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html#constructor-property) are also available, though by default are `undefined`.
Signature to pass through pipe: `s3(plugin_options [, putObjectParams])`;
`key` and `secret` are also alternative option names, though we encourage the use of `accessKeyId` and `secretAccessKey` to match the AWS Config Constructor.

@@ -35,4 +35,4 @@ Create a task.

.pipe(s3({
bucket: 'your-bucket-name', // Required
acl: 'public-read' // Optional ACL permissions, defaults to public-read.
Bucket: 'your-bucket-name', // Required
ACL: 'public-read' // Needs to be user-defined
}))

@@ -42,88 +42,140 @@ ;

**As of Version 0.8.5**, the s3 task can now take a second parameter that has all the options available in the [S3 putObject documentation](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property).
## Options
Any settings that are overlapped between the required paramter in this new optional parameter will be overwritten if defined in the second optional parameter, with `Body` being the exception, since that is purely handled by the gulp stream, and `Bucket` since it is required in the first set of required options.
**Bucket (bucket)** *(required)*
The way this is handled in the future will be changed for version 1.0, condensing everything into one `options` parameter. See the [Feature Roadmap](roadmap.md) for details.
> Type: `string`
### Options
> The bucket that the files will be uploaded to.
**bucket** *(required)*
Other available options are the same as the one found in the AWS-SDK docs for S3. See below for a list of availble AWS-SDK resources that this plugin constantly references.
Type: `string`
**NOTE:** `Key`, `Body`, and `ContentType` are the only options availble in `putObject` that do NOT need to be defined because the gulp will handle these for you. If you define these accidentally, we will filter them out.
The bucket that the files will be uploaded to.
### gulp-s3-plugin options
**acl**
#### keyTransform (nameTransform)
Type: `string`
> Type: `function`
See [Access Control List (ACL) Overview](http://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html)
for more information. Defaults to 'public-read'.
> Use this to transform your file names before they're uploaded to your S3 bucket.
> (Previously known as `name_transform`).
gulp.task("upload_transform", function() {
gulp.src("./dir/to/upload/**")
.pipe(s3({
Bucket: 'example-bucket',
ACL: 'public-read',
keyTransform: function(relative_filename) {
var new_name = changeFileName(relative_filename);
// or do whatever you want
return new_name;
}
}))
;
});
**gzip**
Type: `boolean`
#### metadataMap
Set the `Content-Encoding` meta to `gzip` so a gzipped version of the file can be uploaded to S3.
Type: `object` or `function`
If you have constant metadata you want to attach to each object,
just define the object, and it will be included to each object.
If you wish to change it per object, you can pass a function through
to modify the metadata based on the (transformed) keyname.
**cache**
Example (passing an `object`):
Type: `number`
gulp.task("upload", function() {
gulp.src("./dir/to/upload/**")
.pipe(s3({
Bucket: 'example-bucket',
ACL: 'public-read',
metadataMap: {
"uploadedVia": "gulp-s3-upload",
"exampleFlag": "Asset Flag"
}
}));
});
Set the `Cache-Control` meta to `max-age={cache}` for the object, so browsers won't fetch the file on every page request.
Passing the `s3.putObject` param option `Metadata` is effectively the same thing
as passing an `object` to `metadataMap`. `Metadata` is defined and `metadataMap` is not
it will use the object passed to `Metadata` as metadata for all the files that
will be uploaded. If both `Metadata` and `metadataMap` are defined, `Metadata` will take
precedence and be added to each file being uploaded.
Example (passing a `function`):
// ... setup gulp-s3-upload ...
var path = require('path');
var metadata_collection = {
"file1.txt": {
"uploadedVia": "gulp-s3-upload",
"example": "Example Data"
},
"file2.html": {
"uploadedVia": "gulp-s3-upload"
}
};
**meta**
gulp.task("uploadWithMeta", function() {
gulp.src("./upload/**")
.pipe(s3({
Bucket: 'example-bucket',
ACL: 'public-read',
metadataMap: function(keyname) {
path.basename(keyname); // just get the filename
return metadata_collection[keyname]; // return an object
}
}));
});
Type: `object`
When passing a function, it's important to note that the file
will already be transformed either by the `keyTransform` you defined
or by the default function which creates a keyname relative to
your S3 bucket, e.g. you can get "example.txt" or "docs/example.txt"
depending on how it was structured locally (hence why in the example,
the `path` module is used to just get the filename).
Set metadata values for the object. If you use `{myKey: 'Some value'}` the uploaded object will have the meta property *myKey* with the value *Some value*.
**Note:** You should be responsible for handling mismatching/non-matching keynames
to the metadata you're mapping.
#### mimeTypeLookup
**name_transform**
Type: `function`
Use this to transform your file names before they're uploaded to your S3 bucket.
Use this to transform what the key that is used to match the MIME type when uploading to S3.
Example:
gulp.task("upload_transform", function() {
gulp.task("upload", function() {
gulp.src("./dir/to/upload/**")
.pipe(s3({
bucket: 'example-bucket',
name_transform: function(relative_filename) {
var new_name = change_file_name(relative_filename);
return new_name;
}
}))
;
.pipe(s3({
Bucket: 'example-bucket',
ACL: 'public-read',
mimeTypelookup: function(original_keyname) {
return original_keyname.replace('.gz', ''); // ignore gzip extension
},
}));
});
**mime_type_lookup**
#### uploadNewFilesOnly
Type: `function`
Type: `boolean`
Use this to transform what the key that is used to match the MIME type when uploading to s3.
Set `uploadNewFilesOnly: true` if you only want to upload new files and not
overwrite existing ones.
Example:
gulp.task("upload_transform", function() {
gulp.src("./dir/to/upload/**")
.pipe(s3({
bucket: 'example-bucket',
mime_type_lookup: function(originalFilepath) {
return originalFilepath.replace('.gz', ''); //ignore gzip extension
},
}));
});
## AWS-SDK References
* [AWS Config Constructor](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html#constructor-property)
* [S3 putObject](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property)
* [Access Control List (ACL) Overview](http://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html)
----------------------------------------------------
### License
## License

@@ -130,0 +182,0 @@ Copyright (c) 2014, [Caroline Amaba](mailto:caroline.amaba@gmail.com)

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc