Last active
February 20, 2018 05:56
-
-
Save suganoo/4b48dde2f7ee67f1a6deb9f78be5e749 to your computer and use it in GitHub Desktop.
Go言語でs3からファイルダウンロード+プロキシ使う版
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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