clineamb / gulp-s3-upload

A gulp task to upload/update assets to an S3 account.
62 stars 37 forks source link

Feature Request: Specify s3 Path #33

Closed mrprkr closed 8 years ago

mrprkr commented 8 years ago

Is is possible to add an option to specify the upload path? AWS API suggests prepending the file name with /folder/to/upload/to/. Can't figure out how to implement this here.

clineamb commented 8 years ago

Hello! Checkout the keyTransform function:

keyTransform (nameTransform)

Type: function

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() {
        return 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;
                }
            }))
        ;
    });

So, if you want to prepend the folder name...

    gulp.task("upload_transform", function() {
        return gulp.src("./dir/to/upload/**")
            .pipe(s3({
                Bucket: 'example-bucket',
                ACL: 'public-read',
                keyTransform: function(relative_filename) {
                    var new_name = "folder/to/upload/to/" + relative_filename;
                    // or you can use `path.join("folder", "to", "upload", relative_filename)`
                    return new_name;
                }
            }))
        ;
    });
mrprkr commented 8 years ago

Great, thanks for this!

Sent from my iPhone

On 5 Jan 2016, at 8:37 AM, Caroline A. notifications@github.com wrote:

Hello! Checkout the keyTransform function:

keyTransform (nameTransform)

Type: function

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

So, if you want to prepend the folder name...

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 = "folder/to/upload/to/" + relative_filename;
                // or you can use `path.join("folder", "to", "upload", relative_filename)`
                return new_name;
            }
        }))
    ;
});

— Reply to this email directly or view it on GitHub.

vladaman commented 5 years ago

This method is great but it does not allow me to specify prefix for example. Is there any way to keep the folder structure and prefix all uploads with a path?

 gulp.src(["index.html",'./assets/**'])
        .pipe(s3({
            Bucket: 'mybucket',
            ACL: 'public-read',
            keyTransform: function(relative_filename) {
                var new_name = "production/" + relative_filename; // causes issue, since all keys will be prefixed but original path is not used
                return new_name;
            }
        }, 
kgraf80 commented 4 years ago

This is my solution (workaround) in /node_modules/gulp-s3-upload/index.js change file.relative to: file.path

if(keyTransform) {

    //  Allow the transform function to take the
    //  complete path in case the user wants to change
    //  the path of the file, too.

    // >>>>>>> change here: file.relative to: file.path
    keyname = keyTransform(file.path);

} else {
    // ...Otherwise keep it exactly parallel.

    keyparts = helper.parsePath(file.relative);
    keyname  = helper.buildName(keyparts.dirname, keyparts.basename + keyparts.extname);
}

and after that:

const targetAssets = 'aws-assets/';

 gulp.src(["index.html",'./assets/**'])
        .pipe(s3({
            Bucket: 'mybucket',
            ACL: 'public-read',
            keyTransform: function(filePath) {
                let newFile = targetAssets + filePath.split("www/assets/")[1]
                return newFile;
            }
        },

for example: for filePath: /home/user/webserwer/www/assets/images/test/image.jpg file will be saved in: mybucket/aws-assets/images/test/image.jpg