I'm trying to figure this out since hours but I simply can't get my head around what is wrong here.
I have a file named test_!§$%&_ÄäÜüÖö.mp4 which I want to upload to an AWS S3 bucket. The upload works fine if there are no special characters (yes I've tried the official answer from the AWS docs by using decodeURIComponnet(filename), but this did not work). I'm not so fuzzed about the special characters !§$%& but I do want the German Umlaute to be converted. Thus I'm doing three things:
// remove all special chars (key comes from the file to upload)
let cleanKey = key.replace(/[°^!"§$%&\\()[\]{}=?*+#<>]/g, "");
// convert spaces to `_`
cleanKey = cleanKey.replace(" ", "_");
// convert German characters
const characters = [...cleanKey].map(char => {
switch (encodeURI(char)) {
case encodeURI("Ä"):
return "Ae";
case encodeURI("ä"):
return "ae";
case encodeURI("Ö"):
return "Oe";
case encodeURI("ö"):
return "oe";
case encodeURI("Ü"):
return "Ue";
case encodeURI("ü"):
return "ue";
default:
return char;
}
});
// return converted key
return characters.join("");
The problem is that the characters from my file system (MacOS 12.0.1) do not match in the switch statement. I have read a couple of discussions (e.g. here and here) and it seems that character decoding works different on each operating system. Unfortunately I could not figure out how I would convert my filename into a regular UTF-8 string (if this even exists) to parse out the non valid characters?
I know that my approach is probably not really production ready (eg if someone wants to upload a file written in Hebrew letters), so if someone has a better solution for not getting the
The specified key does not exist
error, please let me know.