【記一下】亞馬遜s3接口簽名算法中編碼uri代碼

com.amazonaws.services.s3.internal.S3Signer#sign(Request<?> request, AWSCredentials credentials)計算簽名方法中,先對資源URI進行編碼,調用了ServiceUtils.urlEncode(resourcePath)方法。方法以下:java

/**
 * URL encodes the specified string and returns it.  All keys specified by
 * users need to URL encoded.  The URL encoded key needs to be used in the
 * string to sign (canonical resource path).
 *
 * @param s
 *            The string to URL encode.
 *
 * @return The new, URL encoded, string.
 */
public static String urlEncode(String s) {
    if (s == null) return null;

    try {
        String encodedString = URLEncoder.encode(s, Constants.DEFAULT_ENCODING);
        // Web browsers do not always handle '+' characters well, use the
        // well-supported '%20' instead.
        encodedString =  encodedString.replaceAll("\\+", "%20");
        // Change all "%2F" back to "/", so that when users download a file in a virtual folder by the presigned URL,
        // the web browsers won't mess up the filename. (e.g. 'folder1_folder2_filename' instead of 'filename')
        encodedString = encodedString.replace("%2F", "/");
        encodedString = encodedString.replace("%7E", "~");
        return encodedString;
    } catch (UnsupportedEncodingException e) {
        throw new AmazonClientException("Unable to encode path: " + s, e);
    }
}

aws-java-sdk-1.4.7web

相關文章
相關標籤/搜索