我按照AWS网站上的示例进行gzipping文件并将其流式传输到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")
}
答案 0 :(得分:1)
我发现即使你可能有这样设计的结构(就像我一样):
type Table struct {
Name string
Path string
FileName string
File *os.File
Buffer *bufio.Writer
Data chan string
}
使用需要指向该结构的指针的函数不一定会使Table.File处于打开状态。
我确保文件在写入完成后关闭,并在我的上传功能中重新打开。这解决了问题并将完整的gzip压缩文件上传到S3。
感谢您对可能出现的问题@jrwren
提出异议答案 1 :(得分:0)
您是否在其他代码中致电t.File.Write()
?如果这样做,t.File
的光标可能是文件的结尾。因此,您应该将文件光标搜索到文件的原点。
在t.File.Seek(0,0)
之前致电io.Copy(gw,t.File)
(第9行)