通过 AWS SDK GO 将 gzip 压缩文件流式传输到 S3

我按照 AWS 站点上的示例对文件进行 gzip 压缩并将它们流式传输到 S3,可在此处找到:http: //docs.aws.amazon.com/sdk-for-go/latest/v1/developerguide/common-examples.title。 html


我有一个问题,我的 S3 存储桶中唯一登陆的是基本上只有 GZIP 标头的文件。每个文件的大小为 23b。


知道什么会导致这种情况吗?


我的代码:


func (t *Table) Upload() {

  year := time.Now().Format("2006")

  month := time.Now().Format("01")

  day := time.Now().Format("02")

  reader, writer := io.Pipe()

  go func() {

    gw := gzip.NewWriter(writer)

    io.Copy(gw, t.File)

    t.File.Close()

    gw.Close()

    writer.Close()

  }()

  uploader := s3manager.NewUploader(session.New(&aws.Config{Region: aws.String(os.Getenv("AWS_REGION"))}))

  result, err := uploader.Upload(&s3manager.UploadInput{

    Body:   reader,

    Bucket: aws.String(os.Getenv("S3_BUCKET")),

    Key:    aws.String(fmt.Sprintf("%s/%s/%s/%s/%s", os.Getenv("S3_KEY"), year, month, day, t.Name+".csv.gz")),

  })

  if err != nil {

    log.WithField("error", err).Fatal("Failed to upload file.")

  }

  log.WithField("location", result.Location).Info("Successfully uploaded to")

}


慕虎7371278
浏览 215回答 2
2回答

森林海

我发现即使您可能有这样设计的结构(就像我一样):type Table struct {                                                                                                                                                                                                                                                                                                             Name     string                                                                                                                                                                                                                                                                                                               Path     string                                                                                                                                                                                                                                                                                                               FileName string                                                                                                                                                                                                                                                                                                               File     *os.File                                                                                                                                                                                                                                                                                                             Buffer   *bufio.Writer                                                                                                                                                                                                                                                                                                        Data     chan string                                                                                                                                                                                                                                                                                                        }使用需要指向该结构的指针的函数不一定会使 Table.File 处于打开状态。我确保在写入文件完成时文件已关闭,并在我的上传功能中重新打开它。这解决了问题并将完整的 gzip 文件上传到 S3。

德玛西亚99

您是否调用t.File.Write()其他代码?如果你这样做,光标t.File可能是文件的结尾。所以你应该将文件光标寻找到文件的来源。t.File.Seek(0,0) 之前调用 io.Copy(gw,t.File)(第 9 行)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go