Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过AWS SDK GO将Gzip文件流式传输到S3_Go_Amazon S3_Gzip_Aws Sdk - Fatal编程技术网

通过AWS SDK GO将Gzip文件流式传输到S3

通过AWS SDK GO将Gzip文件流式传输到S3,go,amazon-s3,gzip,aws-sdk,Go,Amazon S3,Gzip,Aws Sdk,我遵循AWS站点上的示例将文件压缩并流式传输到S3,如下所示: 我遇到了一个问题,在我的S3存储桶中唯一登陆的是基本上只有GZIP头的文件。每个文件的大小都是23b 知道这是什么原因吗 我的代码: func (t *Table) Upload() { year := time.Now().Format("2006") month := time.Now().Format("01") day := time.Now().Format("02") reader, writer :=

我遵循AWS站点上的示例将文件压缩并流式传输到S3,如下所示:

我遇到了一个问题,在我的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")
}

我发现,即使您可能有这样设计的结构(如我所做的):

使用需要指向该结构的指针的函数不一定会使Table.File处于打开状态

我确保文件在写入完成时已关闭,并在上载功能中重新打开。这解决了问题,并将完整的gzip文件上载到S3


感谢您就可能出现的问题提供的提示@jrwren

您是否在其他代码中调用
t.File.Write()
?如果这样做,则
t.File
的光标可能是文件的结尾。因此,您应该将文件光标搜索到文件的原点


调用
t.File.Seek(0,0)
io.Copy(gw,t.File)
(第9行)

什么是t.File,它打开了吗?io.复制副本直到EOF或错误。您应该检查io.Copy的返回值是否有写入的字节和错误。t(或Table)是一个具有指向FileI的打开指针的结构。我确实更新了io.Copy to:\uu,err:=io.Copy(gw,t.File)只是为了检查是否有错误,而没有错误。您使用这种方法是否遇到过性能问题?对我来说,这似乎很慢
type Table struct {                                                                                                                                                                                                                                                                                                           
  Name     string                                                                                                                                                                                                                                                                                                             
  Path     string                                                                                                                                                                                                                                                                                                             
  FileName string                                                                                                                                                                                                                                                                                                             
  File     *os.File                                                                                                                                                                                                                                                                                                           
  Buffer   *bufio.Writer                                                                                                                                                                                                                                                                                                      
  Data     chan string                                                                                                                                                                                                                                                                                                        
}