This topic describes how to use 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() {
// Specify the name of the bucket.
bucketName := "yourBucketName"
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create the bucket.
err = client.CreateBucket(bucketName)
if err != nil {
handleError(err)
}
}
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() {
// Specify the name of the bucket.
bucketName := "yourBucketName"
// Specify the full path of the object. Do not include the bucket name in the full path.
objectName := "yourObjectName"
// Specify the full path of the local file.
localFileName := "yourLocalFileName"
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Obtain the bucket information.
bucket, err := client.Bucket(bucketName)
if err != nil {
handleError(err)
}
// Upload the local file.
err = bucket.PutObjectFromFile(objectName, localFileName)
if err != nil {
handleError(err)
}
}
Download an object
The following code provides an example on how to download an object to your local computer:
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() {
// Specify the name of the bucket.
bucketName := "yourBucketName"
// Specify the full path of the object. Do not include the bucket name in the full path.
objectName := "yourObjectName"
// Specify the full path of the downloaded object.
downloadedFileName := "yourDownloadedFileName"
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Obtain the bucket information.
bucket, err := client.Bucket(bucketName)
if err != nil {
handleError(err)
}
// Download the object.
err = bucket.GetObjectToFile(objectName, downloadedFileName)
if err != nil {
handleError(err)
}
}
List objects
The following code provides an example on how to list objects in a specific bucket. By default, a maximum of 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() {
// Specify the name of the bucket.
bucketName := "yourBucketName"
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Obtain the bucket information.
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 objects are returned at a time.
for _, object := range lsRes.Objects {
fmt.Println("Bucket: ", object.Key)
}
if lsRes.IsTruncated {
marker = lsRes.NextMarker
} else {
break
}
}
}
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() {
// Specify the name of the bucket.
bucketName := "yourBucketName"
// Specify the full path of the object. Do not include the bucket name in the full path.
objectName := "yourObjectName"
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Obtain the bucket information.
bucket, err := client.Bucket(bucketName)
if err != nil {
handleError(err)
}
// Delete the object.
err = bucket.DeleteObject(objectName)
if err != nil {
handleError(err)
}
}
References
Create a bucket
Upload an object
Download an object
List objects
For the complete sample code that is used to list objects, visit GitHub.
For more information about the API operation that you can call to list objects, see GetBucket (ListObjects).
Delete an object
For the complete sample code that is used to delete an object, visit GitHub.
For more information about the API operation that you can call to delete an object, see DeleteObject.