mozillazg / go-cos

腾讯云对象存储服务 COS(Cloud Object Storage) Go SDK(XML API)
https://godoc.org/github.com/mozillazg/go-cos
MIT License
88 stars 26 forks source link

Is there any way to point the specific ip and port for client? #10

Closed toranger closed 5 years ago

mozillazg commented 5 years ago

@toranger Sorry, but I don't understand your problem, could you explain it to me?

P.S. If you mean how to hack the COS backend address which is resolved via dns, you can hack it via setup AuthorizationTransport.Transport with your version of http.RoundTripper(e.g. a modified copy of http.DefaultTransport) .

ref: https://golang.org/pkg/net/http/#RoundTripper

toranger commented 5 years ago

@toranger Sorry, but I don't understand your problem, could you explain it to me?

P.S. If you mean how to hack the COS backend address which is resolved via dns, you can hack it via setup AuthorizationTransport.Transport with your version of http.RoundTripper(e.g. a modified copy of http.DefaultTransport) .

ref: https://golang.org/pkg/net/http/#RoundTripper

To use the port and ip instead of using the bucket host

mozillazg commented 5 years ago

@toranger You can do it via setup AuthorizationTransport.Transport with your version of http.RoundTripper.

For example, one way is via change host of request url:

// demo.go
package main

import (
    "context"
    "net/http"

    "github.com/mozillazg/go-cos"
)

type DemoTransport struct {
    RemoteAddr string
    Transport  http.RoundTripper
}

func (t *DemoTransport) RoundTrip(req *http.Request) (*http.Response, error) {
    host := req.Host
    if host == "" {
        host = req.URL.Host
    }
    req.URL.Host = t.RemoteAddr
    req.Host = host

    resp, err := t.transport().RoundTrip(req)
    return resp, err
}

func (t *DemoTransport) transport() http.RoundTripper {
    if t.Transport != nil {
        return t.Transport
    }
    return http.DefaultTransport
}

func main() {
    b, _ := cos.NewBaseURL("http://test-1253846586.cos.ap-beijing-1.myqcloud.com")
    c := cos.NewClient(b, &http.Client{
        Transport: &cos.AuthorizationTransport{
            SecretID:  "test",
            SecretKey: "test",
            Transport: &DemoTransport{
                RemoteAddr: "127.0.0.1:8080",
            },
        },
    })

    c.Bucket.Get(context.Background(), nil)
}

Start a demo server on another window:

$ echo -e 'HTTP/1.1 200 OK\r\n' | nc -l 8080

Test:

$ go run demo.go

Local demo server will receive the request:

$ echo -e 'HTTP/1.1 200 OK\r\n' | nc -l 8080
GET / HTTP/1.1
Host: test-1253846586.cos.ap-beijing-1.myqcloud.com
User-Agent: go-cos/0.11.0
Authorization: q-sign-algorithm=sha1&q-ak=test&q-sign-time=1546436220;1546439820&q-key-time=1546436220;1546439820&q-header-list=&q-url-param-list=&q-signature=749f551057b64672c6123c80be6babc699bd8030
Content-Type: application/xml
Accept-Encoding: gzip

Another way is via hack dns resolver or connection creator:

mozillazg commented 5 years ago

Or just use <ip>:<port> instead of bucket host as host of bucket url ? go-cos not checking the format of bucket url in case of user may use it in private cloud.

b, _ := cos.NewBaseURL("http://127.0.0.1:8080")
c := cos.NewClient(b, &http.Client{
    Transport: &cos.AuthorizationTransport{
        SecretID:  "test",
        SecretKey: "test",
    },
})