s3 golang:将凭据作为字符串提供

huangapple go评论81阅读模式
英文:

s3 golang: Credentials provided as string

问题

你好!

在我的项目中,我需要上传和下载到不同的存储桶以检索和上传文件。凭据是由数据库提供的。
然而,golang aws sdk(v2)似乎不支持在运行时使用访问密钥和访问ID。它似乎要求凭据存储在主目录的.aws目录中。

有没有可能的解决方案?

英文:

Good day!

In my project, I need to upload and download to various buckets to retrieve and upload files. Credentials are provided by database.
However, golang aws sdk (v2) seems not support access key and access id during runtime. It seems to require credentials to be stored in home .aws directory.

Is there any possible solutions for that?

答案1

得分: 1

credentials package提供了多种来源,包括直接将密钥作为字符串传递:

import (
    "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"
)

awsSession, err := session.NewSessionWithOptions(session.Options{
    // ...
    Config: aws.Config{
        // ...
        Credentials: credentials.NewStaticCredentials(
            "my-access-key",
            "my-secret-key",
            "",
        ),
    },
})
if err != nil {
    // ...
}

s3Client := s3.New(awsSession)
英文:

The credentials package offers a variety of sources, including passing keys as strings directly:

import (
    "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"
)

awsSession, err := session.NewSessionWithOptions(session.Options{
    // ...
    Config: aws.Config{
        // ...
        Credentials: credentials.NewStaticCredentials(
            "my-access-key",
            "my-secret-key",
            "",
        ),
    },
})
if err != nil {
    // ...
}

s3Client := s3.New(awsSession)

huangapple
  • 本文由 发表于 2022年6月13日 16:49:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/72600060.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定