duanjia9577 2018-04-05 13:22
浏览 27
已采纳

转到S3文件的代理适用于localhost,但不适用于生产环境

I've got some GIFs on S3. They are all public. Here is a sample one

https://s3.amazonaws.com/spolytics.com/moves/2bfc1ab0-5bda-4121-a3ea-b34c4fc01260/27e7b9d9-ff09-475f-885a-6b097a466174.gif

I'd like to proxy them through my Go app so the user always sees my custom domain instead of the S3 domain.

https://spolytics.com/zemirco/matches/2bfc1ab0-5bda-4121-a3ea-b34c4fc01260/moves/27e7b9d9-ff09-475f-885a-6b097a466174.gif

The format is simply spolytics.com/{username}/matches/{uuid}/moves/{uuid}.gif. However, as you can see when you click on the second link, it does not work as expected. I get the error

<Error>
  <Code>NoSuchKey</Code>
  <Message>The specified key does not exist.</Message>
  <Key>
    spolytics.com/moves/2bfc1ab0-5bda-4121-a3ea-b34c4fc01260/27e7b9d9-ff09-475f-885a-6b097a466174.gif
  </Key>
  <RequestId>04A5AD2E108B3B78</RequestId>
  <HostId>oNgfLfa4vl3Lb/TWrqgFlGDIXRr2bL6bduhSLKKQF+5w5H82GpnXnXrW3oWY4tVKsnYmiUvx7fI=</HostId>
</Error>

It works when I run the app on my local machine under localhost. So the keys are definitely right.

Here is my Go proxy code

func(w http.ResponseWriter, r *http.Request) {
    uuid := "..."
    id := "..."
    rawURL := fmt.Sprintf("https://s3.amazonaws.com/spolytics.com/moves/%s/%s.gif", uuid, id)
    parsedURL, err := url.Parse(rawURL)
    if err != nil {
        ...
    }
    director := func(request *http.Request) {
        request.URL = parsedURL
    }
    proxy := &httputil.ReverseProxy{
        Director: director,
    }
    proxy.ServeHTTP(w, r)
}

Any ideas? Especially why it does not work in production?

  • 写回答

1条回答 默认 最新

  • duanguochong0397 2018-04-05 14:25
    关注

    I'm guessing it's because you are just passing through your incoming request, which should result into something like that:

    curl -H "Host: spolytics.com" https://s3.amazonaws.com/...gif 
    

    When your change the host header in your director function you should turn that into:

    curl -H "Host: s3.amazonaws.com" https://s3.amazonaws.com/...gif
    

    director := func(request *http.Request) {
        request.Host = "s3.amazonaws.com"
        request.URL = parsedURL
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择