Fine uploader add prefix to file name on s3

So when I upload the file to s3, I want to keep the original file name, which works if I use

objectProperties: {key: "filename"},

I would also like to add a prefix to the object in S3. Therefore, if I load foo.jpg, I want the key of the S3 object to be "12345 / foo.jpg". I look through the documentation, and I do not proceed with a clear guide.

Thanks J

+6
source share
3 answers

To add a prefix to the key name of an object, you can use the objectProperties.key parameter, for example,

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); } } }); 
+10
source

To add a β€œMark” answer, if you like that Fineuploader generated a UUID, but you still want the file to be uploaded to a specific folder in the bucket, you can simply use the same approach but specify the getUuid method. Just make sure that you first extract the file extension from the original file name as follows:

Non loadable jquery loader

 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; } } }); 
+8
source

I would use

 return prefix + '/' + $('#' + tagid).fineUploader("getName", fileId).replace(/[^\w\s.-]/gi, ''); 

to remove non-alphanumeric values.

0
source

Source: https://habr.com/ru/post/955615/


All Articles