Exemplo n.º 1
0
func credentials() *s3.S3 {
	// 指定なしで、~/.aws/credentialsのdefaultになる
	creds, err := aws.ProfileCreds("", "", 0)
	if err != nil {
		log.SetFlags(log.Llongfile)
		log.Fatalln(err)
	}

	return s3.New(creds, "ap-northeast-1", nil)
}
Exemplo n.º 2
0
func main() {
	var (
		err         error
		conf        conf
		bucket      string = "testawsgo" // change to your convenience
		fd          *os.File
		contenttype string = "binary/octet-stream"
	)
	// obtain credentials from ~/.awsgo
	credfile := os.Getenv("HOME") + "/.awsgo"
	_, err = os.Stat(credfile)
	if err != nil {
		fmt.Println("Error: missing credentials file in ~/.awsgo")
		os.Exit(1)
	}
	err = gcfg.ReadFileInto(&conf, credfile)
	if err != nil {
		panic(err)
	}

	// create a new client to S3 api
	creds := aws.Creds(conf.Credentials.AccessKey, conf.Credentials.SecretKey, "")
	cli := s3.New(creds, "us-east-1", nil)

	// open the file to upload
	if len(os.Args) != 2 {
		fmt.Printf("Usage: %s <inputfile>\n", os.Args[0])
		os.Exit(1)
	}
	fi, err := os.Stat(os.Args[1])
	if err != nil {
		fmt.Printf("Error: no input file found in '%s'\n", os.Args[1])
		os.Exit(1)
	}
	fd, err = os.Open(os.Args[1])
	if err != nil {
		panic(err)
	}
	defer fd.Close()

	// create a bucket upload request and send
	objectreq := s3.PutObjectRequest{
		ACL:           aws.String("public-read"),
		Bucket:        aws.String(bucket),
		Body:          fd,
		ContentLength: aws.Integer(int(fi.Size())),
		ContentType:   aws.String(contenttype),
		Key:           aws.String(fi.Name()),
	}
	_, err = cli.PutObject(&objectreq)
	if err != nil {
		fmt.Printf("Error: %v\n", err)
	} else {
		fmt.Printf("%s\n", "https://s3.amazonaws.com/"+bucket+"/"+fi.Name())
	}

	// list the content of the bucket
	//listreq := s3.ListObjectsRequest{
	//	Bucket: aws.StringValue(&bucket),
	//}
	//listresp, err := cli.ListObjects(&listreq)
	//if err != nil {
	//	panic(err)
	//}
	//if err != nil {
	//	fmt.Printf("Error: %v\n", err)
	//} else {
	//	fmt.Printf("Content of bucket '%s': %d files\n", bucket, len(listresp.Contents))
	//	for _, obj := range listresp.Contents {
	//		fmt.Println("-", *obj.Key)
	//	}
	//}
}
Exemplo n.º 3
0
func iamCreds() *s3.S3 {
	return s3.New(aws.IAMCreds(), "ap-northeast-1", nil)
}
Exemplo n.º 4
0
func accessKeyCreds() *s3.S3 {
	accessKey := os.Getenv("AWS_ACCESS_KEY")
	secretKey := os.Getenv("AWS_SECRET_KEY")
	creds := aws.Creds(accessKey, secretKey, "")
	return s3.New(creds, "ap-northeast-1", nil)
}