go - 如何将文件上传到 S3 Bucket/目录

标签 go amazon-s3

我需要将文件上传到特定目录内的 S3 存储桶。我已经使用了 aws-sdk-go 包,并且能够使用存储桶中的 PutObject 函数上传文件。该文件将位于 bucket/root 目录中。

我需要做的是将它上传到 bucket/root/subDirectory 中,但我无法弄清楚这一点。

当前上传函数

_, s3Err := s3.New(s).PutObject(&s3.PutObjectInput{
        Bucket:               aws.String(envs.InitAWSEnvs().AWSS3Name),
        Key:                  aws.String(fileDir),
        ACL:                  aws.String("private"),
        Body:                 bytes.NewReader(buffer),
        ContentLength:        aws.Int64(size),
        ContentType:          aws.String(http.DetectContentType(buffer)),
        ContentDisposition:   aws.String("attachment"),
        ServerSideEncryption: aws.String("AES256"),
    })

最佳答案

我想出了一个可能对很多人有用的不太常见的 gem,path.Join 函数。

主要是 PutObject 函数调用中 PutObjectInput 参数的 Key 对象的值为 aws.String(path .Join(path_to_directory, fileName) 而不是 aws.String(fileName)

关于go - 如何将文件上传到 S3 Bucket/目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57142035/

相关文章:

go - 分段文件上传 : How to Handle FileHeaders

go - 如何在模板中转换给定的代码

amazon-s3 - 我应该有多少个 S3 存储桶?

javascript - Amazon S3 仅向 react native 中的应用程序用户提供文件

linux - 通过 AWS CLI 进行 AWS 转录

node.js - Node : How to stub AWS s3 getobject with sinon

unit-testing - Testify 似乎同时运行测试套件?

go - 在 Golang 中传递 interface{} 或 []interface{}

java - s3 分段上传总是在第二部分失败并超时

google-app-engine - Go 中的 AppEngine 标准环境 Pub/Sub 上下文