Help us understand the problem. What is going on with this article?

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

More than 1 year has 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)
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
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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
ユーザーは見つかりませんでした