dowm41315 2014-04-17 16:04
浏览 77
已采纳

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

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条回答 默认 最新

  • duandu8707 2014-04-17 16:20
    关注

    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) {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能