golang 去S3

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了golang 去S3相关的知识,希望对你有一定的参考价值。

package main

import (
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/s3"
	"github.com/aws/aws-sdk-go/service/s3/s3manager"
)

func main() {

	// export AWS_REGION=ap-northeast-1
	// export AWS_ACCESS_KEY_ID=AKIAJFMLIYDGYVCXX3MA
	// export AWS_SECRET_ACCESS_KEY=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

	sess, err := session.NewSession()
	if err != nil {
		log.Println("failed to start the session; check AWS environment variables: ", err)
	}

	listBuckets(sess)

	// bucketName := "com.les"

	// testFileURL := "./status.html"
	// uploadObject(sess, bucketName, testFileURL)

	// listObjects(sess, bucketName)

	// _, key := filepath.Split(testFileURL)
	// fetchItem(sess, bucketName, key)
}

func listBuckets(sess *session.Session) {
	svc := s3.New(sess)

	res, err := svc.ListBuckets(nil)
	if err != nil {
		log.Println("failed to list buckets: ", err.Error())
	}

	log.Println("Buckets:")
	for _, b := range res.Buckets {
		fmt.Printf("* %s created on %s\n\n", aws.StringValue(b.Name), aws.TimeValue(b.CreationDate))
	}
}

func listObjects(sess *session.Session, bucketName string) {
	svc := s3.New(sess)

	res, err := svc.ListObjects(&s3.ListObjectsInput{
		Bucket: aws.String(bucketName)})
	if err != nil {
		log.Printf("failed to list objects in bucket %s: %v\n", bucketName, err)
	}

	log.Printf("Objects in %s: \n", bucketName)
	for _, item := range res.Contents {
		fmt.Printf("%+v\n", item)
	}
}

func listObjectsWithPrefix(sess *session.Session, bucketName string) {
	svc := s3.New(sess)

	res, err := svc.ListObjects(&s3.ListObjectsInput{Bucket: aws.String(bucketName)})
	if err != nil {
		log.Printf("failed to list objects in bucket %s: %v\n", bucketName, err)
	}

	log.Printf("Objects in %s: \n", bucketName)
	for _, item := range res.Contents {
		fmt.Printf("%+v\n", item)
	}
}

func uploadObject(sess *session.Session, bucketName, srcURL string) {

	_, filename := filepath.Split(srcURL)

	file, err := os.Open(srcURL)

	if err != nil {
		log.Printf("Unable to open file %s: %v\n", filename, err)
	}

	defer file.Close()

	uploader := s3manager.NewUploader(sess)

	_, err = uploader.Upload(&s3manager.UploadInput{
		Bucket: aws.String(bucketName),
		Key:    aws.String(filename),
		Body:   file,
	})

	if err != nil {
		log.Printf("Unable to upload %q to %q, %v", filename, bucketName, err)
		return
	}

	log.Printf("Successfully uploaded %q to %q\n", filename, bucketName)
}

func fetchItem(sess *session.Session, bucketName, key string) {
	downloader := s3manager.NewDownloader(sess)

	filename := "status_from_s3.html"
	file, err := os.Create(filepath.Join("support", filename))
	if err != nil {
		log.Printf("Unable to open file %s: %v\n", filename, err)
	}
	defer file.Close()
	sz, err := downloader.Download(file, &s3.GetObjectInput{
		Bucket: aws.String(bucketName),
		Key:    aws.String(key),
	})

	if err != nil {
		log.Printf("Unable to download %q from %q: %v", filename, bucketName, err)
	}

	fmt.Println("Downloaded", file.Name(), sz, "bytes")

}

golang笔记——string

  任何语言中,字符串操作API都是非常重要的,有些还是熟记比较好,当然如果记不住可以去看源码文件,不得不说GO语言源码看起来非常舒服。

  字符串操作相关的API大多封装在 strings 包里,下面列一些常见的

func Count(s, sep string) int

  获取指定子字符串的个数

func Contains(s, substr string) bool

  判断是否包括某子字符串

func ContainsAny(s, chars string) bool

  判断是否包括某字符串中的做任意一个字符,只要包括其中任意一个字符则返回true

func EqualFold(s, t string) bool

  忽略大小写时,判断两个字符串是否相等。

func Fields(s string) []string

  其实就是其它语言中的 Splite 函数,分隔字符串的,这个是按空格分隔。

func Split(s, sep string) []string

  按指定字符分隔字符串

func FieldsFunc(s string, f func(rune) bool) []string

  更强大的自定义分隔字符串,使用函数作为参数

    s := "a,b,c d,e,f"
    slice1 := strings.FieldsFunc(s, func(c rune) bool {
        if c == , || c ==   {
            return true
        }
        return false
    })
    print_array(slice1)

func HasPrefix(s, prefix string) bool

  判断是否以某字符串开头

func HasSuffix(s, suffix string) bool

  判断是否以某字符串结尾

 

  

接合这个看源码吧  http://www.widuu.com/archives/01/939.html

以上是关于golang 去S3的主要内容,如果未能解决你的问题,请参考以下文章

golang aws-sdk-go 之 s3 服务

golang s3cp.go

golang Amazon S3使用AWS SDK for Go选择CSV示例

golang Amazon S3使用AWS SDK for Go选择带有JSON的示例

golang中的字符串拼接

【Minio】基于AWS S3协议搭建个人云存储服务