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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了golang Amazon S3使用AWS SDK for Go选择CSV示例相关的知识,希望对你有一定的参考价值。

package main

import (
	"fmt"
	"os"

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

func exitErrorf(msg string, args ...interface{}) {
	fmt.Fprintf(os.Stderr, msg+"\n", args...)
	os.Exit(1)
}

func main() {
	sess := session.Must(session.NewSession())
	svc := s3.New(
		sess,
		aws.NewConfig().WithRegion("ap-northeast-1"),
	)


	params := &s3.SelectObjectContentInput{
		Bucket:          aws.String("s3-select-sampleyy"),
		Key:             aws.String("FIFA2018_Statistics.csv"),
		ExpressionType:  aws.String(s3.ExpressionTypeSql),
		Expression:      aws.String("SELECT * FROM S3Object LIMIT 10"),
		RequestProgress: &s3.RequestProgress{},
		InputSerialization: &s3.InputSerialization{
			CompressionType: aws.String("NONE"),
			CSV: &s3.CSVInput{
				FileHeaderInfo: aws.String(s3.FileHeaderInfoUse),
			},
		},
		OutputSerialization: &s3.OutputSerialization{
			CSV: &s3.CSVOutput{},
		},
	}

	resp, err := svc.SelectObjectContent(params)
	if err != nil {
		exitErrorf("failed making API request", err)
	}
	defer resp.EventStream.Close()

	for event := range resp.EventStream.Events() {
	 // v, ok := event.(*s3.RecordsEvent)
		// if ok {
		// 	fmt.Println(string(v.Payload))
		// }
		switch v := event.(type) {
		case *s3.RecordsEvent:
		  // s3.RecordsEvent.Records is a byte slice of select records
			fmt.Println(string(v.Payload))
		case *s3.StatsEvent:
		  // s3.StatsEvent contains information on the data that’s processed
			fmt.Println("Processed", *v.Details.BytesProcessed, "bytes")
			fmt.Println("Returned", *v.Details.BytesReturned, "bytes")
			fmt.Println("Scanned", *v.Details.BytesScanned, "bytes")
		case *s3.EndEvent:
			fmt.Println("SelectObjectContent completed")
		}
	}

	if err := resp.EventStream.Err(); err != nil {
		exitErrorf("failed to read from SelectObject EventStream, %v", err)
	}
}

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

package main

import (
	"fmt"
	"os"

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

func exitErrorf(msg string, args ...interface{}) {
	fmt.Fprintf(os.Stderr, msg+"\n", args...)
	os.Exit(1)
}

func main() {
	sess := session.Must(session.NewSession())
	svc := s3.New(
		sess,
		aws.NewConfig().WithRegion("ap-northeast-1"),
	)

	params := &s3.SelectObjectContentInput{
		Bucket:         aws.String("s3-select-sampleyy"),
		Key:            aws.String("sample.jsonl"),
		ExpressionType: aws.String(s3.ExpressionTypeSql),
		Expression:     aws.String("SELECT * FROM S3Object LIMIT 10"),
		InputSerialization: &s3.InputSerialization{
			CompressionType: aws.String("NONE"),
			JSON: &s3.JSONInput{
				Type: aws.String("Lines"),
			},
		},
		OutputSerialization: &s3.OutputSerialization{
			JSON: &s3.JSONOutput{},
		},
		//// You can also output with csv.
		//OutputSerialization: &s3.OutputSerialization{
		//	CSV: &s3.CSVOutput{
		//		//RecordDelimiter: aws.String("\n"),
		//		//FieldDelimiter:  aws.String(","),
		//	},
		//},
	}

	resp, err := svc.SelectObjectContent(params)
	if err != nil {
		exitErrorf("failed making API request", err)
	}
	defer resp.EventStream.Close()

	for event := range resp.EventStream.Events() {
		switch v := event.(type) {
		case *s3.RecordsEvent:
			fmt.Println(string(v.Payload))
		case *s3.StatsEvent:
			fmt.Println("Processed", *v.Details.BytesProcessed, "bytes")
			fmt.Println("Returned", *v.Details.BytesReturned, "bytes")
			fmt.Println("Scanned", *v.Details.BytesScanned, "bytes")
		case *s3.EndEvent:
			fmt.Println("SelectObjectContent completed")
		}
	}

	if err := resp.EventStream.Err(); err != nil {
		exitErrorf("failed to read from SelectObject EventStream, %v", err)
	}
}

以上是关于golang Amazon S3使用AWS SDK for Go选择CSV示例的主要内容,如果未能解决你的问题,请参考以下文章

Amazon S3 设置元数据使用 AWS SDK PHP v2 upload() 失败

golang aws-sdk-go 之 s3 服务

在iOS Swift中不使用AWS SDK在Amazon S3存储桶中上传的背景图像(背景模式)

使用 AWS SDK for PHP 上传的 Amazon S3 文件总是“application/octet-stream”?

如何使用 Java SDK 将目录与 AWS S3 同步?

如何使用 amazon sdk 为虚域生成预签名的 Amazon S3 url?