Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

signURL 不能处理object key中包含/路径分隔符 #82

Closed
jiyeyuran opened this issue Dec 28, 2017 · 6 comments
Closed

signURL 不能处理object key中包含/路径分隔符 #82

jiyeyuran opened this issue Dec 28, 2017 · 6 comments

Comments

@jiyeyuran
Copy link

如果object key中包含路径分隔符/,signURL会将/转换为%252F,导致返回的url无法使用。

@cssivision
Copy link

you should use url.QueryUnescape method

@jiyeyuran
Copy link
Author

感觉没必要在SignURL里面对key做url.QueryEscape吧,返回以后还是要url.QueryUnescape

@hangzws hangzws closed this as completed Jan 10, 2019
@koolay
Copy link

koolay commented Jun 12, 2019

可否给个例子? 这是常见问题

@koolay
Copy link

koolay commented Jun 12, 2019

      	ur, err := url.Parse(rawURL)
	if err != nil {
		return err
	}
	epath, err := url.PathUnescape(ur.Path)
	if err != nil {
		return err
	}

	return bucket.SignURL(strings.TrimLeft(epath, "/"), oss.HTTPGet, 100)

@calfzhou
Copy link

这个问题是不考虑修复吗?目前还是会这样,SignUrl 得到的结果,把文件路径中的 / 都变成 %2F 了。用 url.PathUnescape 并不合适,它会把 url 最后的 Signature 末尾的 %3D 转换成 =

@mtiller
Copy link

mtiller commented Dec 8, 2021

I agree with the above comment (at least what Google translated it to). Why is url.QueryEscape being called in buildURL at all? This isnt' a query string, this is a path component. This is just wrong. You might want to do Go's equivalent of Javascripts encodeURI (because that explicitly leaves / alone, as it should).

My fix for this is to first Sign the URL and then run url.PathUnescape on the URL, e.g.,

	// Ask Aliyun library to sign the URL
	aliurl, err := bucket.SignURL(file, "GET", 60*60)
	if err != nil {
		// notest
		return "", err
	}
        // Parse the resulting URL
	ur, err := url.Parse(aliurl)
	if err != nil {
		return "", err
	}
	// This bit fixes an unnecessary (and incorrect, IMHO) encoding performed by the
	// Aliyun Go SDK.
	epath, err := url.PathUnescape(ur.Path)
	if err != nil {
		return "", err
	}
        // Plug the correct path back into the URL
	ur.Path = epath
	ur.RawPath = epath
	return ur.String(), nil

But this shouldn't be necessary and it probably doesn't work for some problematic URLs (which I fortunately don't seem to have).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants