All Products
Search
Document Center

Object Storage Service:Get started with OSS SDK for Go

Last Updated:Feb 27, 2026

Install OSS SDK for Go, configure credentials, and perform basic operations: creating a bucket, uploading, downloading, listing, and deleting objects.

Prerequisites

Before you begin, make sure you have:

  • An Alibaba Cloud account with Object Storage Service (OSS) activated

  • A Resource Access Management (RAM) user with an AccessKey pair. For details, see CreateAccessKey

  • Go installed

Verify your Go installation:

go version

Step 1: Install the SDK

Initialize a Go module and install OSS SDK for Go:

go mod init oss-quickstart
go get github.com/aliyun/aliyun-oss-go-sdk/oss

Step 2: Configure access credentials

Set the AccessKey pair of your RAM user as environment variables.

Important

Never hardcode AccessKey credentials in source code. Use environment variables or other secure methods to avoid accidental exposure. For more credential options, see Configure access credentials.

macOS/Linux

export OSS_ACCESS_KEY_ID=<ALIBABA_CLOUD_ACCESS_KEY_ID>
export OSS_ACCESS_KEY_SECRET=<ALIBABA_CLOUD_ACCESS_KEY_SECRET>

Windows

set OSS_ACCESS_KEY_ID=<ALIBABA_CLOUD_ACCESS_KEY_ID>
set OSS_ACCESS_KEY_SECRET=<ALIBABA_CLOUD_ACCESS_KEY_SECRET>

Step 3: Run the code example

This program creates a bucket, uploads an object, downloads it, lists objects in the bucket, and deletes the object.

  1. Create a file named main.go in your project directory:

package main

import (
	"log"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

// Global client shared across helper functions.
var client *oss.Client

func main() {
	// Replace with your actual values.
	bucketName := "yourBucketName"
	endpoint := "yourEndpoint"         // Example: https://oss-cn-hangzhou.aliyuncs.com
	region := "yourRegion"             // Example: cn-hangzhou

	// Load credentials from environment variables.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Error: %v", err)
	}

	// Create the client with AuthV4 signature.
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region(region))
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err = oss.New(endpoint, "", "", clientOptions...)
	if err != nil {
		log.Fatalf("Error: %v", err)
	}
	log.Printf("Client: %#v\n", client)

	// Create a bucket.
	if err := createBucket(bucketName); err != nil {
		log.Fatalf("Error: %v", err)
	}

	// Upload an object.
	objectName := "file.txt"
	localFileName := "/path/to/local/file.txt"
	if err := uploadFile(bucketName, objectName, localFileName); err != nil {
		log.Fatalf("Error: %v", err)
	}

	// Download the object.
	downloadedFileName := "/path/to/downloaded/file.txt"
	if err := downloadFile(bucketName, objectName, downloadedFileName); err != nil {
		log.Fatalf("Error: %v", err)
	}

	// List objects in the bucket.
	if err := listObjects(bucketName); err != nil {
		log.Fatalf("Error: %v", err)
	}

	// Delete the object.
	if err := deleteObject(bucketName, objectName); err != nil {
		log.Fatalf("Error: %v", err)
	}
}

func createBucket(bucketName string) error {
	err := client.CreateBucket(bucketName)
	if err != nil {
		return err
	}
	log.Printf("Bucket created successfully: %s", bucketName)
	return nil
}

func uploadFile(bucketName, objectName, localFileName string) error {
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		return err
	}
	err = bucket.PutObjectFromFile(objectName, localFileName)
	if err != nil {
		return err
	}
	log.Printf("File uploaded successfully to %s/%s", bucketName, objectName)
	return nil
}

func downloadFile(bucketName, objectName, downloadedFileName string) error {
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		return err
	}
	err = bucket.GetObjectToFile(objectName, downloadedFileName)
	if err != nil {
		return err
	}
	log.Printf("File downloaded successfully to %s", downloadedFileName)
	return nil
}

func listObjects(bucketName string) error {
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		return err
	}
	// ListObjects returns up to 100 objects per request by default.
	marker := ""
	for {
		lsRes, err := bucket.ListObjects(oss.Marker(marker))
		if err != nil {
			return err
		}
		for _, object := range lsRes.Objects {
			log.Printf("Object: %s", object.Key)
		}
		if !lsRes.IsTruncated {
			break
		}
		marker = lsRes.NextMarker
	}
	return nil
}

func deleteObject(bucketName, objectName string) error {
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		return err
	}
	err = bucket.DeleteObject(objectName)
	if err != nil {
		return err
	}
	log.Printf("Object deleted successfully: %s/%s", bucketName, objectName)
	return nil
}
  1. Replace the placeholder values in main.go:

    PlaceholderDescriptionExample
    yourBucketNameName of your OSS bucketmy-go-demo-bucket
    yourEndpointEndpoint for the region where the bucket is locatedhttps://oss-cn-hangzhou.aliyuncs.com
    yourRegionRegion ID where the bucket is locatedcn-hangzhou
    /path/to/local/file.txtPath to a local file to upload/tmp/file.txt
    /path/to/downloaded/file.txtPath to save the downloaded object/tmp/downloaded-file.txt
  2. Run the program:

    go run main.go

    Expected output:

    Client: &oss.Client{...}
    Bucket created successfully: my-go-demo-bucket
    File uploaded successfully to my-go-demo-bucket/file.txt
    File downloaded successfully to /tmp/downloaded-file.txt
    Object: file.txt
    Object deleted successfully: my-go-demo-bucket/file.txt

Clean up

After testing, delete the bucket to avoid unnecessary charges. Delete the bucket through the OSS console or by adding the following code:

err := client.DeleteBucket(bucketName)
if err != nil {
    log.Fatalf("Error: %v", err)
}
log.Printf("Bucket deleted: %s", bucketName)
Note A bucket must be empty before deletion. Delete all objects and incomplete multipart uploads first.

References