LoginSignup
4
5

More than 3 years have passed since last update.

golangでawsのs3のデータをダウンロードするコード

Last updated at Posted at 2020-02-06

下記を参考にした
https://docs.aws.amazon.com/sdk-for-go/api/service/s3/
https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/go/example_code/s3

package main

import (
    "fmt"
    "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() {
    creds := credentials.NewStaticCredentials("アクセスキー", "シークレットキー", "")

    sess := session.Must(session.NewSession(&aws.Config{
        Credentials: creds,
        Region:      aws.String("ap-northeast-1"),
    }))

    filename := "aaa.txt"

    // Create a downloader with the session and default options
    downloader := s3manager.NewDownloader(sess)

    // Create a file to write the S3 Object contents to.
    f, err := os.Create(filename)
    if err != nil {
        fmt.Println(fmt.Errorf("failed to create file %q, %v", filename, err))
        return
    }

    // Write the contents of S3 Object to the file
    n, err := downloader.Download(f, &s3.GetObjectInput{
        Bucket: aws.String("backet-name"),
        Key:    aws.String("/aaa.txt"),
    })
    if err != nil {
        fmt.Println(fmt.Errorf("failed to download file, %v", err))
        return
    }
    fmt.Printf("file downloaded, %d bytes\n", n)

}

// ついでにアップロードは下記
func upload() {

    accessKey := "your accessKey"
    privateKey := "your privateKey"
    region := "ap-northeast-1"
    fileName := "gazou.png"
    bucketName := "your bucketName"

    creds := credentials.NewStaticCredentials(accessKey, privateKey, "")
    sess := session.Must(session.NewSession(&aws.Config{
        Credentials: creds,
        Region:      aws.String(region),
    }))
    uploader := s3manager.NewUploader(sess)

    f, err := os.Open(fileName)
    if err != nil {
        log.Fatal(fmt.Errorf("failed to open file %q, %v", fileName, err))
    }
    res, err := uploader.Upload(&s3manager.UploadInput{
        Bucket: aws.String(bucketName),
        Key:    aws.String("/test/" + fileName),
        Body:   f,
    })
    if err != nil {
        fmt.Println(res)
        if err, ok := err.(awserr.Error); ok && err.Code() == request.CanceledErrorCode {
            fmt.Fprintf(os.Stderr, "upload canceled due to timeout, %v\n", err)
        } else {
            fmt.Fprintf(os.Stderr, "failed to upload object, %v\n", err)
        }
        os.Exit(1)
    }
    fmt.Println("successfully uploaded file!!")
}

exampleに、credentialsの部分の記述が見当たらなかったので、メモ的にアップしました。

それ以外はサンプル通りです。結構簡単ですね。

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5