Hello first of i am new to Golang and am trying to upload a photo to my s3 Bucket however I am getting an error every time I try, the error I get is this
err opening file: open 55fc14631c00004800082775.jpeg: no such file or directory2016/11/30 19:28:10 http: panic serving [::1]:58502: runtime error: invalid memory address or nil pointer dereference goroutine 5 [running]
My buckets permission are set to read and write for everyone so it must be something in this code is that wrong
and I am following this tutorial https://medium.com/@questhenkart/s3-image-uploads-via-aws-sdk-with-golang-63422857c548#.ahda1pgly and I am trying to upload a public image found here http://img.huffingtonpost.com/asset/,scalefit_950_800_noupscale/55fc14631c00004800082775.jpeg
my current code is this below
func UploadProfile(w http.ResponseWriter) {
creds := credentials.NewStaticCredentials(aws_access_key_id, aws_secret_access_key,token)
_, err := creds.Get()
if err != nil {
fmt.Printf("bad credentials: %s", err)
}
config := &aws.Config{
Region :aws.String("us-west-1"),
Endpoint :aws.String("s3.amazonaws.com"),
S3ForcePathStyle:aws.Bool(true),
Credentials :creds,
}
svc := s3.New(session.New(), config)
file, err := os.Open("55fc14631c00004800082775.jpeg")
if err != nil {
fmt.Printf("err opening file: %s", err)
}
defer file.Close()
fileInfo, _ := file.Stat()
var size int64 = fileInfo.Size()
buffer := make([]byte, size)
file.Read(buffer)
fileBytes := bytes.NewReader(buffer)
fileType := http.DetectContentType(buffer)
path := "http://img.huffingtonpost.com/asset/,scalefit_950_800_noupscale/" + file.Name()
params := &s3.PutObjectInput{
Bucket: aws.String("MyBucket"),
Key: aws.String(path),
Body: fileBytes,
ContentLength: aws.Int64(size),
ContentType: aws.String(fileType),
}
resp, err := svc.PutObject(params)
if err != nil {
fmt.Printf("bad response: %s", err)
}
fmt.Printf("response %s", awsutil.StringValue(resp))
fmt.Fprintf(w,"done")
}
I do not know why this is not working as my Credentials and buckets are correct . In addition the IMG address works for the image above so do not know why the file is nil
<img height="200" width="200" src="http://img.huffingtonpost.com/asset/,scalefit_950_800_noupscale/55fc14631c00004800082775.jpeg"/>
@user1591668,
Your error is "err opening file: open 55fc14631c00004800082775.jpeg: no such file or directory". Sounds like not related to AWS's SDK.
Check that your binary actually starts in the same directory where your image file is ("working directory") or, to test this assumption, you can try with an absolute path to the image.
Cheers, Dennis