从Golang REST API生成Amazon S3令牌

huangapple go评论69阅读模式
英文:

Amazon S3 token generation from a Golang REST API

问题

我正在编写一个使用Golang编写的REST API,需要为用户生成S3令牌,以便他们可以将文件上传到存储桶内的特定文件夹中。

这是我需要实现的内容:

方法:GET
端点:myapp.com/images/:imageid/token
描述:返回2个令牌,以便用户可以将文件上传到与图像ID参数相关的S3存储桶中。

我正在使用Golang的echo框架。
我不太确定如何实现这个功能。

这应该通过AWS SDK来完成,还是亚马逊提供其他以编程方式生成令牌的方法?

英文:

I'm writing a Golang REST API that needs to generate S3 tokens for users so that they can upload files to the specific folder inside a bucket.

This is what I need to implement:

Method: GET
Endpoint: myapp.com/images/:imageid/token
Description: Return 2 tokens so that the user can upload files to the S3 
             bucket related to the image ID parameter.

I'm using the Golang echo framework.
And I'm not really sure how to implement this functionality.

Should this be done via the AWS SDK or does Amazon offer other ways of programmatically generrating tokens?

答案1

得分: 1

为了为用户生成用于直接上传文件到S3的令牌,您可以使用预签名URL

生成预签名URL后,将其返回给用户,调用应用程序可以使用该URL上传文件。

下面是来自上述链接的示例,应该符合您的需求:

svc := s3.New(session.New(&aws.Config{Region: aws.String("us-west-2")}))
req, _ := svc.PutObjectRequest(&s3.PutObjectInput{
    Bucket: aws.String("myBucket"),
    Key:    aws.String("myKey"),
})
str, err := req.Presign(15 * time.Minute)

log.Println("The URL is:", str, " err:", err)
英文:

To generate tokens for users to upload files directly to s3, you can use pre-signed URLS.

After generating a pre-signed URL, return that to the user and the calling application can use that to upload the file.

This example from the link above should be about what you're looking for:

svc := s3.New(session.New(&aws.Config{Region: aws.String("us-west-2")}))
req, _ := svc.PutObjectRequest(&s3.PutObjectInput{
    Bucket: aws.String("myBucket"),
    Key:    aws.String("myKey"),
})
str, err := req.Presign(15 * time.Minute)

log.Println("The URL is:", str, " err:", err)

huangapple
  • 本文由 发表于 2017年5月6日 02:22:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/43811508.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定