I can’t seem to capture the filename in order to pass it as the key in my AWS S3 set up with Nodejs and Express.
Front end:
<script src="https://transloadit.edgly.net/releases/uppy/v1.6.0/uppy.min.js"></script>
<script>
const AwsS3 = Uppy.AwsS3
var uppy = Uppy.Core()
.use(Uppy.Dashboard, {
trigger: ".UppyModalOpenerBtn",
inline: false,
closeModalOnClickOutside: true,
target: '#drag-drop-area',
replaceTargetContent: true,
showProgressDetails: true,
proudlyDisplayPoweredByUppy: true,
note: "Images, Word, Excel, PDF, and similar files only, max 20 files of 30 MB each",
height: 470,
restrictions: {
maxFileSize: 31457280, //i.e. 30MB
maxNumberOfFiles: 20,
minNumberOfFiles: null,
allowedFileTypes: null
},
metaFields: [
{ id: 'name', name: 'Name', placeholder: 'You can rename the file here'},
{id: 'caption', name: "Caption", placeholder: "Briefly describe what the file contains"}
],
browserBackButtonClose: true
})
.use(AwsS3, {
getUploadParameters(file){
return fetch('/path/to/server/', {
method: 'post',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({
filename: file.name,
contentType: file.type
})
}).then((response) => {
return response.json();
}).then((data) => {
console.log('>>>', data);
return {
method: data.method,
url: data.url,
fields: data.fields,
headers: data.headers
};
});
},
})
uppy.on('complete', (result) => {
console.log('Upload complete! We’ve uploaded these files:', result.successful)//<-- returns correct name under 'name:'!
})
uppy.on('upload-success', (file, data) => {
console.log("file.meta['key'] is: ");
console.log(file.meta['key']); //<-- this returns 'undefined'
file.meta['key']
})
</script>
</div>
Server:
router.post("/uppytest", (req, res) => {
console.log(req.fileName); //<-- 'undefined'
console.log(req.body.filename); //<-- 'undefined'
console.log(req.query.filename); //<-- 'undefined'
console.log(req.body);//<-- '{ }'
const params = {
Bucket: myBucket,
Key: `${Date.now().toString()}-${req.body.filename}`, //<-- ends up in S3 bucket as '1573509650845-undefined'
ContentType: req.body.contentType,
};
s3.getSignedUrl('putObject', params, (err, url) => {
res.status(200).json({
method: 'put',
url,
fields: {},
});
});
});
I’m sure I’m making some obvious mistake, but I’ve been staring at this for hours and trying everything I can think of, and I just can’t seem to snag the original file name server side.
*Edit: If I edit the file name in the Dashboard it also has no effect.