Amazon S3 api

https://raw.githubusercontent.com/chineking/CloudBackup/master/CloudBackup/lib/s3.py

see:

http://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html

http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUT.html

http://docs.aws.amazon.com/zh_cn/AmazonS3/latest/dev/VirtualHosting.html

http://docs.aws.amazon.com/zh_cn/AmazonCloudFront/latest/DeveloperGuide/private-content-creating-signed-url-canned-policy.html


下面是例子, 上傳圖片,設置private的acl,gzip壓縮!


type MyBucket struct {
	*s3.Bucket
}

func initMyBucket(access_key, secret_key, bucket_name string) *MyBucket {
	auth, err := aws.GetAuth(access_key, secret_key)
	if err != nil {
		Log4e("aws.GetAuth", err)
		return nil
	}
	s := s3.New(auth, aws.USEast)

	bucket := s.Bucket(bucket_name)

	return &MyBucket{bucket}
}

func (b *MyBucket) Upload(path string, content_type string, data []byte) error {
	var header = make(map[string][]string)
	if content_type== TYPE_IMAGE_JPG {
		header["Content-Type"] = []string{"image/gif"}
	} else if content_type== TYPE_IMAGE_GIF {
		header["Content-Type"] = []string{"image/jpeg"}
	} else if content_type== TYPE_IMAGE_PNG {
		header["Content-Type"] = []string{"image/png"}
	}else{
		Log4e("MyBucket.Upload, content_type error", content_type)
	}
	header["Content-Encoding"] = []string{"gzip"}
	header["Cache-Control"] = []string{"max-age=259200, public"}
	//err = bucket.Put("test.txt", data, "text/plain", s3.Private)
	err := b.PutHeader("test.txt", b.Gzip(data), header, s3.Private)
	if err != nil {
		Log4e("bucket.Put", err.Error())
	}

	return err
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章