Example #1
0
func getObject(client *s3.S3) {
	req := s3.GetObjectRequest{}
	req.Bucket = aws.String("kyokomi-foo")
	req.Key = aws.String("bar/media.json")

	res, err := client.GetObject(&req)
	if err != nil {
		log.Fatalln(err)
	}

	defer res.Body.Close()
}
Example #2
0
func printGetObject(client *s3.S3) {
	req := s3.GetObjectRequest{}
	req.Bucket = aws.String("kyokomi-foo")
	req.Key = aws.String("bar/media.json")

	res, err := client.GetObject(&req)
	if err != nil {
		log.Fatalln(err)
	}

	defer res.Body.Close()

	data, err := ioutil.ReadAll(res.Body)
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Println(string(data))
}
Example #3
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 #4
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)
	//	}
	//}
}