英文:
GO AWS SDK s3.GetObjectInput is not a type
问题
我正在尝试使用Go SDK for AWS来下载S3中的文件,类似于AWS提供的示例中所做的操作:https://docs.aws.amazon.com/sdk-for-go/api/service/s3/
当我尝试构建项目时,我遇到了以下错误:
"github.com/aws/aws-sdk-go/service/s3"已导入但未使用
和
s3.GetObjectInput不是一个类型
我已经在使用的包上运行了go get命令,并使用go mod tidy命令整理了我的go.mod文件。它似乎对s3manager行和其他aws包很满意,只是在s3引用方面出现了问题。
我是否遗漏了什么简单的东西?
英文:
Im trying to use the Go sdk for aws to download a file from s3 doing something similar to what has been done in the examples provided by aws: https://docs.aws.amazon.com/sdk-for-go/api/service/s3/
package main
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/davidbyttow/govips/v2/vips"
)
func handler(ctx context.Context, s3Event events.S3Event) {
for _, record := range s3Event.Records {
s3 := record.S3
sess, err := session.NewSession(&aws.Config{
Region: aws.String("<REGION>"),
})
if err != nil {
exitErrorf("cannot create s3 session, %v", err)
}
downloader := s3manager.NewDownloader(sess)
file, err := os.Create(filepath.Base(s3.Object.Key))
if err != nil {
exitErrorf("DownloadFile:::Unable to open file, %v", err)
}
defer file.Close()
numBytes, err := downloader.Download(file,
&s3.GetObjectInput{
Bucket: aws.String("<BUCKET>"),
Key: aws.String(s3.Object.Key),
})
if err != nil {
exitErrorf("Unable to download item %q, %v", s3.Object.Key, err)
}
fmt.Println("Downloaded", file.Name(), numBytes, "bytes")
}
When I try to build the project I get the errors:
"github.com/aws/aws-sdk-go/service/s3" imported and not used
and
s3.GetObjectInput is not a type
I have ran go get on the packages used and go mod tidy to sort out my go.mod file. It seems perfectly happy with the s3manager lines and the other aws packages and it is just complaining about the s3 references.
Is there something simple I am missing?
答案1
得分: 1
导入包github.com/aws/aws-sdk-go/service/s3
的包名是s3
,但被s3 := record.S3
所隐藏。这就是你看到错误的原因。
推荐的解决方案是将s3 := record.S3
修改为选择另一个标识符。例如,s3Entity := record.S3
。不要忘记在源代码中将s3.Object.Key
替换为s3Entity.Object.Key
。
另一种选择是给导入的包指定另一个名称。例如:
import awss3 "github.com/aws/aws-sdk-go/service/s3"
参考自规范的“声明和作用域”部分:
声明将非空白标识符绑定到常量、类型、类型参数、变量、函数、标签或包。...
Go使用块进行词法作用域:
- 预声明标识符的作用域是全局块。
- 标识符表示常量、类型、变量或函数(但不是方法)的作用域(在任何函数之外的顶级)是包块。
- 导入包的包名的作用域是包含导入声明的文件块。
- 表示方法接收器、函数参数或结果变量的标识符的作用域是函数体。
- 表示函数类型参数或由方法接收器声明的标识符的作用域从函数名开始,到函数体结束。
- 表示类型类型参数的标识符的作用域从类型名开始,到TypeSpec结束。
- 在函数内部声明的常量或变量标识符的作用域从ConstSpec或VarSpec的结束处开始,到最内层包含块的结束处结束。
- 在函数内部声明的类型标识符的作用域从TypeSpec中的标识符开始,到最内层包含块的结束处结束。
在块中声明的标识符可以在内部块中重新声明。在内部声明的标识符在作用域内时,表示内部声明所声明的实体。
另请参阅导入声明。
英文:
The package name of the imported package github.com/aws/aws-sdk-go/service/s3
is s3
, which is hidden by s3 := record.S3
. That's why you see the error.
The recommended solution is to modify s3 := record.S3
to choose another identity. For example, s3Entity := record.S3
. Don't forget to replace s3.Object.Key
with s3Entity.Object.Key
in the source code.
Another option is to give another name to the imported package. For example:
import awss3 "github.com/aws/aws-sdk-go/service/s3"
Reference from the "Declarations and scope" section of the spec:
> A declaration binds a non-blank identifier to a constant, type, type parameter, variable, function, label, or package. ...
>
> Go is lexically scoped using blocks:
>
> - The scope of a predeclared identifier is the universe block.
> - The scope of an identifier denoting a constant, type, variable, or function (but not method) declared at top level (outside any function) is the package block.
> - The scope of the package name of an imported package is the file block of the file containing the import declaration.
> - The scope of an identifier denoting a method receiver, function parameter, or result variable is the function body.
> - The scope of an identifier denoting a type parameter of a function or declared by a method receiver begins after the name of the function and ends at the end of the function body.
> - The scope of an identifier denoting a type parameter of a type begins after the name of the type and ends at the end of the TypeSpec.
> - The scope of a constant or variable identifier declared inside a function begins at the end of the ConstSpec or VarSpec (ShortVarDecl for short variable declarations) and ends at the end of the innermost containing block.
> - The scope of a type identifier declared inside a function begins at the identifier in the TypeSpec and ends at the end of the innermost containing block.
>
> An identifier declared in a block may be redeclared in an inner block. While the identifier of the inner declaration is in scope, it denotes the entity declared by the inner declaration.
See also Import declarations.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论