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

Go Cloud を触ってみる / gopher-dojo-lt

Avatar for d-kuro d-kuro
September 18, 2018

Go Cloud を触ってみる / gopher-dojo-lt

Gopher 道場#3 卒業式& LT 大会の LT スライドになります
https://mercari.connpass.com/event/101178/

Avatar for d-kuro

d-kuro

September 18, 2018
Tweet

More Decks by d-kuro

Other Decks in Programming

Transcript

  1. ֤छΫϥ΢υϓϩόΠμͰ ࣅͨΑ͏ͳαʔϏε • Storage • Amazon S3 • Google Cloud

    Storage • MySQL • Amazon RDS • Cloud SQL • ͳͲͳͲ (ଞʹ΋͍ͬͺ͍)
  2. ࣅͨΑ͏ͳαʔϏεͰ΋ API ͷΠϯλϑΣʔε͸ҧ͏ // Amazon S3 func (c *S3) PutObject(input

    *PutObjectInput) (*PutObjectOutput, error) // Google Cloud Strage func (o *ObjectHandle) NewWriter (ctx context.Context) *Writer
  3. Go Cloud ͱ͸ • ༷ʑͳΫϥ΢υͷػೳΛ൚༻తͳ API Ͱར༻Ͱ͖Δ
 (Α͏ʹͳΔ͜ͱΛ໨ࢦͯ͠Δ) • OSS

    • ݱ࣌఺Ͱ͸ AWS ͱ GCP ʹରԠ • ࠓޙ΋ͬͱଟ͘ͷΫϥ΢υϓϩόΠμʹରԠ͢Δܭը • ·ͩ v0.2.0 (ൃද͞Εͨͷ͸ 18.07.24)
  4. ·ͩ v0.2.0 • Ͱ͖Δ͜ͱ͕গͳ͍ • Blob storage • Runtime configuration

    • Connecting MySQL • Server startup, request logging, tracing,
 health checking
  5. ؀ڥߏங go get github.com/google/go-cloud go get github.com/google/go-cloud/wire/cmd/wire • go get

    ͢Δ • Wire ͸ DI πʔϧ • Ϋϥ΢υݻ༗ͷηοτΞοϓίʔυΛࣗಈͰੜ੒ͯ͘͠ΕΔ • ࠓճ͸࢖Θͳ͍ͷͰৄ͘͠͸υΩϡϝϯτΛಡ΋͏
 (https://github.com/google/go-cloud/tree/master/wire)
  6. Go 1.11 Modules • Go 1.11 ͔Βࢼݧಋೖ͞Ε͍ͯΔ Modules Ͱ
 go

    build ͨ͠ͱ͖ʹґଘύοέʔδΛউखʹμ΢ϯϩʔυ͢Δ • go.mod ʹґଘύοέʔδ͕ॻ͍ͯ͋Δ • https://github.com/google/go-cloud/blob/master/go.mod • ͪΌΜͱ Go ͷόʔδϣϯΛ্͛Α͏ • GO111MODULE=on Λ๨Εͣʹ # ໌ࣔతͳґଘύοέʔδͷμ΢ϯϩʔυΛ͍ͨ͠ͱ͖ $ go mod download
  7. TIPS • IntelliJ ͷͻͱ͸ Go Module ༗ޮʹ͠ͱ͖·͠ΐ͏ • GoLand ͷਓͷͻͱ͸ͬͪ͜

    • (https://pleiades.io/help/go/create-a-project-with-vgo-integration.html)
  8. ηοτΞοϓίʔυ • Go Cloud Ͱ͸ಛఆͷϓϥοτϑΥʔϜʹґଘ͢Δ΋ͷ͸ ηοτΞοϓίʔυͷΈ • ֤ϓϥοτϑΥʔϜͷ SDK Ͱૢ࡞Λ͍ͯͨ͠Γ͢Δͱ

    ϏδωεϩδοΫ΋ϓϥοτϑΥʔϜʹґଘͯ͠͠·͏ • ϏδωεϩδοΫ͸ಛఆͷϓϥοτϑΥʔϜʹґଘ͠ͳ͍ • ผͷΫϥ΢υʹҠߦ͢Δͷ͕؆୯ʹͳΔ
  9. Setup GCP func setupGCP(ctx context.Context, bucket string) (*blob.Bucket, error) {

    // DefaultCredentials assumes a user has logged in with gcloud. // See here for more information: // https://cloud.google.com/docs/authentication/getting-started creds, err := gcp.DefaultCredentials(ctx) if err != nil { return nil, err } c, err := gcp.NewHTTPClient(gcp.DefaultTransport(), gcp.CredentialsTokenSource(creds)) if err != nil { return nil, err } // The bucket name must be globally unique. return gcsblob.OpenBucket(ctx, bucket, c) }
  10. Setup AWS func setupAWS(ctx context.Context, bucket string) (*blob.Bucket, error) {

    c := &aws.Config{ // Either hard-code the region or use AWS_REGION. Region: aws.String("us-east-2"), // credentials.NewEnvCredentials assumes two environment variables are // present: // 1. AWS_ACCESS_KEY_ID, and // 2. AWS_SECRET_ACCESS_KEY. Credentials: credentials.NewEnvCredentials(), } s := session.Must(session.NewSession(c)) return s3blob.OpenBucket(ctx, s, bucket) }
  11. ಉ͡໭Γ஋Λฦ͢ // AWS func OpenBucket(ctx context.Context, sess client.ConfigProvider, bucketName string)

    (*blob.Bucket, error) // GCP func OpenBucket(ctx context.Context, bucketName string, client *gcp.HTTPClient) (*blob.Bucket, error)
  12. blob.Bucket type Bucket struct { b driver.Bucket } type Bucket

    interface { NewRangeReader(ctx context.Context, key string, offset, length int64) (Reader, error) NewTypedWriter(ctx context.Context, key string, contentType string, opt *WriterOptions) (Writer, error) Delete(ctx context.Context, key string) error }
  13. ֤ૢ࡞ͷந৅Խ • Go Cloud Ͱ͸֤Ϋϥ΢υαʔϏεͷૢ࡞ΛΠϯλϑΣʔε ͱͯ͠ந৅Խ͍ͯ͠Δ • ͦΕͧΕͷ SDK ͷૢ࡞Λ

    Go Cloud ͕ϥοϓ • ͲͷΫϥ΢υͳͷ͔Λҙࣝ͢Δ͜ͱͳ͘αʔϏεΛར༻ Ͱ͖Δ b, err := setupBucket(context.Background(), *cloud, *bucketName) … w, err := b.NewWriter(ctx, file, nil)
  14. ·ͱΊ • Go Cloud ͸֤Ϋϥ΢υϓϩόΠμؒͷΠϯλ ϑΣʔεͷҧ͍Λٵऩ͢Δ൚༻తͳ API Λఏ ڙͯ͘͠ΕΔ •

    ·ͩ·ͩͰ͖Δ͜ͱ͸গͳ͍ͷͰࠓޙʹظ଴ • ࠓͳΒίϯτϦϏϡʔγϣϯνϟϯε