文档中心 > 快速入门
快速入门

最近更新时间:2022-07-25

本节介绍如何快速使用Yig-S3-SDK-Go完成常见操作,如创建桶、上传文件、下载文件等。


创建桶

以下代码用于创建桶:

package sample


import (
"fmt"
"github.com/journeymidnight/Yig-S3-SDK-Go/s3lib"
)

func MakeBucketSample() {
DeleteTestBucketAndObject()
defer DeleteTestBucketAndObject()

sc := s3lib.NewS3(endpoint, accessKey, secretKey)
// Create a bucket
err := sc.MakeBucket(bucketName)
if err != nil {
HandleError(err)
}

// Delete a bucket
err = sc.DeleteBucket(bucketName)
if err != nil {
HandleError(err)
}

//Make bucket with ACL
err = sc.MakeBucketWithAcl(bucketName, "public-read")
if err != nil {
HandleError(err)
}

out, err := sc.GetBucketAcl(bucketName)
if err != nil {
HandleError(err)
}
fmt.Println("Get Bucket ACL:", out)

err = sc.DeleteBucket(bucketName)
if err != nil {
HandleError(err)
}
fmt.Printf("CreateBucketSample Run Success!\n\n")
}


上传文件

以下代码用于上传文件至UOS:

package sample


import (
"fmt"
"github.com/journeymidnight/Yig-S3-SDK-Go/s3lib"
"os"
"strings"
)

func PutObjectSample() {
DeleteTestBucketAndObject()
defer DeleteTestBucketAndObject()
sc := s3lib.NewS3(endpoint, accessKey, secretKey)
// Create a bucket
err := sc.MakeBucket(bucketName)
if err != nil {
HandleError(err)
}

// 1. Put a string object
err = sc.PutObject(bucketName, objectKey, strings.NewReader("NewBucketAndObjectSample"))
if err != nil {
HandleError(err)
}

err = sc.DeleteObject(bucketName, objectKey)
if err != nil {
HandleError(err)
}

// 2. Put a file
f, err := os.Open(localFilePath)
defer f.Close()
if err != nil {
HandleError(err)
}
err = sc.PutObject(bucketName, objectKey, f)
if err != nil {
HandleError(err)
}
out, err := sc.GetObject(bucketName, objectKey)
if err != nil {
HandleError(err)
}

out.Close()

fmt.Printf("NewObjectSample Run Success !\n\n")
}


下载文件

以下代码用于下载文件到本地:

package sample


import (
"fmt"
"os"
"github.com/journeymidnight/Yig-S3-SDK-Go/s3lib"
"io"
)

func GetObjectSample() {
DeleteTestBucketAndObject()
defer DeleteTestBucketAndObject()
sc := s3lib.NewS3(endpoint, accessKey, secretKey)
// Create a bucket
err := sc.MakeBucket(bucketName)
if err != nil {
HandleError(err)
}

// Put a file
f, err := os.Open(localFilePath)
defer f.Close()
if err != nil {
HandleError(err)
}
err = sc.PutObject(bucketName, objectKey, f)
if err != nil {
HandleError(err)
}

// Get the reader
out, err := sc.GetObject(bucketName, objectKey)
if err != nil {
HandleError(err)
}

// Download to a file
f2, err := os.OpenFile("sample/Download.jpeg", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755)
defer f2.Close()
if err != nil {
HandleError(err)
}
io.Copy(f2, out)
out.Close()

fmt.Printf("GetObjectSample Run Success !\n\n")
}


列举文件

以下代码用于列举指定桶下的文件。默认列举100个文件。

package sample


import (
"fmt"
"github.com/journeymidnight/Yig-S3-SDK-Go/s3lib"
"strings"
)

func ListObjectsSample() {
var keys = []string{
objectKey + "1",
objectKey + "2",
objectKey + "3",
objectKey + "4",
objectKey + "/1-1",
objectKey + "/1-2",
objectKey + "/2-1",
objectKey + "/2-2",
}
sc := s3lib.NewS3(endpoint, accessKey, secretKey)
for _, k := range keys {
sc.DeleteObject(bucketName, k)
}
DeleteTestBucketAndObject()


// Create a bucket
err := sc.MakeBucket(bucketName)
if err != nil {
HandleError(err)
}

for _, k := range keys {
err := sc.PutObject(bucketName, k, strings.NewReader(k))
if err != nil {
HandleError(err)
}
}

keys, _, _, err = sc.ListObjects(bucketName, objectKey+"/", "/", 1000)
if err != nil {
HandleError(err)
}

for _, k := range keys {
err := sc.DeleteObject(bucketName, k)
if err != nil {
HandleError(err)
}
}
DeleteTestBucketAndObject()
fmt.Printf("ListObjectsSample Run Success !\n\n")
}

func printSlice(ss []string) {
for _, s := range ss{
fmt.Println(s)
}
}


删除文件

以下代码用于删除指定文件:

package sample


import (
"fmt"
"github.com/journeymidnight/Yig-S3-SDK-Go/s3lib"
"strings"
)

func DeleteObjectSample() {
DeleteTestBucketAndObject()
defer DeleteTestBucketAndObject()
// Delete single key
sc := s3lib.NewS3(endpoint, accessKey, secretKey)
// Create a bucket
err := sc.MakeBucket(bucketName)
if err != nil {
HandleError(err)
}

// 1. Delete an object
err = sc.PutObject(bucketName, objectKey, strings.NewReader("NewBucketAndObjectSample"))
if err != nil {
HandleError(err)
}

err = sc.DeleteObject(bucketName, objectKey)
if err != nil {
HandleError(err)
}

// Delete file that not exists will not failed
err = sc.DeleteObject(bucketName, objectKey)
if err != nil {
HandleError(err)
}

// 2. Delete multiple objects
err = sc.PutObject(bucketName, objectKey+"1", strings.NewReader("NewBucketAndObjectSample"))
if err != nil {
HandleError(err)
}

err = sc.PutObject(bucketName, objectKey+"2", strings.NewReader("NewBucketAndObjectSample"))
if err != nil {
HandleError(err)
}


fmt.Printf("DeleteObjectSample Run Success !\n\n")
}