Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fine uploader add prefix to file name on s3

So when I upload a file to s3, I want to retain the original file name, which appears to work if I use

objectProperties: { key: "filename" },

I would also like to add a prefix to the object in S3. So if I upload foo.jpg, I want the S3 object key to be '12345/foo.jpg'. I'm going through the documentation and I'm not coming up with a clear direction to go in.

Thanks, j

like image 495
Josh Avatar asked Oct 09 '13 19:10

Josh


2 Answers

To add a prefix to your object's keyname you can use the objectProperties.key option like so,

non-jQuery Uploader

var uploader;

uploader = new qq.s3.FineUploader({
    // ....
    objectProperties: {
        key: function (id) {
            return '12345/' + uploader.getName(id);
        }
    }
});

jQuery Uploader

$("#fineuploader-s3").fineUploaderS3({
    // ....
    objectProperties: {
        key: function (fileId) { 
           return '12345/' + $("#fineuploader-s3").fineUploader("getName",fileId); }
    }
});
like image 107
Mark Feltner Avatar answered Dec 10 '22 05:12

Mark Feltner


To add to Mark's answer if you like that Fineuploader generated the UUID but you still wanted the file to be uploaded to a certain folder within the bucket you can just use the same approach but specify the method getUuid. Just make sure you extract the file extension from the original file name first like so:

Non-jquery uploader

uploader = new qq.s3.FineUploader({
    // ....
    objectProperties: {
        key: function (fileId) {

            var filename = uploader.getName(fileId);
            var uuid = uploader.getUuid(fileId);
            var ext = filename.substr(filename.lastIndexOf('.') + 1);

            return  'folder/within/bucket/' + uuid + '.' + ext;

        }
    }
});

jQuery Uploader

$("#fineuploader-s3").fineUploaderS3({
    // ....
    objectProperties: {
        key: function (fileId) {

            var filename = $('#fineuploader-s3').fineUploader('getName', fileId);
            var uuid = $('#fineuploader-s3').fineUploader('getUuid', fileId);
            var ext = filename.substr(filename.lastIndexOf('.') + 1); 

           return  'folder/within/bucket/' + uuid + '.' + ext;
        }
    }
});
like image 36
racl101 Avatar answered Dec 10 '22 06:12

racl101