{"id":396,"date":"2017-08-23T15:00:00","date_gmt":"2017-08-23T06:00:00","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2017\/08\/23\/golang-s3-zip"},"modified":"2017-08-23T15:00:00","modified_gmt":"2017-08-23T06:00:00","slug":"golang-s3-zip","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2017\/08\/23\/golang-s3-zip\/","title":{"rendered":"Golang\u3067S3\u304b\u3089\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u305f\u30d5\u30a1\u30a4\u30eb\u3092zip\u306b\u3057\u3066\u30ec\u30b9\u30dd\u30f3\u30b9\u3059\u308b"},"content":{"rendered":"
\u3053\u3093\u306b\u3061\u306f\u3002
\nMMM\u30b5\u30fc\u30d0\u30b5\u30a4\u30c9\u30a8\u30f3\u30b8\u30cb\u30a2\u306e\u67f3\u6cbc\u3067\u3059\u3002\u304a\u4e16\u8a71\u306b\u306a\u3063\u3066\u304a\u308a\u307e\u3059\u3002
\n\u5317\u6d77\u9053\u306e\u590f\u306f\u305d\u308d\u305d\u308d\u7d42\u308f\u308a\u3067\u3001\u591c\u306f\u3060\u3044\u3076\u5bd2\u3044\u3067\u3059\u3002<\/p>\n
\u524d\u56de<\/a>\u306b\u5f15\u304d\u7d9a\u304dgolang\u306b\u3064\u3044\u3066\u66f8\u3044\u3066\u3044\u304d\u307e\u3059\u3002 \u6d41\u308c\u3068\u3057\u3066\u306f\u3001<\/p>\n \u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n \u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u306f\u3001AWS SDK\u3092\u4f7f\u3044\u307e\u3059\u3002 \u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u306e\u6d41\u308c\u3068\u3057\u3066\u306f\u3001<\/p>\n \u306b\u306a\u308a\u307e\u3059\u3002 err\u304cnil\u3067\u3042\u308c\u3070\u3001\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u306b\u306f\u6210\u529f\u3059\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n zip\u30d5\u30a1\u30a4\u30eb\u3092\u6271\u3046\u306b\u306f\u3001archive\/zip<\/a>\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n \u3053\u3093\u306a\u611f\u3058\u3067zipWriter\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002(http.ResponseWriter\u306f\u5404API\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u306b\u3088\u3063\u3066\u751f\u6210\u65b9\u6cd5\u304c\u7570\u306a\u308b\u306e\u3067\u3001\u74b0\u5883\u306b\u3042\u3063\u305f\u3084\u308a\u65b9\u3067\u4f5c\u6210\u3057\u3066\u304f\u3060\u3055\u3044\u3002) \u3082\u66f8\u3044\u3066\u304a\u3044\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n zip\u3092\u4f5c\u6210\u3059\u308b\u624b\u9806\u3068\u3057\u3066\u306f\u3001<\/p>\n \u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n \u3053\u3093\u306a\u611f\u3058\u3067\u3001zip\u3092\u30ec\u30b9\u30dd\u30f3\u30b9\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002 \u3068\u3057\u306a\u3044\u3068\u3001\u4e0d\u6b63\u306azip\u304c\u30ec\u30b9\u30dd\u30f3\u30b9\u3055\u308c\u3066\u3057\u307e\u3046\u306e\u3067\u3001\u6c17\u3092\u3064\u3051\u3066\u304f\u3060\u3055\u3044\u3002 \u5b9f\u969b\u306fS3\u304b\u3089\u8907\u6570\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3059\u308b\u7b87\u6240\u3082\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u3002 \u4eca\u56de\u306f\u3001\u8907\u6570\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u305f\u3044\u3051\u3069\u3001\u3082\u3057\u3072\u3068\u3064\u3067\u3082\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u306b\u30b3\u30b1\u305f\u3089\u30a8\u30e9\u30fc\u3068\u3057\u305f\u304b\u3063\u305f\u306e\u3067\u3001 \u5148\u306b\u30b3\u30fc\u30c9\u304b\u3089\u304a\u898b\u305b\u3057\u307e\u3059\u3002<\/p>\n goroutine\u3092\u4f7f\u3046\u3068\u3001\u91cd\u3044\u51e6\u7406\u304c\u7c21\u5358\u306b\u4e26\u5217\u3067\u66f8\u3051\u3066\u7d20\u6674\u3089\u3057\u3044\u3067\u3059\u306d\uff01
\nS3\u306e\u30d5\u30a1\u30a4\u30eb\u3092zip\u306b\u56fa\u3081\u3066\u3001\u305d\u308c\u3092API\u304b\u3089\u30ec\u30b9\u30dd\u30f3\u30b9\u3059\u308b\u3084\u308a\u65b9\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\nS3\u304b\u3089\u30ed\u30fc\u30ab\u30eb\u306b\u30d5\u30a1\u30a4\u30eb\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3059\u308b<\/h3>\n
\n
\ns3manager<\/a>\u3068\u3044\u3046\u4fbf\u5229\u30d1\u30c3\u30b1\u30fc\u30b8\u304c\u3042\u308b\u306e\u3067\u3001\u3053\u3061\u3089\u304b\u3089\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n\n
\n\u4ee5\u4e0b\u304c\u5b9f\u30b3\u30fc\u30c9\u3067\u3059\u3002<\/p>\n\/\/ s3manager\u4f5c\u6210\n\/\/ \u30ea\u30fc\u30b8\u30e7\u30f3\u306f\u74b0\u5883\u306b\u5408\u308f\u305b\u308b\nsess := (session.New(&aws.Config{Region: aws.String("ap-northeast-1")}))\ndownloader := s3manager.NewDownloader(sess)\n\n\/\/ \u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u9818\u57df\u3092\u4f5c\u6210\nos.MkdirAll("\/tmp\/download", 0755)\nf, _ := os.Create("\/tmp\/download\/sample.pdf")\ndefer f.Close()\n\n\/\/ download\u5b9f\u884c\n_, err = downloader.Download(f, &s3.GetObjectInput{\n Bucket: aws.String("bucket_name_sample"),\n Key: aws.String("sample\/sample.pdf"),\n})<\/code><\/pre>\n
\u30ed\u30fc\u30ab\u30eb\u306b\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u305f\u30d5\u30a1\u30a4\u30eb\u3092zip\u306b\u56fa\u3081\u308b<\/h3>\n
zip.NewWriter(w io.Writer)<\/code> \u95a2\u6570\u3067\u3001zipWriter\u3092\u4f5c\u6210\u3059\u308b\u306e\u3067\u3059\u304c\u3001
\nhttp.ResponseWriter\u3092\u5f15\u6570\u306b\u53d6\u308c\u308b\u306e\u3067\u3001<\/p>\n\/\/ w\u306fhttp.ResponseWriter\nzipWriter := zip.NewWriter(w)<\/code><\/pre>\n
\n\u307e\u305f\u3001\u3053\u306e\u6642\u3001zip\u3092\u30ec\u30b9\u30dd\u30f3\u30b9\u3059\u308b\u305f\u3081\u306b\u306f\u3001\u30ec\u30b9\u30dd\u30f3\u30b9\u30d8\u30c3\u30c0\u306e\u8a2d\u5b9a\u304c\u4e0d\u53ef\u6b20\u306a\u306e\u3067\u3001<\/p>\nw.Header().Set("Content-Type", "application\/zip")\nw.Header().Set("Content-Disposition", "attachment; filename=sample.zip")<\/code><\/pre>\n
\n
zipWriter := zip.NewWriter(w)\ndefer zipWriter.Close()\n\nfile, _ := os.Open("\/tmp\/download\/sample.pdf")\ndefer file.Close()\nwriter, _ := zipWriter.Create("download\/sample.pdf")\ndefer writer.Close()\nio.Copy(writer, file)<\/code><\/pre>\n
\n\u3053\u308c\u306f\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u306a\u306e\u3067\u7701\u3044\u3066\u307e\u3059\u304c\u3001\u5b9f\u969b\u306f\u30a8\u30e9\u30fc\u30cf\u30f3\u30c9\u30ea\u30f3\u30b0\u304c\u5fc5\u8981\u306b\u306a\u308a\u307e\u3059\u3002
\n\u6ce8\u610f\u70b9\u3068\u3057\u3066\u3001\u4f8b\u3048\u3070\u30a8\u30e9\u30fc\u6642\u306fzip\u3092\u8fd4\u3057\u305f\u304f\u306a\u3044\u5834\u5408\u3001<\/p>\nif err := nil {\n w.Header().Del("Content-Disposition")<\/code><\/pre>\n
\n(\u3053\u306e\u8fba\u306f\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306e\u4ed5\u69d8\u306b\u3082\u3088\u308a\u307e\u3059\u304c\u2026)<\/p>\n\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3092\u9ad8\u901f\u5316\u3057\u3066\u307f\u3088\u3046<\/h3>\n
\n\u305d\u306e\u6642\u3001\u30d5\u30a1\u30a4\u30eb\u30921\u3064\u305a\u3064\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u3044\u308b\u3068\u3001\u30d5\u30a1\u30a4\u30eb\u304c\u591a\u3044\u6642\u6642\u9593\u304c\u304b\u304b\u308a\u307e\u3059\u3002
\n\u3053\u3053\u306fgoroutine\u3092\u4f7f\u3063\u3066\u3001\u4e26\u5217\u5b9f\u884c\u3059\u308b\u3088\u3046\u306b\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n
\nsync.errgroup<\/a>\u3092\u4f7f\u3063\u3066\u307f\u307e\u3059\u3002<\/p>\nfunc main() error {\n fileNames := []string{"sample1.pdf","sample2.pdf","sample3.pdf"}\n eg := errgroup.Group{}\n for _, fileName := range fileNames {\n r := r\n eg.Go(func() error {\n err := download(fileName)\n return err\n })\n }\n\n if err := eg.Wait(); err != nil {\n panic(err)\n }\n \/\/ \u3053\u306e\u5f8c\u306b\u30ec\u30b9\u30dd\u30f3\u30b9\u51e6\u7406\u3092\u66f8\u304f\n}\n\nfunc download(fileName string) {\n \/\/ s3manager\u4f5c\u6210\n \/\/ \u30ea\u30fc\u30b8\u30e7\u30f3\u306f\u74b0\u5883\u306b\u5408\u308f\u305b\u308b\n sess := (session.New(&aws.Config{Region: aws.String("ap-northeast-1")}))\n downloader := s3manager.NewDownloader(sess)\n\n \/\/ \u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u9818\u57df\u3092\u4f5c\u6210\n os.MkdirAll("\/tmp\/download", 0755)\n f, _ := os.Create("\/tmp\/download\/" + fileName)\n defer f.Close()\n\n \/\/ download\u5b9f\u884c\n _, err = downloader.Download(f, &s3.GetObjectInput{\n Bucket: aws.String("bucket_name_sample"),\n Key: aws.String("sample\/" + fileName),\n })\n return err\n}<\/code><\/pre>\n
eg.Go()<\/code> \u306e\u4e2d\u306e\u30d5\u30a1\u30f3\u30af\u30b7\u30e7\u30f3\u306f\u3001errgroup\u304cgoroutine\u3092\u7acb\u3066\u3066\u4e26\u5217\u5b9f\u884c\u3057\u3066\u304f\u308c\u307e\u3059\u3002
\n\u305d\u306e\u5f8c\u3001 eg.Wait()<\/code> \u304c\u3001\u7acb\u3066\u305fgoroutine\u306e\u4e2d\u306e\u6700\u521d\u306e\u30a8\u30e9\u30fc\u3092\u8fd4\u3057\u3066\u304f\u308c\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n
\u307e\u3068\u3081<\/h3>\n
\n\u305c\u3072\u53c2\u8003\u306b\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n\u53c2\u8003<\/h3>\n