douju9272 2017-10-04 22:16
浏览 739
已采纳

如何在Golang中从AWS S3获取资源URL

I need to get public permanent (not signed) URL of a resource using golang and official aws go sdk. In Java AWS S3 SDK there's a method called getResourceUrl() what's the equivalent in go?

  • 写回答

1条回答 默认 最新

  • douxianji6181 2017-10-04 22:40
    关注

    This is how you get presigned URLs using the go sdk:

    func GetFileLink(key string) (string, error) {
        svc := s3.New(some params)
    
        params := &s3.GetObjectInput{
            Bucket: aws.String(a bucket name),
            Key:    aws.String(key),
        }
    
        req, _ := svc.GetObjectRequest(params)
    
        url, err := req.Presign(15 * time.Minute) // Set link expiration time
        if err != nil {
            global.Log("[AWS GET LINK]:", params, err)
        }
    
        return url, err
    }
    

    If what you want is just the URL of a public access object you can build the URL yourself:

    https://<region>.amazonaws.com/<bucket-name>/<key>
    

    Where is something like us-east-2. So using go it will be something like:

    url := "https://%s.amazonaws.com/%s/%s"
    url = fmt.Sprintf(url, "us-east-2", "my-bucket-name", "some-file.txt")
    

    Here is a list of all the available regions for S3.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器