1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
| package main
import ( "bytes" "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" )
func NewSess() *session.Session { access_key := "AKIAIOSFODNN7EXAMPLE" secret_key := "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" end_point := "http://192.168.163.121:9000"
sess, err := 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(true), })
if err != nil { panic(err) } return sess }
func main() {
get_bucket(NewSess()) get_file_and_folder(NewSess(), "test") getFile(NewSess(), "test", "新建 文本文档.txt") uploadFile(NewSess(), "test", "新建 文本文档1.txt", "im data in 新建 文本文档1.txt") }
func exitErrorf(msg string, args ...interface{}) { fmt.Fprintf(os.Stderr, msg+"", args...) os.Exit(1) }
func get_bucket(sess *session.Session) {
svc := s3.New(sess) 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", aws.StringValue(b.Name), aws.TimeValue(b.CreationDate)) }
for _, b := range result.Buckets { fmt.Printf("\n%s", aws.StringValue(b.Name)) }
}
func get_file_and_folder(sess *session.Session, bucket string) {
fmt.Println() fmt.Println(bucket)
svc := s3.New(sess)
params := &s3.ListObjectsInput{ Bucket: aws.String(fmt.Sprint("/", bucket)), Prefix: aws.String(""), } resp, err := svc.ListObjects(params)
if err != nil { exitErrorf("Unable to list items in bucket %q, %v", bucket, err) }
for _, item := range resp.Contents { fmt.Println("Name: ", *item.Key) fmt.Println("Last modified:", *item.LastModified) fmt.Println("Size: ", *item.Size) fmt.Println("Storage class:", *item.StorageClass) fmt.Println("") }
}
func getFile(sess *session.Session, 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(fmt.Sprint("/", 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 uploadFile(sess *session.Session, bucket string, filename, fileData string) {
uploader := s3manager.NewUploader(sess)
_, err := uploader.Upload(&s3manager.UploadInput{ Bucket: aws.String(fmt.Sprint("/", bucket)), Key: aws.String(filename), Body: bytes.NewReader([]byte(fileData)), }) if err != nil { exitErrorf("Unable to upload %q to %q, %v", filename, bucket, err) }
fmt.Printf("Successfully uploaded %q to %q ", filename, bucket) }
|