dr897777 2018-02-20 21:22
浏览 29

即使存在也缺少密钥

I'm using the following code to create a presigned url:

package main

import (
    "fmt"
    "log"
    "time"

    "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/kelseyhightower/envconfig"
)

func main() {
    configuration := s3.PutObjectInput{ Key: aws.String("default_key") }

    // Add Bucket to configuration from environment variable
    // e.g:
    //    CONFIGURATION_BUCKET -> configuration.Bucket
    err := envconfig.Process("configuration", &configuration)

    if err != nil {
        log.Fatal(err.Error())
    }

    sess := session.Must(session.NewSessionWithOptions(session.Options{
      SharedConfigState: session.SharedConfigEnable,
    }))
    svc := s3.New(sess)
    req, _ := svc.PutObjectRequest(&configuration)

    url, err := req.Presign(15 * time.Minute)

    if err != nil {
        log.Fatal(err.Error())
    }
    fmt.Println("Url is", url)
}

The key is always the same, but the bucket can change depending on the environment that I'm.

When I print with fmt.Println the Bucket and Key exists:

fmt.Println(configuration.Bucket, configuration.Key) // returns address

But the following error happens: required key CONFIGURATION_KEY missing value

Thank you!

  • 写回答

1条回答 默认 最新

  • dpwtr666638 2018-02-20 21:50
    关注

    If you look at the type s3.PutObjectInput, the key field has a required:"true" tag.

    The required tag is also used by the envconfig package to indicate that the environment variable is required.

    You either need to supply the key from the environment, or not use the aws structs directly with the envconfig package.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用