Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
package utility
import (
"bufio"
"github.com/revel/revel"
"launchpad.net/goamz/aws"
"launchpad.net/goamz/s3"
"net/http"
"os"
)
/**
* S3へアップロード
*/
func S3Upload(fileToBeUploaded, targetPath string) (isSuccess bool) {
accesskey := revel.Config.StringDefault("aws.accesskey", "")
secretkey := revel.Config.StringDefault("aws.secretkey", "")
bucketName := revel.Config.StringDefault("aws.bucket", "")
AWSAuth := aws.Auth{
AccessKey: accesskey, // change this to yours
SecretKey: secretkey,
}
region := aws.USEast
connection := s3.New(AWSAuth, region)
bucket := connection.Bucket(bucketName)
file, err := os.Open(fileToBeUploaded)
if err != nil {
revel.INFO.Println(err)
return false
}
defer file.Close()
fileInfo, _ := file.Stat()
var size int64 = fileInfo.Size()
bytes := make([]byte, size)
buffer := bufio.NewReader(file)
_, err = buffer.Read(bytes)
filetype := http.DetectContentType(bytes)
err = bucket.Put(targetPath, bytes, filetype, s3.ACL("public-read-write"))
if err != nil {
revel.INFO.Println(err)
return false
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment