英文:
Simple Example of how to get the un-presigned s3 object url using the golang aws sdk v2
问题
我在Google/Stack Overflow上看到很多使用s3client.GetRequestObject的示例,但这个方法在SDK的v2版本中已被移除。
client := s3.NewFromConfig(cfg)
params := &s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
}
req, _ := client.GetRequestObject(params)
我查看了SDK v2的示例,但没有找到一个明显的示例来完成这个操作。
我不想下载文件,只是提供一个下载链接。
英文:
I see lots of example on google/stackoverflow using s3client.GetRequestObject which is now removed from v2 of the sdk
client := s3.NewFromConfig(cfg)
params := &s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
}
req, _ := client.GetRequestObject(params)
https://stackoverflow.com/questions/46575011/how-to-get-resource-url-from-aws-s3-in-a-golang
I looked through the sdk v2 examples:
https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/gov2/s3/
And nothing stands out to me as an example of how to do this.
I don't want to download, just provide a link to download
答案1
得分: 1
以下是获取带有过期时间的对象的预签名URL的示例:
s3client := s3.NewFromConfig(cfg)
presignClient := s3.NewPresignClient(s3client)
presignedUrl, err := presignClient.PresignGetObject(context.Background(),
&s3.GetObjectInput{
Bucket: aws.String("bucket-name"),
Key: aws.String("key-name"),
},
s3.WithPresignExpires(time.Minute*15))
if err != nil {
log.Fatal(err)
}
fmt.Println(presignedUrl.URL)
在此链接中查看我关于S3预签名URL的文章:https://ronen-niv.medium.com/aws-s3-handling-presigned-urls-2718ab247d57
英文:
Here is an example of getting presigned URL for an object with an expiration time:
s3client := s3.NewFromConfig(cfg)
presignClient := s3.NewPresignClient(s3client)
presignedUrl, err := presignClient.PresignGetObject(context.Background(),
&s3.GetObjectInput{
Bucket: aws.String("bucket-name"),
Key: aws.String("key-name"),
},
s3.WithPresignExpires(time.Minute*15))
if err != nil {
log.Fatal(err)
}
fmt.Println(presignedUrl.URL)
See my article about S3 presigned URLs in this link https://ronen-niv.medium.com/aws-s3-handling-presigned-urls-2718ab247d57
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论