Golang amazon s3视频上传出现奇怪错误
我使用Golang作为我的后端代码,我有一个允许人们上传图像和视频的应用程序。我有图像和视频工作,但现在由于某种原因,我得到了这个错误,这是一样的。我已经检查过了,因为返回的文件有一定的大小,所以视频正在转换,但由于某些原因,在上传视频时,我一直收到下面的错误。。。错误发生在下面的PutObject中,任何建议都会很好Golang amazon s3视频上传出现奇怪错误,go,amazon-s3,Go,Amazon S3,我使用Golang作为我的后端代码,我有一个允许人们上传图像和视频的应用程序。我有图像和视频工作,但现在由于某种原因,我得到了这个错误,这是一样的。我已经检查过了,因为返回的文件有一定的大小,所以视频正在转换,但由于某些原因,在上传视频时,我一直收到下面的错误。。。错误发生在下面的PutObject中,任何建议都会很好 func UploadStreamVideo(w http.ResponseWriter, r *http.Request) { r.ParseForm() v
func UploadStreamVideo(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
var buf bytes.Buffer
sess, _ := session.NewSession(&aws.Config{
Region: aws.String("us-west-2"),
Credentials: credentials.NewStaticCredentials(aws_access_key_id, aws_secret_access_key, ""),
})
svc := s3.New(sess)
file, handler, err := r.FormFile("file")
if err != nil {
log_errors("HomePage-UploadStreamImage: error receiving file", err.Error(), w)
fmt.Println("Error Uploading Image")
return
}
defer file.Close()
println(handler.Filename) // I can see filename
println(handler.Size) // I also see the size here
read_file, err := ioutil.ReadAll(file)
if err != nil {
log_errors("HomePage-UploadStreamImage")
return
}
file.Read(read_file)
fileBytes := bytes.NewReader(read_file)
fileSize, err := buf.ReadFrom(file)
if err != nil {
log_errors("HomePage-UploadStreamImage")
}
fileType := http.DetectContentType(read_file)
path := handler.Filename
params := &s3.PutObjectInput{
Bucket: aws.String("yisl"),
Key: aws.String(path),
Body: fileBytes,
ContentLength: aws.Int64(fileSize),
ContentType: aws.String(fileType),
}
// This code below gives the error
_, err = svc.PutObject(params)
if err != nil {
println(err.Error())
log_errors("HomePage-UploadStreamImage")
return
}
io.WriteString(w, "Done")
}
错误是:
NotImplemented: A header you provided implies functionality that is
not implemented status code: 501, request id: 310D0CE7643E1B4B, host
id:
GUB8UDi9KJTIPybXh3I1m5Vu8Ixyt4LQv5PbVnyOH2ngAdtOv0DsyALHp1mMI3SmRpK+xHV3uds=
> 2018/05/12 03:30:21 http: panic serving [::1]:64945: runtime error:
> invalid memory address or nil pointer dereference goroutine 10
> [running]: net/http.(*conn).serve.func1(0xc420229400)
> /usr/local/go/src/net/http/server.go:1726 +0xd0 panic(0x1451b20,
> 0x1752c30) /usr/local/go/src/runtime/panic.go:502 +0x229
> database/sql.(*Rows).close(0x0, 0x0, 0x0, 0x0, 0x0)
> /usr/local/go/src/database/sql/sql.go:2907 +0x6b
> database/sql.(*Rows).Close(0x0, 0x14f5698, 0x4d)
> /usr/local/go/src/database/sql/sql.go:2903 +0x33
> _/Users/ignaciohernandez/Documents/go/Yisly-Backend/Controllers.log_errors(0x14ef38b,
> 0x2e, 0xc42010aa80, 0xda, 0x15737c0, 0xc420244380)
> /Users/ignaciohernandez/Documents/go/Yisly-Backend/Controllers/function_store.go:27
> +0x2d8
> _/Users/ignaciohernandez/Documents/go/Yisly-Backend/Controllers.UploadStreamVideo(0x15737c0,
> 0xc420244380, 0xc420211700)
> /Users/ignaciohernandez/Documents/go/Yisly-Backend/Controllers/HomePage.go:377
> +0x7ad net/http.HandlerFunc.ServeHTTP(0x14fd130, 0x15737c0, 0xc420244380, 0xc420211700) /usr/local/go/src/net/http/server.go:1947
> +0x44 github.com/gorilla/mux.(*Router).ServeHTTP(0xc420142380, 0x15737c0, 0xc420244380, 0xc420211700)
> /Users/ignaciohernandez/Documents/go/src/github.com/gorilla/mux/mux.go:162
> +0xed net/http.(*ServeMux).ServeHTTP(0x1760f20, 0x15737c0, 0xc420244380, 0xc420211500) /usr/local/go/src/net/http/server.go:2337
> +0x130 net/http.serverHandler.ServeHTTP(0xc4201c89c0, 0x15737c0, 0xc420244380, 0xc420211500) /usr/local/go/src/net/http/server.go:2694
> +0xbc net/http.(*conn).serve(0xc420229400, 0x1573b40, 0xc42002c9c0) /usr/local/go/src/net/http/server.go:1830 +0x651 created by
> net/http.(*Server).Serve /usr/local/go/src/net/http/server.go:2795
> +0x27b
您的错误消息有点混乱。你就是这么看的吗?或者这是两个错误(一个错误,后跟堆栈跟踪)?正如@Flimzy所说,整个事情有点混乱。所以我的建议是从一开始就开始稍微打扫一下。首先是
ioutil.ReadAll
,然后是file.Read
(但是由于您是ioutil.ReadAll,所以光标位于最末尾,所以应该没有字节)。您不需要执行该过程来获取文件大小,因为您已经有了handler.size
。查看PutObject的AWS S3文档(下面的链接),主体是io.readseek
和r.FormFile(“文件”)时得到的多部分.file
实现了io.Reader
和io.Seeker
,因此我认为您可以将文件直接传递到PutObject
。