dquv73115
2018-03-27 22:37
浏览 278
已采纳

使用AWS开发工具包Go的完整URI从S3下载文件

The examples I've seen for downloading a file from S3 using the AWS SDK for Go are of the form:

downloader := s3manager.NewDownloader(session, /* other args */)

s3object := &s3.GetObjectInput{
    Bucket: aws.String(myBucket),
    Key: aws.String(myKey),
}

bytesDownloaded, err := downloader.Download(myFile, s3object)

That is, one uses the bucket and key to specify the file. But what if I already have the full URI of the file on S3? E.g.:

https://s3.us-west-2.amazonaws.com/myBucket/myKey

Is there a way using the SDK to specify the file to download using the URL directly?

No, the bucket is not public. In the SDK, I'm also setting the access and secret keys (elided from the example code).

Lastly, if it's simply not possible to do what I'm asking via the SDK, that's an acceptable (though not desired) answer.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • doushifen4060 2018-03-28 02:07
    已采纳

    There is no way to do what you want. The only ways to get a private object are:

    1. Use the bucket and key to download the file
    2. Generate a presigned URL an do an hhtp.Get on that URL (to generate the presigned URL you need the bucket and key)

    If you really want to use the URL I recommend you create a wrapper around s3.GetObjectInput that accepts the URL as an argument, parses it and then create the s3.GetObjectInput object.

    Also, I don't know if you could do this it depends on your use case, but if you are storing the URLs somewhere and that's why you want to use them, you can consider storing instead the presigned url with a long expiration time, and then when you want to fetch the object you can just use that URL.

    打赏 评论
  • dot_0620 2018-03-27 23:15

    Here you first need to enable public access on the object that you are trying to download from S3. You could achieve this by either of following approach.

    • Make object publicly readable. But remember, whoever got access on the resource url can download your object from S3.
    • Grant an anonymous access to your S3 bucket through bucket policies. Refer here to read about this approach with policy samples. Remember to verify what you expose when you go in this route.
    • Use a PreSignedUrl that permit to download the object in time-limited manner. This is what I would prefer from aforementioned approaches.

    Now last step would be to use go's http client to make the call to your pre-signed url and download the required object.

    打赏 评论
  • douqiao1413 2018-03-27 23:17

    You could parse the URL to get the bucket and key:

    package main
    
    import (
        "fmt"
        "log"
        "net/url"
        "strings"
    )
    
    func main() {
        u, err := url.Parse("https://s3.us-west-2.amazonaws.com/myBucket/myKey/morekey")
        if err != nil {
            log.Fatal(err)
        }
    
        path := strings.SplitN(u.Path, "/", 3)
        bucket := path[1]
        key := path[2]
        fmt.Println(bucket)
        fmt.Println(key)
    }
    

    Runnable: https://play.golang.org/p/G3Mxmm4f4qM

    打赏 评论
  • duanchao5258 2019-08-28 06:28

    Created new S3URLtoURI parser for this.

    func S3URLtoURI(s3Url string) (map[string]string, error) {
        m := make(map[string]string)
        u, err := url.Parse(s3Url)
        if err != nil {
            return m, err
        }
    
        if u.Scheme == "s3" {
            //s3: //bucket/key
            m["bucket"] = u.Host
            m["key"] = strings.TrimLeft(u.Path, "/")
        } else if u.Scheme == "https" {
            host := strings.SplitN(u.Host, ".", 2)
            if host[0] == "s3" {
                // No bucket name in the host;
                path := strings.SplitN(u.Path, "/", 3)
                m["bucket"] = path[1]
                m["key"] = path[2]
            } else { //bucket name in host
                m["bucket"] = host[0]
                m["key"] = strings.TrimLeft(u.Path, "/")
            }
    
        }
        return m, err
    }
    

    Runnable: https://play.golang.org/p/vkPJ7E4Fyno

    打赏 评论