对象存储BOS

    AWS-lambda同步S3数据到BOS

    目标场景

    AWS Lambda是一项计算服务,无需预配置或管理服务器即可运行代码,你可以通过配置AWS Lambda的触发器来执行你上传的函数代码。因此,我们可以利用AWS Lambda来实时同步用户上传到S3 Bucket的Object到BOS Bucket。

    主要步骤

    1. 登录AWS 控制台,进入Lambda服务控制台,点击“创建函数”

    2. 从头开始创建函数,该示例中运行语言选择Go 1.x(后续示例lambda程序也是Go程序,用户可以选择自己擅长的语言来实现lambda函数),角色选择从模板创建新角色即可

    3. 配置Lambda函数Desiger、函数代码、并发、内存分配、超时时间,环境变量等,按需配置

      • 基本配置:按需配置,例如你的代码执行需要的内存大小,超时时间、环境变量

        注意事项

        1. 超时时间建议填1分钟以上,Lambda函数从S3下载文件,再上传到BOS耗时与Object大小有关,有时会比较长,按实际情况设置即可;
        2. Lambda函数保存Object到磁盘,只能选择/tmp/目录,其他目录没有写权限;
        3. 内存配置与函数运行时需要的内存大小有关,按实际需要设置即可。
      • 上传lambda代码:上传代码必须为zip格式,处理程序填写Go文件的文件名

      • 配置触发器:我们选择S3触发器,进一步选择需要监听的S3存储桶,还能通过object前后缀缩小监听事件范围;添加之后会看到触发器内容为:存储桶: s3/bucket-name 事件类型: ObjectCreated

    4. 保存配置,用户每次上传object到S3 bucket,就会执行上传的Go lambda代码,同步该object到BOS;进入CloudWatch控制台可以查看程序执行的日志

    Lambda函数代码

    • 库环境准备:参考AWS S3 Golang sdk和BOS Golang sdk使用教程

      go get -u github.com/aws/aws-sdk-go/...
      go get github.com/aws/aws-lambda-go/lambda
      go get github.com/baidubce/bce-sdk-go
    • 代码保存在s32bos.go文件中,编译成二进制文件之后压缩成zip文件上传,命令如下

      GOOS=linux GOARCH=amd64 go build -o s32bos s32bos.go
      zip s32bos.zip s32bos
    • s32bos.go代码

      package main
      
      import (
      	"context"
      	"fmt"
      	"os"
      )
      
      import (
      	"github.com/aws/aws-lambda-go/events"
      	"github.com/aws/aws-lambda-go/lambda"
      	"github.com/aws/aws-sdk-go/aws"
      	"github.com/aws/aws-sdk-go/aws/credentials"
      	"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"
      	"github.com/baidubce/bce-sdk-go/services/bos"
      )
      
      var (
      	S3_REGION = "ap-northeast-1"
      	S3_AK     = "AKIAJ*****LWGRYAULYTA"
      	S3_SK     = "ABZrI****RqhrE8VzvuESvQMWXcsOPiMJ7"
      
      	BOS_AK       = "388e4e0b*****cfff3cbecb5"
      	BOS_SK       = "2db53e648c53456e***5b7d378"
      	BOS_ENDPOINT = "http://bj.bcebos.com"
      	BOS_BUCKET   = "bucket-name"
      )
      
      func downloadFromS3(bucket string, object string) {
      	file, err := os.Create("/tmp/" + object)
      	if err != nil {
      		fmt.Printf("Unable to open file, err %v", err)
      	}
      	defer file.Close()
      	// init s3 downloader
      	sess, err := session.NewSession(&aws.Config{
      		Region:      aws.String(S3_REGION),
      		Credentials: credentials.NewStaticCredentials(S3_AK, S3_SK, ""),
      	})
      	downloader := s3manager.NewDownloader(sess)
      
      	numBytes, err := downloader.Download(file,
      		&s3.GetObjectInput{
      			Bucket: aws.String(bucket),
      			Key:    aws.String(object),
      		})
      	if err != nil {
      		fmt.Printf("Unable to download object %s, size %d, err %v", object, numBytes, err)
      	}
      	fmt.Println("download from s3 success")
      }
      
      func upload2Bos(bucket string, object string) {
      	bosClient, err := bos.NewClient(BOS_AK, BOS_SK, BOS_ENDPOINT)
      	if err != nil {
      		fmt.Printf("create bos client err %v", err)
      		return
      	}
      
      	etag, err := bosClient.PutObjectFromFile(bucket, object, "/tmp/"+object, nil)
      	if err != nil {
      		fmt.Printf("put object to bos err %v", err)
      		return
      	}
      	fmt.Printf("upload2Bos success etag: %s", etag)
      }
      
      func HandleLambdaEvent(ctx context.Context, s3Event events.S3Event) {
      	fmt.Println("start to handle lambda event")
      	for _, record := range s3Event.Records {
      		s3 := record.S3
      		fmt.Printf("[%s - %s] Bucket = %s, Object = %s \n", record.EventSource, record.EventTime, s3.Bucket.Name, s3.Object.Key)
      
      		downloadFromS3(s3.Bucket.Name, s3.Object.Key)
      		upload2Bos(BOS_BUCKET, s3.Object.Key)
      
      	}
      
      	fmt.Println("handle lambda event success")
      }
      
      func main() {
      	lambda.Start(HandleLambdaEvent)
      }

    参考文档

    1. Lambda 函数处理程序 (Go)
    2. AWS SDK for Go Developer Guide
    3. BOS Go SDK
    4. aws-lambda-go
    上一篇
    数据迁移
    下一篇
    Web数据直传至BOS