Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Specify s3 Path #33

Closed
mrprkr opened this issue Jan 3, 2016 · 4 comments
Closed

Feature Request: Specify s3 Path #33

mrprkr opened this issue Jan 3, 2016 · 4 comments

Comments

@mrprkr
Copy link

mrprkr commented Jan 3, 2016

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
Copy link
Owner

clineamb commented Jan 4, 2016

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
Copy link
Author

mrprkr commented Jan 4, 2016

Great, thanks for this!

Sent from my iPhone

On 5 Jan 2016, at 8:37 AM, Caroline A. [email protected] 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
Copy link

vladaman commented Jan 6, 2019

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
Copy link

kgraf80 commented Mar 18, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants