S3 正在将带有空格和符号的 url 编码为未知格式

S3 is encoding urls with spaces and symbols to unkown format

我有一个带有静态网站托管的 S3 存储桶

S3正在编码路由

/AniketFuryRocks/What is Lorem Ipsum?

前往路线

/AniketFuryRocks/What+is+Lorem+Ipsum%3F

代替路线

/AniketFuryRocks/What%20is%20Lorem%20Ipsum?

此 Url 编码导致 404 错误。

我已经尝试在编码和解码的时尚中存储对象的路线。

使用 javascript encodeUri() 函数存储 S3 对象时。带有空格的路径有效,但带有 ? 等符号的路径无效。

我注意到浏览器要求 ? 而 s3 期望字符串末尾有 %3F

原来需要对URL

中的符号进行编码

我制作了 S3Encode,同样的 npm 存储库

这是一个可以完成这项工作的函数

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>