连接到S3

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

Go Connecting to S3

问题

我正在学习Go,并且正在编写一个管理图片的组件。

我在这里看到了s3库:https://godoc.org/launchpad.net/goamz/s3#ACL

在Node中,我使用Knox客户端并像这样连接到我的存储桶:

  1. var bucket = knox.createClient({
  2. key: config.get('AWS_KEY'),
  3. secret: config.get('AWS_SECRET'),
  4. bucket: "bucketName"
  5. });

在Go的s3库中,我看到了所有我需要使用s3存储桶的函数,但是我找不到连接函数 - 类似上面的那个。

到目前为止,我在文档中找到了这个:

  1. type Auth struct {
  2. AccessKey, SecretKey string
  3. }

而且我似乎需要调用这个函数:

  1. func EnvAuth() (auth Auth, err error)

这是EnvAuth函数:

  1. func EnvAuth() (auth Auth, err error) {
  2. auth.AccessKey = os.Getenv("AWS_ACCESS_KEY_ID")
  3. auth.SecretKey = os.Getenv("AWS_SECRET_ACCESS_KEY")
  4. // 如果没有使用AWS_变量,则回退到EC2_环境变量。
  5. if auth.AccessKey == "" && auth.SecretKey == "" {
  6. auth.AccessKey = os.Getenv("EC2_ACCESS_KEY")
  7. auth.SecretKey = os.Getenv("EC2_SECRET_KEY")
  8. }
  9. if auth.AccessKey == "" {
  10. err = errors.New("在环境中找不到AWS_ACCESS_KEY_ID")
  11. }
  12. if auth.SecretKey == "" {
  13. err = errors.New("在环境中找不到AWS_SECRET_ACCESS_KEY")
  14. }
  15. return
  16. }

在S3文档中,我看到了我需要的所有内容。我只是不确定如何使用该库,这是我第一次使用Go库。

关于连接到AWS/S3,然后进行删除调用的指南将非常有帮助!

非常感谢 连接到S3

英文:

Working on learning Go, and I am writing a component to manage pictures.

I've been looking at the s3 library here: https://godoc.org/launchpad.net/goamz/s3#ACL

In Node, I use the Knox client and connect to my bucket like this:

  1. var bucket = knox.createClient({
  2. key: config.get('AWS_KEY'),
  3. secret: config.get('AWS_SECRET'),
  4. bucket: "bucketName"
  5. });

In the Go s3 library I see all of the functions I need to work with the s3 bucket, but I can't find the connect function - similar to this one above.

So far, I've found this in the Docs:

  1. type Auth struct {
  2. AccessKey, SecretKey string
  3. }

And it seems like I need to call this function:

  1. func EnvAuth() (auth Auth, err error)

This is the EnvAuth function:

  1. func EnvAuth() (auth Auth, err error) {
  2. auth.AccessKey = os.Getenv("AWS_ACCESS_KEY_ID")
  3. auth.SecretKey = os.Getenv("AWS_SECRET_ACCESS_KEY")
  4. // We fallback to EC2_ env variables if the AWS_ variants are not used.
  5. if auth.AccessKey == "" && auth.SecretKey == "" {
  6. auth.AccessKey = os.Getenv("EC2_ACCESS_KEY")
  7. auth.SecretKey = os.Getenv("EC2_SECRET_KEY")
  8. }
  9. if auth.AccessKey == "" {
  10. err = errors.New("AWS_ACCESS_KEY_ID not found in environment")
  11. }
  12. if auth.SecretKey == "" {
  13. err = errors.New("AWS_SECRET_ACCESS_KEY not found in environment")
  14. }
  15. return
  16. }

In the S3 docs, I see all of the things that I need. I am just unsure about how I use the library, this is the first time I use a Go library.

A guide on connecting to AWS/S3, then making a delete call would be very helpful!

Many thanks 连接到S3

答案1

得分: 27

这是一个示例代码,用于列出一个存储桶的内容。当然,你需要使用你自己的凭证和存储桶名称。

  1. package main
  2. import (
  3. "fmt"
  4. "launchpad.net/goamz/aws"
  5. "launchpad.net/goamz/s3"
  6. "log"
  7. )
  8. func main() {
  9. auth := aws.Auth{
  10. AccessKey: "ASDFASDFASDFASDK",
  11. SecretKey: "DSFSDFDWESDADSFASDFADFDSFASDF",
  12. }
  13. euwest := aws.EUWest
  14. connection := s3.New(auth, euwest)
  15. mybucket := connection.Bucket("mytotallysecretbucket")
  16. res, err := mybucket.List("", "", "", 1000)
  17. if err != nil {
  18. log.Fatal(err)
  19. }
  20. for _, v := range res.Contents {
  21. fmt.Println(v.Key)
  22. }
  23. }

这段代码使用了launchpad.net/goamz库来连接到 AWS S3 服务,并列出了名为mytotallysecretbucket的存储桶中的内容。你需要将AccessKeySecretKey替换为你自己的凭证信息,以及将mytotallysecretbucket替换为你要操作的存储桶名称。

英文:

It's probably easier than you've thought. This sample code lists a bucket. You have to use your credentials and a bucket name, of course...

  1. package main
  2. import (
  3. "fmt"
  4. "launchpad.net/goamz/aws"
  5. "launchpad.net/goamz/s3"
  6. "log"
  7. )
  8. func main() {
  9. auth := aws.Auth{
  10. AccessKey: "ASDFASDFASDFASDK",
  11. SecretKey: "DSFSDFDWESDADSFASDFADFDSFASDF",
  12. }
  13. euwest := aws.EUWest
  14. connection := s3.New(auth, euwest)
  15. mybucket := connection.Bucket("mytotallysecretbucket")
  16. res, err := mybucket.List("", "", "", 1000)
  17. if err != nil {
  18. log.Fatal(err)
  19. }
  20. for _, v := range res.Contents {
  21. fmt.Println(v.Key)
  22. }
  23. }

答案2

得分: 14

原始帖子使用了goamz库。AWS维护着官方的aws-sdk-go库,应该使用它来替代。

请参考下面的示例中的connect方法,它使用来自AWS的官方Go SDK列出特定存储桶中的所有键:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/aws/aws-sdk-go/aws"
  5. "github.com/aws/aws-sdk-go/aws/session"
  6. "github.com/aws/aws-sdk-go/service/s3"
  7. )
  8. func main() {
  9. svc := s3.New(session.New(), &aws.Config{Region: aws.String("us-east-1")})
  10. params := &s3.ListObjectsInput{
  11. Bucket: aws.String("bucket"),
  12. }
  13. resp, _ := svc.ListObjects(params)
  14. for _, key := range resp.Contents {
  15. fmt.Println(*key.Key)
  16. }
  17. }
英文:

The original post uses the goamz library. AWS maintains the official aws-sdk-go library which should be used instead.

See the connect method in the below example, which lists all keys in a specific bucket using official Go sdk from AWS:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/aws/aws-sdk-go/aws"
  5. "github.com/aws/aws-sdk-go/aws/session"
  6. "github.com/aws/aws-sdk-go/service/s3"
  7. )
  8. func main() {
  9. svc := s3.New(session.New(), &aws.Config{Region: aws.String("us-east-1")})
  10. params := &s3.ListObjectsInput{
  11. Bucket: aws.String("bucket"),
  12. }
  13. resp, _ := svc.ListObjects(params)
  14. for _, key := range resp.Contents {
  15. fmt.Println(*key.Key)
  16. }
  17. }

huangapple
  • 本文由 发表于 2014年4月4日 23:32:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/22867013.html
匿名

发表评论

匿名网友

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

确定