S3 将带有空格和符号的 url 编码为未知格式 [英] S3 is encoding urls with spaces and symbols to unkown format
问题描述
我有一个带有静态网站托管的 S3 存储桶
I have a S3 bucket with static website hosting
S3 是编码路由
/AniketFuryRocks/What is Lorem Ipsum?
路由
/AniketFuryRocks/What+is+Lorem+Ipsum%3F
代替路由
/AniketFuryRocks/What%20is%20Lorem%20Ipsum?
此 Url 编码导致 404 错误.
This Url encoding is resulting in a 404 error.
我曾尝试以编码和解码的方式存储对象的路径.
I have tried storing the object's route in both encoded and decoded fashing.
使用 javascript encodeUri()
函数存储 S3 对象时.带有空格的路径有效,但带有 ?
等符号的路径无效.
When storing the S3 Object using javascript encodeUri()
function. Paths with spaces are working but paths with symbols like ?
are not working.
我注意到浏览器要求一个 ?
而 s3 需要一个 %3F
在字符串的末尾
I am noticing that the browser asks for a ?
while s3 expects a %3F
at the end of the string
推荐答案
原来需要对 URL 中的符号进行编码
It turns out that you need to encode symbols in the URL
我制作了 S3Encode,相同的 npm 存储库
I made S3Encode, npm repository for the same
这是一个完成工作的函数
Here is a function which does the job
function encode(filename) {
const encodings = {
'\+': "%2B",
'\!': "%21",
'\"': "%22",
'\#': "%23",
'\$': "%24",
'\&': "%26",
'\'': "%27",
'\(': "%28",
'\)': "%29",
'\*': "%2A",
'\,': "%2C",
'\:': "%3A",
'\;': "%3B",
'\=': "%3D",
'\?': "%3F",
'\@': "%40",
};
return filename.replace(
/([+!"#$&'()*+,:;=?@])/img,
match => encodings[match]
);
}
在anchor
标签及相关
例如
<a href=encode("/AniketFuryRocks/What is Lorem Ipsum?")></a>
这篇关于S3 将带有空格和符号的 url 编码为未知格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!