英文:
Cannot Pass type s3.Bucket into a Golang function
问题
我正在尝试调整我S3存储桶中的所有图片的大小,但是当我将存储桶传递给resize_images函数时,出现了一个错误。为了这个例子,我限制了我获取的图片数量为5(假设我需要保持这个结构)。
这是我一直遇到的错误:
./mass_resize.go:92: cannot use mybucket (type *s3.Bucket) as type s3.Bucket in function argument
这是代码:
package main
import (
"fmt"
"launchpad.net/goamz/aws"
"launchpad.net/goamz/s3"
"log"
"image"
"bytes"
"github.com/nfnt/resize"
"image/jpeg"
// "reflect"
)
func resize_images(image_keys []s3.Key, mybucket s3.Bucket) {
for _, v := range image_keys {
// 检查这个图片是否已经是小尺寸版本
image_data, err := mybucket.Get(v.Key)
if err != nil {
panic(err.Error())
} else {
fmt.Println("success")
}
image_bytes := []byte(image_data)
original_image, _, err := image.Decode(bytes.NewReader(image_bytes))
if err != nil {
fmt.Println("Error occurred after image.Decode function")
panic(err.Error())
} else {
fmt.Println("Another success")
}
new_image := resize.Resize(160, 0, original_image, resize.Lanczos3)
if new_image != nil {
fmt.Println("Image has been resized");
}
buf := new(bytes.Buffer)
err = jpeg.Encode(buf, new_image, nil)
if err != nil {
fmt.Println("Error occurred while encoding the new_image into a buffer")
fmt.Println(err)
}
send_S3 := buf.Bytes()
new_path := v.Key + "_sm"
PublicRead := s3.ACL("public-read")
err = mybucket.Put(new_path, send_S3, "image/jpg", PublicRead)
if err != nil {
fmt.Println("-----------------------------------------------")
fmt.Println("Error occurred in the mybucket.Put function")
fmt.Println(err)
fmt.Println(v.Key)
fmt.Println("-----------------------------------------------")
} else {
fmt.Println("Upload was successful")
}
}
}
func main() {
// 连接到S3
auth := aws.Auth{
AccessKey: "key",
SecretKey: "secret",
}
useast := aws.USEast
connection := s3.New(auth, useast)
mybucket := connection.Bucket("bucket")
// 获取前5个图片的键
res, err := mybucket.List("", "", "", 5)
if err != nil {
log.Fatal(err)
}
resize_images(res.Contents, mybucket)
}
你有什么想法这个错误是什么意思吗?我还在学习Go语言,我不明白为什么我不能将s3存储桶传递给我的函数。非常感谢解释!
英文:
I'm trying to resize all of the pictures in my S3 bucket, but I am getting an error when I pass bucket into my resize_images function. For the sake of the this example, I've limited the pictures I pull to 5 (let's assume I need to maintain this structure).
This is the error I have have been getting:
> ./mass_resize.go:92: cannot use mybucket (type *s3.Bucket) as type s3.Bucket in function argument
This is the code:
package main
import (
"fmt"
"launchpad.net/goamz/aws"
"launchpad.net/goamz/s3"
"log"
"image"
"bytes"
"github.com/nfnt/resize"
"image/jpeg"
// "reflect"
)
func resize_images(image_keys []s3.Key, mybucket s3.Bucket) {
for _, v := range image_keys {
// check to see if this image is already in small version
image_data, err := mybucket.Get(v.Key)
if err != nil {
panic(err.Error())
} else {
fmt.Println("success")
}
image_bytes := []byte(image_data)
original_image, _, err := image.Decode(bytes.NewReader(image_bytes))
if err != nil {
fmt.Println("Error occurred after image.Decode function")
panic(err.Error())
} else {
fmt.Println("Another success")
}
new_image := resize.Resize(160, 0, original_image, resize.Lanczos3)
if new_image != nil {
fmt.Println("Image has been resized");
}
buf := new(bytes.Buffer)
err = jpeg.Encode(buf, new_image, nil)
if err != nil {
fmt.Println("Error occurred while encoding the new_image into a buffer")
fmt.Println(err)
}
send_S3 := buf.Bytes()
new_path := v.Key + "_sm"
PublicRead := s3.ACL("public-read")
err = mybucket.Put(new_path, send_S3, "image/jpg", PublicRead)
if err != nil {
fmt.Println("-----------------------------------------------")
fmt.Println("Error occurred in the mybucket.Put function")
fmt.Println(err)
fmt.Println(v.Key)
fmt.Println("-----------------------------------------------")
} else {
fmt.Println("Upload was successful")
}
}
}
func main() {
// connect to S3
auth := aws.Auth{
AccessKey: "key",
SecretKey: "secret",
}
useast := aws.USEast
connection := s3.New(auth, useast)
mybucket := connection.Bucket("bucket")
// pull the first 5 picture keys
res, err := mybucket.List("", "", "", 5)
if err != nil {
log.Fatal(err)
}
resize_images(res.Contents, mybucket)
}
Any ideas what this error means? Still learning Go, and I don't understand why I can't pass the s3 bucket into my function. An explanation would be very appreciated!
答案1
得分: 5
错误意味着你的变量 mybucket
的类型是 "指向 s3.Bucket 的指针":*s3.Bucket
,但函数期望的是一个非指针变量。
你可以尝试通过解引用指针来调用函数:
resize_images(res.Contents, *mybucket)
或者你可以尝试更改函数的签名以接收一个指针:
func resize_images(image_keys []s3.Key, mybucket *s3.Bucket) {
英文:
The error means your variable mybucket
is of type "pointer to s3.Bucket": *s3.Bucket
but the function expects a non-pointer variable.
You can try calling the function deferencing the pointer:
resize_images(res.Contents, *mybucket)
Or you can try changing the signature of the function to receive a pointer:
func resize_images(image_keys []s3.Key, mybucket *s3.Bucket) {
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论