我在golang中有以下代码,用于读取文件并提取文件的一些列,fileName是从我的lambda接收的,但在我的第一步,即从s3获取文件时,我遇到了一个问题。
我的代码是:
package main
import (
"fmt"
"packageXXX/cmd/utils"
"github.com/aws/aws-lambda-go/lambda"
"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"
"go.uber.org/zap"
)
var (
log *zap.Logger
)
func init() {
log = utils.InitLogger()
}
func DownloadFile(downloader *s3manager.Downloader, bucketName string, key string) error {
buff := &aws.WriteAtBuffer{}
numBytes, err := downloader.Download(
buff,
&s3.GetObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(key),
},
)
data := buff.Bytes() // now data is my []byte array
log.Info("nnnnnnnnnnnnnnn", zap.Any("nnn", numBytes))
log.Info("xxxxxxxxxxxxxxx", zap.Any("bytes", data))
return err
}
func handler(fileName string) (string, error) {
log.Info(fmt.Sprintf("received: %s", fileName))
sess, err := session.NewSessionWithOptions(session.Options{
Profile: "default",
Config: aws.Config{
Region: aws.String("us-west-2"),
},
})
if err != nil {
log.Info("errorrrrrrrrr", zap.Any("session", err))
return "errorr,", nil
} else {
log.Info("sessionnnnnnnnnnnnnn", zap.Any("sess", sess))
}
bucketName := "s3-test-lambda-go-xxx"
downloader := s3manager.NewDownloader(sess)
log.Info("downloader", zap.Any("downloader", err))
key := fileName
err = DownloadFile(downloader, bucketName, key)
if err != nil {
fmt.Printf("Couldn't download file: %v", err)
return "errror", nil
}
fmt.Println("Successfully downloaded file")
return fmt.Sprintf("el file recibido es: %s", fileName), nil
}
func main() {
lambda.Start(handler)
}
正在打印的日志为:
For verbose messaging see aws.Config.CredentialsChainVerboseErrorsSTART RequestId: dba8ebd5-9102-4a2f-8e45-cdef355f1bbd Version: $LATEST
{"level":"info","ts":"2022-11-02T17:31:32Z","msg":"received: gggggg2.txt"}
{"level":"info","ts":"2022-11-02T17:31:32Z","msg":"sessionnnnnnnnnnnnnn","sessError":"json: unsupported type: endpoints.endpointDefaults"}
{"level":"info","ts":"2022-11-02T17:31:32Z","msg":"downloader","downloader":null}
{"level":"info","ts":"2022-11-02T17:31:38Z","msg":"nnnnnnnnnnnnnnn","nnn":0}
{"level":"info","ts":"2022-11-02T17:31:38Z","msg":"xxxxxxxxxxxxxxx","bytes":""}
Couldn't download file: NoCredentialProviders: no valid providers in chain. Deprecated.
END RequestId: dba8ebd5-9102-4a2f-8e45-cdef355f1bbd
问题是会话
那么是否可以创建与角色的会话,因为像是生产我不想使用访问和密钥?谢谢
1条答案
按热度按时间5vf7fwbs1#
会话将加载您的默认会话,如果您这样操作它
而不是:
这意味着它将使用您配置的任何会话,而不取决于您是在lambda内部还是在本地。这也意味着您可以承担任何您想承担的角色,甚至使用SSO登录(因此不需要访问密钥和密码)。