Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What is going on with this article?
@sakayuka

GoでURLからファイルを取得してS3へ保存

More than 3 years have passed since last update.

一度ファイルを作成してから、S3へ配置します。
(ストリームのままS3へ保存する方法教えてください!)

// download file
response, err := http.Get(url)
if err != nil {
  return fmt.Errorf("download error: %s - %s", url, err)
}
defer response.Body.Close()

// create file
output, err := os.Create(filename)
if err != nil {
  return fmt.Errorf("file create error: %s - %s", filename, err)
}
defer output.Close()

// copy
n, err := io.Copy(output, response.Body)
if err != nil {
  return fmt.Errorf("file copy error: %s - %s", filename, err)
}

cli := s3.New(session.New(), aws.NewConfig().WithRegion("ap-northeast-1"))

input := &s3.PutObjectInput{
  Body:   output,
  Bucket: aws.String("bucket-name"),
  Key:    aws.String("filename"),
}
if _, err := cli.PutObject(input); err != nil {
  return fmt.Errorf("put object error: %s", err)
}

// delete file
if err := os.Remove(filename); err != nil {
  return fmt.Errorf("file delete error: %s - %s", filename, err)
}

fmt.Printf("download from %s -> %s (%dbytes)\n", url, filename, n)
1
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
sakayuka

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
1
Help us understand the problem. What is going on with this article?