gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 21:28:25 26 4
gpt4 key购买 nike

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

Code as gist to show both the server and the client code.

最佳答案

这里有多个问题。

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

    "github.com/tus/tusd/pkg/handler"
"github.com/tus/tusd/pkg/s3store"

您没有正确使用 S3 存储,而是设置了配置以直接在服务器上存储存储
fStore := filestore.FileStore{
Path: "./uploads",
}

相反,它应该是这样的:
// 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))
}

您的客户也可能工作不正常(我没有测试过)。

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

关于Golang HTTP 上传文件到 S3 使用 tusd 仅上传元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55199956/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com