dongliechuich10319 2014-04-04 15:32
浏览 59
已采纳

连接到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:

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

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:

    type Auth struct {
        AccessKey, SecretKey string
    }

And it seems like I need to call this function:

    func EnvAuth() (auth Auth, err error)

This is the EnvAuth function:

    func EnvAuth() (auth Auth, err error) {
        auth.AccessKey = os.Getenv("AWS_ACCESS_KEY_ID")
        auth.SecretKey = os.Getenv("AWS_SECRET_ACCESS_KEY")
        // We fallback to EC2_ env variables if the AWS_ variants are not used.
        if auth.AccessKey == "" && auth.SecretKey == "" {
            auth.AccessKey = os.Getenv("EC2_ACCESS_KEY")
            auth.SecretKey = os.Getenv("EC2_SECRET_KEY")
        }
        if auth.AccessKey == "" {
            err = errors.New("AWS_ACCESS_KEY_ID not found in environment")
        }
        if auth.SecretKey == "" {
            err = errors.New("AWS_SECRET_ACCESS_KEY not found in environment")
        }
        return
     }

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 :)

  • 写回答

2条回答 默认 最新

  • dongshan0202405 2014-04-04 16:10
    关注

    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...

    package main
    
    import (
        "fmt"
        "launchpad.net/goamz/aws"
        "launchpad.net/goamz/s3"
        "log"
    )
    
    func main() {
        auth := aws.Auth{
            AccessKey: "ASDFASDFASDFASDK",
            SecretKey: "DSFSDFDWESDADSFASDFADFDSFASDF",
        }
        euwest := aws.EUWest
    
        connection := s3.New(auth, euwest)
        mybucket := connection.Bucket("mytotallysecretbucket")
        res, err := mybucket.List("", "", "", 1000)
        if err != nil {
            log.Fatal(err)
        }
        for _, v := range res.Contents {
            fmt.Println(v.Key)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测