This topic describes how to use the OSS Go SDK to perform routine operations, such as creating buckets, uploading objects, and downloading objects.

Create buckets

A bucket is a global namespace in OSS. A bucket is a container for objects stored in OSS. The following code provides an example on how to create a bucket:

package main
import (
    "fmt"
    "os"
    "github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func handleError(err error) {
    fmt.Println("Error:", err)
    os.Exit(-1)
}
func main() {
    // This example uses the endpoint of the China (Hangzhou) region. Specify the actual endpoint based on your requirements.
    endpoint := "http://oss-cn-hangzhou.aliyuncs.com"
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS, because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to https://ram.console.aliyun.com.
    accessKeyId := "<yourAccessKeyId>"
    accessKeySecret := "<yourAccessKeySecret>"
    bucketName := "<yourBucketName>"
    // Create an OSSClient instance.
    client, err := oss.New(endpoint, accessKeyId, accessKeySecret)
    if err ! = nil {
        handleError(err)
    }
    // Create a bucket.
    err = client.CreateBucket(bucketName)
    if err ! = nil {
        handleError(err)
    }
}

For more information about bucket naming conventions, see the "Naming conventions" section in Basic concepts. For more information about creating buckets, see Create buckets.

For more information about endpoints, see Regions and endpoints.

Upload objects

The following code provides an example on how to upload an object to OSS:

package main
import (
    "fmt"
    "os"
    "github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func handleError(err error) {
    fmt.Println("Error:", err)
    os.Exit(-1)
}
func main() {
    // This example uses the endpoint of the China (Hangzhou) region. Specify the actual endpoint based on your requirements.
    endpoint := "http://oss-cn-hangzhou.aliyuncs.com"
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS, because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to https://ram.console.aliyun.com.
    accessKeyId := "<yourAccessKeyId>"
    accessKeySecret := "<yourAccessKeySecret>"
    bucketName := "<yourBucketName>"
    // <yourObjectName> indicates the complete path of the object you want to upload to OSS, and must include the file extension of the object. For example, set <yourObjectName> to abc/efg/123.jpg.
    objectName := "<yourObjectName>"
    // <yourLocalFileName> consists of a local object path and an object name with extension, for example, /users/local/myfile.txt.
    localFileName := "<yourLocalFileName>"
    // Create an OSSClient instance.
    client, err := oss.New(endpoint, accessKeyId, accessKeySecret)
    if err ! = nil {
        handleError(err)
    }
    // Obtain the bucket name.
    bucket, err := client.Bucket(bucketName)
    if err ! = nil {
        handleError(err)
    }
    // Upload an object.
    err = bucket.PutObjectFromFile(objectName, localFileName)
    if err ! = nil {
        handleError(err)
    }
}

For more information about uploading objects, see Upload objects.

Download objects

The following code provides an example on how to download an object to a local directory:

package main
import (
    "fmt"
    "os"
    "github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func handleError(err error) {
    fmt.Println("Error:", err)
    os.Exit(-1)
}
func main() {
    // This example uses the endpoint of the China (Hangzhou) region. Specify the actual endpoint based on your requirements.
    endpoint := "http://oss-cn-hangzhou.aliyuncs.com"
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS, because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to https://ram.console.aliyun.com.
    accessKeyId := "<yourAccessKeyId>"
    accessKeySecret := "<yourAccessKeySecret>"
    bucketName := "<yourBucketName>"
    // <yourObjectName> indicates the complete path of the object you want to download from OSS, and must include the file extension of the object. For example, set <yourObjectName> to abc/efg/123.jpg.
    objectName := "<yourObjectName>"
    downloadedFileName := "<yourDownloadedFileName>"
    // Create an OSSClient instance.
    client, err := oss.New(endpoint, accessKeyId, accessKeySecret)
    if err ! = nil {
        handleError(err)
    }
    // Obtain the bucket name.
    bucket, err := client.Bucket(bucketName)
    if err ! = nil {
        handleError(err)
    }
    // Download an object.
    err = bucket.GetObjectToFile(objectName, downloadedFileName)
    if err ! = nil {
        handleError(err)
    }
}

For more information about downloading objects, see Download objects.

List objects

The following code provides an example on how to list objects in a specified bucket. By default, 100 objects are listed.

package main
import (
    "fmt"
    "os"
    "github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func HandleError(err error) {
    fmt.Println("Error:", err)
    os.Exit(-1)
}
func main() {
    // Create an OSSClient instance.
    client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
    if err ! = nil {
        HandleError(err)
    }
    // Obtain the bucket name.
    bucketName := "<yourBucketName>"
    bucket, err := client.Bucket(bucketName)
    if err ! = nil {
        HandleError(err)
    }
    // List objects.
    marker := ""
    for {
        lsRes, err := bucket.ListObjects(oss.Marker(marker))
        if err ! = nil {
            HandleError(err)
        }
        // Display the listed objects. By default, 100 records are returned at a time. 
        for _, object := range lsRes.Objects {
            fmt.Println("Bucket: ", object.Key)
        }
        if lsRes.IsTruncated {
            marker = lsRes.NextMarker
        } else {
            break
        }
    }
}

For more information about listing objects, see List objects .

Delete objects

The following code provides an example on how to delete a specified object:

package main
import (
    "fmt"
    "os"
    "github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func handleError(err error) {
    fmt.Println("Error:", err)
    os.Exit(-1)
}
func main() {
    // This example uses the endpoint of the China (Hangzhou) region. Specify the actual endpoint based on your requirements.
    endpoint := "http://oss-cn-hangzhou.aliyuncs.com"
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS, because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to https://ram.console.aliyun.com.
    accessKeyId := "<yourAccessKeyId>"
    accessKeySecret := "<yourAccessKeySecret>"
    bucketName := "<yourBucketName>"
    // <yourObjectName> indicates the complete path of the object you want to delete from OSS, and must include the file extension of the object. For example, set <yourObjectName> to abc/efg/123.jpg.
    objectName := "<yourObjectName>"
    // Create an OSSClient instance.
    client, err := oss.New(endpoint, accessKeyId, accessKeySecret)
    if err ! = nil {
        handleError(err)
    }
    // Obtain the bucket name.
    bucket, err := client.Bucket(bucketName)
    if err ! = nil {
        handleError(err)
    }
    // Delete an object.
    err = bucket.DeleteObject(objectName)
    if err ! = nil {
        handleError(err)
    }
}

For more information about deleting objects, see Delete objects in Manage objects.