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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测