Example #1
0
func main() {
	var (
		err  error
		conf conf
	)
	// 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 EC2 api
	creds := aws.Creds(conf.Credentials.AccessKey, conf.Credentials.SecretKey, "")
	cli := ec2.New(creds, "us-east-1", nil)

	fireq := ec2.Filter{
		Name:   aws.String("private-ip-address"),
		Values: []string{"172.30.200.13"},
	}
	direq := ec2.DescribeInstancesRequest{
		Filters: []ec2.Filter{fireq},
	}
	resp, err := cli.DescribeInstances(&direq)
	if err != nil {
		panic(err)
	}
	for _, reservation := range resp.Reservations {
		for _, instance := range reservation.Instances {
			fmt.Printf("%s\t%s", *instance.InstanceID, *instance.PrivateIPAddress)
			for _, tag := range instance.Tags {
				fmt.Printf("\t%s:%s", *tag.Key, *tag.Value)
			}
			fmt.Printf("\n")
		}
	}
}
Example #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)
	//	}
	//}
}
Example #3
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)
}