This topic describes how to use the Object Storage Service (OSS) SDK for Go to perform routine operations, such as creating buckets, uploading objects, and downloading objects.

Create a bucket

A bucket is a global namespace in OSS. It is similar to a data container that stores files. 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() {
    // The China (Hangzhou) region is used in this example as the endpoint. Specify the actual endpoint. 
    endpoint := "http://oss-cn-hangzhou.aliyuncs.com"
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access Object Storage Service (OSS) because the account has permissions on all API operations. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    accessKeyId := "<yourAccessKeyId>"
    accessKeySecret := "<yourAccessKeySecret>"
    bucketName := "<yourBucketName>"
    // Create an OSSClient instance. 
    client, err := oss.New(endpoint, accessKeyId, accessKeySecret)
    if err != nil {
        handleError(err)
    }
    // Create the bucket. 
    err = client.CreateBucket(bucketName)
    if err != nil {
        handleError(err)
    }
}

For more information about bucket naming conventions, see Basic concepts. For more information about how to create a bucket, see Create buckets.

For more information about how to obtain an endpoint, see Regions and endpoints.

Upload an object

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() {
    // The China (Hangzhou) region is used in this example as the endpoint. Specify the actual endpoint. 
    endpoint := "http://oss-cn-hangzhou.aliyuncs.com"
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    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. Example: abc/efg/123.jpg. 
    objectName := "<yourObjectName>"
    // <yourLocalFileName> consists of a local file path and an object name with extension. 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. 
    bucket, err := client.Bucket(bucketName)
    if err != nil {
        handleError(err)
    }
    // Upload the object. 
    err = bucket.PutObjectFromFile(objectName, localFileName)
    if err != nil {
        handleError(err)
    }
}
For more information about how to upload objects, see Overview.

Download an object

The following code provides an example on how to download an object to your local device:

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() {
    // The China (Hangzhou) region is used in this example as the endpoint. Specify the actual endpoint. 
    endpoint := "http://oss-cn-hangzhou.aliyuncs.com"
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    accessKeyId := "<yourAccessKeyId>"
    accessKeySecret := "<yourAccessKeySecret>"
    bucketName := "<yourBucketName>"
    // <yourObjectName> indicates the complete path of the object you want to download from OSS. The path must include the file extension of the object. For example, you can 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. 
    bucket, err := client.Bucket(bucketName)
    if err != nil {
        handleError(err)
    }
    // Download the object. 
    err = bucket.GetObjectToFile(objectName, downloadedFileName)
    if err != nil {
        handleError(err)
    }
}

For more information about how to download objects, see Overview.

List objects

The following code provides an example on how to list objects in a specified bucket. You can list a maximum of 100 objects by default.

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() {    
    // The China (Hangzhou) region is used in this example as the endpoint. Specify the actual endpoint. 
    endpoint := "http://oss-cn-hangzhou.aliyuncs.com"
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    accessKeyId := "<yourAccessKeyId>"
    accessKeySecret := "<yourAccessKeySecret>"
    // Create an OSSClient instance. 
    client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
    if err != nil {
        HandleError(err)
    }
    // Obtain the bucket. 
    bucketName := "<yourBucketName>"
    bucket, err := client.Bucket(bucketName)
    if err != nil {
        handleError(err)
    }
    // List the objects in the bucket. 
    marker := ""
    for {
        lsRes, err := bucket.ListObjects(oss.Marker(marker))
        if err != nil {
            HandleError(err)
        }
        // Display the listed objects. By default, 100 objects 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 how to list objects, see List objects.

Delete an object

The following code provides an example on how to delete an 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() {
    // The China (Hangzhou) region is used in this example as the endpoint. Specify the actual endpoint. 
    endpoint := "http://oss-cn-hangzhou.aliyuncs.com"
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    accessKeyId := "<yourAccessKeyId>"
    accessKeySecret := "<yourAccessKeySecret>"
    bucketName := "<yourBucketName>"
    // <yourObjectName> indicates the complete path of the object that you want to delete from OSS. The path must include the extension of the object name. Example: 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. 
    bucket, err := client.Bucket(bucketName)
    if err != nil {
        handleError(err)
    }
    // Delete the object. 
    err = bucket.DeleteObject(objectName)
    if err != nil {
        handleError(err)
    }
}

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