Upgrade to Pro — share decks privately, control downloads, hide ads and more …

AWS SDK for GoのContextパターン / aws sdk for go context pattern

AWS SDK for GoのContextパターン / aws sdk for go context pattern

Shuichi Ohsawa

May 28, 2018
Tweet

More Decks by Shuichi Ohsawa

Other Decks in Programming

Transcript

  1. ࣗݾ঺հ • େᖒलҰ(Shuichi Ohsawa) • GitHub, Twitter: @ohsawa0515 • Blog:

    http://blog.jicoman.info/ • Sansanגࣜձࣾ - ໊ࢗ؅ཧΫϥ΢υαʔϏε • ໊ࢗσʔλԽγεςϜͷΠϯϑϥߏஙɾӡ༻ɾվળ 2/13
  2. S3΁ΞοϓϩʔυʹλΠϜΞ΢τΛઃ ͚Δ ctx, cancel := context.WithTimeout(context.Background(), time.Duration(30)*time.Second) defer cancel() svc

    := s3.New(sess) if _, err := svc.PutObjectWithContext(ctx, &s3.PutObjectInput{ Body: f, Bucket: aws.String(s3Bucket), Key: aws.String(file), }); err != nil { log.Fatal(err) } 8/13
  3. PutObjectWithContext() PutObject() ʹ ContextΛηοτ͍ͯ͠Δ func (c *S3) PutObjectWithContext(ctx aws.Context, input

    *PutObjectInput, opts ...request.Option) (*PutObjectOutput, error) { req, out := c.PutObjectRequest(input) req.SetContext(ctx) req.ApplyOptions(opts...) return out, req.Send() } 9/13
  4. PutObjectWithContext() PutObject() ʹ͸ͳ͍Ҿ਺͕ŋŋŋ func (c *S3) PutObjectWithContext(ctx aws.Context, input *PutObjectInput,

    opts ...request.Option) (*PutObjectOutput, error) { req, out := c.PutObjectRequest(input) req.SetContext(ctx) req.ApplyOptions(opts...) return out, req.Send() } 10/13
  5. ϦΫΤετϋϯυϥΛڬΊΔ PutObjectʹ͔͔ͬͨ࣌ؒΛදࣔ͢Δ ctx, cancel := context.WithTimeout(context.Background(), time.Duration(30)*time.Second) defer cancel() svc

    := s3.New(sess) if _, err := svc.PutObjectWithContext(ctx, &s3.PutObjectInput{ Body: f, Bucket: aws.String(s3Bucket), Key: aws.String(file), }, func(r *request.Request) { start := time.Now() r.Handlers.Complete.PushBack(func(req *request.Request) { fmt.Printf("request %s took %s to complete\n", req.RequestID, time.Since(start)) }) }); err != nil { log.Fatal(err) } 11/13