Golang HTTP使用tusd将文件上传到S3仅上传元数据

aml*_*ker 1 upload amazon-s3 go tus

我正在使用 tusd 库将文件直接上传到 Go 中的 S3。它似乎可以正常工作,但是 tusd 上传两个文件:.info 元数据文件和 .bin 实际内容文件。由于某种原因,我的代码仅上传信息文件。该文档很难导航,所以也许我错过了某个地方的设置

代码要点显示服务器和客户端代码。

小智 5

这里有多个问题。

您的 tus 库导入路径是错误的,它们应该是:

    "github.com/tus/tusd/pkg/handler"
    "github.com/tus/tusd/pkg/s3store"
Run Code Online (Sandbox Code Playgroud)

您没有正确使用 S3 存储,您设置了一个配置来直接在服务器上存储

fStore := filestore.FileStore{
        Path: "./uploads",
    }
Run Code Online (Sandbox Code Playgroud)

相反,它应该是这样的:

// S3 acces configuration
 s3Config := &aws.Config{
     Region:      aws.String(os.Getenv("AWS_REGION")),
     Credentials: credentials.NewStaticCredentials(os.Getenv("AWS_ACCESS_KEY_ID"), os.Getenv("AWS_SECRET_ACCESS_KEY"), ""),
     DisableSSL:       aws.Bool(true),
     S3ForcePathStyle: aws.Bool(true),
 }

// Setting up the s3 storage
 s3Store := s3store.New(os.Getenv("AWS_BUCKET_NAME"), s3.New(session.Must(session.NewSession()), s3Config))

// Creates a new and empty store composer   
 composer := handler.NewStoreComposer()
// UseIn sets this store as the core data store in the passed composer and adds all possible extension to it.
 s3Store.UseIn(composer)

// Setting up handler
 handler, err := handler.NewHandler(handler.Config{
     BasePath:                "/files/",
     StoreComposer:           composer,
 })
 if err != nil {
     panic(fmt.Errorf("Unable to create handler: %s", err))
 }

// Listen and serve
 http.Handle("/files/", http.StripPrefix("/files/", handler))
 err = http.ListenAndServe(":8080", nil)
 if err != nil {
     panic(fmt.Errorf("Unable to listen: %s", err))
 }
Run Code Online (Sandbox Code Playgroud)

您的客户端也可能无法正常工作(我没有测试它)。

我建议您使用https://github.com/eventials/go-tus而不是尝试自己实现该协议。