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

报告相同问题?

悬赏问题

  • ¥15 CATIA有些零件打开直接单机确定终止
  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址