官网API地址:https://docs.aws.amazon.com/sdk-for-go/api/service/s3/
初始化session和连接,同时打印错误日志
var (
sess *session.Session
svc *s3.S3
)
func init() {
access_key := "xxxxxx"
secret_key := "xxxxxxxxxxx"
end_point := "http://xxx.xxx.xxx.xxx:xxx/" //根据自己endpint修改
sess, _ = session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentials(access_key, secret_key, ""),
Endpoint: aws.String(end_point),
Region: aws.String("us-east-1"),
DisableSSL: aws.Bool(true),
S3ForcePathStyle: aws.Bool(false), //virtual-host style方式,不要修改
})
svc = s3.New(sess)
}
func main(){
....
}
func exitErrorf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(1)
}
//获取所有桶
func ListBuckets() {
result, err := svc.ListBuckets(nil)
if err != nil {
exitErrorf("Unable to list buckets, %v", err)
}
fmt.Println("Buckets:")
for _, b := range result.Buckets {
fmt.Printf("* %s created on %s\n",
aws.StringValue(b.Name), aws.TimeValue(b.CreationDate))
}
for _, b := range result.Buckets {
fmt.Printf("%s\n", aws.StringValue(b.Name))
}
}
func createBucket(bucket string) {
params := &s3.CreateBucketInput{
Bucket: aws.String(bucket),
}
_, err := svc.CreateBucket(params)
if err != nil {
exitErrorf("Unable to create bucket %q, %v", bucket, err)
}
// Wait until bucket is created before finishing
fmt.Printf("Waiting for bucket %q to be created...\n", bucket)
err = svc.WaitUntilBucketExists(&s3.HeadBucketInput{
Bucket: aws.String(bucket),
})
if err != nil {
exitErrorf("Error occurred while waiting for bucket to be created, %v", bucket)
}
fmt.Printf("Bucket %q successfully created\n", bucket)
}
func deleteBucket(bucket string) {
params := &s3.DeleteBucketInput{
Bucket: aws.String(bucket),
}
_, err := svc.DeleteBucket(params)
if err != nil {
exitErrorf("Unable to delete bucket %q, %v", bucket, err)
}
//wait until bucket is deleted
err = svc.WaitUntilBucketNotExists(&s3.HeadBucketInput{
Bucket: aws.String(bucket),
})
if err != nil {
exitErrorf("Error occurred while waiting for bucket to be deleted, %v", bucket)
}
fmt.Printf("Bucket %q successfully delete\n", bucket)
}
func downloadFile(bucket, item string) {
file, err := os.Create(item)
if err != nil {
exitErrorf("Unable to open file %q, %v", err)
}
defer file.Close()
downloader := s3manager.NewDownloader(sess)
numBytes, err := downloader.Download(file,
&s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(item),
})
if err != nil {
exitErrorf("Unable to download item %q, %v", item, err)
}
fmt.Println("Downloaded", file.Name(), numBytes, "bytes")
}
func deleteFile(bucket, obj string) {
_, err := svc.DeleteObject(&s3.DeleteObjectInput{Bucket: aws.String(bucket), Key: aws.String(obj)})
if err != nil {
exitErrorf("Unable to delete object %q from bucket %q, %v", obj, bucket, err)
}
err = svc.WaitUntilObjectNotExists(&s3.HeadObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(obj),
})
fmt.Printf("Object %q successfully deleted\n", obj)
}
func uploadFile(bucket, filename string) {
file, err := os.Open(filename)
if err != nil {
exitErrorf("Unable to open file %q, %v", err)
}
defer file.Close()
uploader := s3manager.NewUploader(sess)
_, err = uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(bucket),
Key: aws.String(filename),
Body: file,
})
if err != nil {
// Print the error and exit.
exitErrorf("Unable to upload %q to %q, %v", filename, bucket, err)
}
fmt.Printf("Successfully uploaded %q to %q\n", filename, bucket)
}
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
_ "github.com/aws/aws-sdk-go/service/s3/s3manager"
"os"
)
var (
sess *session.Session
svc *s3.S3
)
func init() {
access_key := "xxxxxx"
secret_key := "xxxxxxxxxxx"
end_point := "http://xxx.xxx.xxx.xxx:xxx/" //根据自己endpint修改
sess, _ = session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentials(access_key, secret_key, ""),
Endpoint: aws.String(end_point),
Region: aws.String("us-east-1"),
DisableSSL: aws.Bool(true),
S3ForcePathStyle: aws.Bool(false), //virtual-host style方式,不要修改
})
svc = s3.New(sess)
}
func main() {
//注意!!!桶名称后面一定要根`/`
//downloadFile("bucket-ziyi/", "banner.png")
//uploadFile("bucket-ziyi/", "test/test1/test2/test.csv")
//createBuckt("bucket-demo1/")
//deleteBucket("bucket-demo1/")
deleteFile("bucket-ziyi/test/", "log.txt")
}
func deleteFile(bucket, obj string) {
_, err := svc.DeleteObject(&s3.DeleteObjectInput{Bucket: aws.String(bucket), Key: aws.String(obj)})
if err != nil {
exitErrorf("Unable to delete object %q from bucket %q, %v", obj, bucket, err)
}
err = svc.WaitUntilObjectNotExists(&s3.HeadObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(obj),
})
fmt.Printf("Object %q successfully deleted\n", obj)
}
func deleteBucket(bucket string) {
params := &s3.DeleteBucketInput{
Bucket: aws.String(bucket),
}
_, err := svc.DeleteBucket(params)
if err != nil {
exitErrorf("Unable to delete bucket %q, %v", bucket, err)
}
//wait until bucket is deleted
err = svc.WaitUntilBucketNotExists(&s3.HeadBucketInput{
Bucket: aws.String(bucket),
})
if err != nil {
exitErrorf("Error occurred while waiting for bucket to be deleted, %v", bucket)
}
fmt.Printf("Bucket %q successfully delete\n", bucket)
}
func createBucket(bucket string) {
params := &s3.CreateBucketInput{
Bucket: aws.String(bucket),
}
_, err := svc.CreateBucket(params)
if err != nil {
exitErrorf("Unable to create bucket %q, %v", bucket, err)
}
// Wait until bucket is created before finishing
fmt.Printf("Waiting for bucket %q to be created...\n", bucket)
err = svc.WaitUntilBucketExists(&s3.HeadBucketInput{
Bucket: aws.String(bucket),
})
if err != nil {
exitErrorf("Error occurred while waiting for bucket to be created, %v", bucket)
}
fmt.Printf("Bucket %q successfully created\n", bucket)
}
func uploadFile(bucket, filename string) {
file, err := os.Open(filename)
if err != nil {
exitErrorf("Unable to open file %q, %v", err)
}
defer file.Close()
uploader := s3manager.NewUploader(sess)
_, err = uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(bucket),
Key: aws.String(filename),
Body: file,
})
if err != nil {
// Print the error and exit.
exitErrorf("Unable to upload %q to %q, %v", filename, bucket, err)
}
fmt.Printf("Successfully uploaded %q to %q\n", filename, bucket)
}
func downloadFile(bucket, item string) {
file, err := os.Create(item)
if err != nil {
exitErrorf("Unable to open file %q, %v", err)
}
defer file.Close()
downloader := s3manager.NewDownloader(sess)
numBytes, err := downloader.Download(file,
&s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(item),
})
if err != nil {
exitErrorf("Unable to download item %q, %v", item, err)
}
fmt.Println("Downloaded", file.Name(), numBytes, "bytes")
}
//获取所有桶
func ListBuckets() {
result, err := svc.ListBuckets(nil)
if err != nil {
exitErrorf("Unable to list buckets, %v", err)
}
fmt.Println("Buckets:")
for _, b := range result.Buckets {
fmt.Printf("* %s created on %s\n",
aws.StringValue(b.Name), aws.TimeValue(b.CreationDate))
}
for _, b := range result.Buckets {
fmt.Printf("%s\n", aws.StringValue(b.Name))
}
}
func exitErrorf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(1)
}
后续补充