无法将类型s3.Bucket传递给Golang函数

时间:2014-04-17 16:04:35

标签: go

我试图调整S3存储桶中的所有图片,但是当我将存储桶传入resize_images函数时出现错误。为了这个例子,我将我拉到的图片限制为5(假设我需要保持这种结构)。

这是我一直在犯的错误:

  

./ mass_resize.go:92:不能在函数参数中使用mybucket(类型* s3.Bucket)作为s3.Bucket类型

这是代码:

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)
}

这个错误意味着什么?还在学习Go,我不明白为什么我不能将s3桶传递给我的函数。非常感谢您的解释!

1 个答案:

答案 0 :(得分:5)

错误意味着您的变量mybucket的类型为“指向s3.Bucket的指针”:*s3.Bucket但该函数需要非指针变量。

您可以尝试调用引用指针的函数:

resize_images(res.Contents, *mybucket)

或者您可以尝试更改函数的签名以接收指针:

func resize_images(image_keys []s3.Key, mybucket *s3.Bucket) {