Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Go言語でs3からファイルダウンロード+プロキシ使う版
package main
import (
"net/http"
"net/url"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)
func main() {
//===== create proxy client
proxyUrl := "http://hoge.proxy.net:8080"
httpclient := &http.Client{
Transport: &http.Transport{
Proxy: func(*http.Request) (*url.URL, error) {
return url.Parse(proxyUrl)
},
},
}
//===== create session on aws
crds := credentials.NewStaticCredentials("AWS_ACCESS_KEY_ID","AWS_SECRET_ACCESS_KEY","")
sess := session.Must(session.NewSession(&aws.Config{
Region: aws.String("ap-northeast-1"),
HTTPClient: httpclient,
Credentials: crds,
}))
//===== create output file
outputFile := "./hogefile.gz"
file, err := os.Create(outputFile)
if err != nil {
os.Exit(1)
}
defer file.Close()
//===== download from s3
downloader := s3manager.NewDownloader(sess)
_, err = downloader.Download(file, &s3.GetObjectInput{
Bucket: aws.String("bucket.hoge.fuga.com"),
Key: aws.String("hogelog.gz"),
})
if err != nil {
os.Remove(outputFile)
os.Exit(1)
}
}
// ref.
// https://gist.github.com/jakexks/2f876697dfca1fe15b92f7bb6032780d
// https://github.com/aws/aws-sdk-go/issues/1226
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.