在 Go 中将文件放入 amazon s3 后如何获取文件 url?

Web*_*per 6 file-upload amazon-s3 go

文件上传成功,上传后我想返回文件url。

_, err := s3.New(s).PutObject(&s3.PutObjectInput{
    Bucket:               aws.String("bucket"),
    Key:                  aws.String(tempFileName),
    ACL:                  aws.String("public-read"), // could be private if you want it to be access by only authorized users
    Body:                 bytes.NewReader(buffer),
    ContentLength:        aws.Int64(int64(size)),
    ContentType:          aws.String(http.DetectContentType(buffer)),
    ContentDisposition:   aws.String("attachment"),
    ServerSideEncryption: aws.String("AES256"),
    StorageClass:         aws.String("INTELLIGENT_TIERING"),
})
Run Code Online (Sandbox Code Playgroud)

我已经检查了亚马逊文档中的 PutObject 函数,但是 PutObjectOutput 结构类型 但此结构中没有上传的文件 url。

我怎样才能获取文件的url?上传成功后是否有其他方法在 amazon s3 sdk 中返回上传的文件 url?

Joh*_*ein 3

您可以将 URL 构造为:

https://BUCKETNAME.s3-REGIONNAME.amazonaws.com/KEY
Run Code Online (Sandbox Code Playgroud)

例如:

https://my-bucket.s3-ap-southeast-2.amazonaws.com/foo/bar.txt
Run Code Online (Sandbox Code Playgroud)