Image styles (OSS SDK for Go 1.0)

Updated at:

Image styles let you combine multiple image processing operations, such as resizing, cropping, rotation, and watermarking, into a reusable preset that you can apply to any image in a bucket.

Usage notes

  • For information about the image processing parameters that are supported by image styles, see IMG parameters.

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and Endpoints.

  • In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.

  • This topic demonstrates creating an OSSClient instance with an OSS endpoint. For alternative configurations, such as using a custom domain or authenticating with credentials from Security Token Service (STS), see Configure a client (Go SDK V1).

  • By default, an Alibaba Cloud account has the permissions to create and use image styles. If you want to create and use image styles by using a RAM user or Security Token Service (STS), take note of the following items:

    • To create an image style, you must have the oss:PutStyle permission.

    • To query a specific image style of a bucket, you must have the oss:GetStyle permission.

    • To query all image styles of a bucket, you must have the oss:ListStyle permission.

    • To delete a specific image style of a bucket, you must have the oss:DeleteStyle permission.

    For more information, see Common examples of RAM policies.

Create an image style

You can create up to 50 image styles per bucket. The following sample code shows how to create an image style:

package main

import (
	"fmt"
	"os"

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

func main()  {
    // 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. 
    // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
    clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
    clientOptions = append(clientOptions, oss.Region("yourRegion"))
    // Specify the version of the signature algorithm.
    clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
    client, err := oss.New("yourEndpoint", "", "", clientOptions...)
    if err != nil {
    	    fmt.Println("Error:", err)
	    os.Exit(-1)
    }
    // Specify the name of the bucket. Example: examplebucket. 
    bucketName := "examplebucket"
    // Specify the operation of the image style, such as scaling the source image down to 63% and setting the relative quality of the image to 90%.
    styleContent := "image/resize,p_63/quality,q_90"
    // Specify the name of the image style. Example: imagestyle. 
    styleName := "imagestyle"
    err = client.PutBucketStyle(bucketName,styleName, styleContent)
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    fmt.Println("Bucket Style Set Success!")
}

Query a specific image style

The following sample code shows how to query a specific image style of a bucket:

package main

import (
	"fmt"
	"os"

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

func main()  {
    // 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. 
    // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
    clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
    clientOptions = append(clientOptions, oss.Region("yourRegion"))
    // Specify the version of the signature algorithm.
    clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
    client, err := oss.New("yourEndpoint", "", "", clientOptions...)
    if err != nil {
    	    fmt.Println("Error:", err)
	    os.Exit(-1)
    }
    // Specify the name of the bucket. Example: examplebucket. 
    bucketName := "examplebucket"
    // Specify the name of the image style. Example: imagestyle. 
    styleName := "imagestyle"
    // Query the image style of the examplebucket bucket. 
    style,err := client.GetBucketStyle(bucketName,styleName)
    if err != nil {
            fmt.Println("Error:", err)
            os.Exit(-1)
    }
    fmt.Printf("Style Name:%s\n", style.Name)
    fmt.Printf("Style Content:%s\n", style.Content)
    fmt.Printf("Style Create Time:%s\n", style.CreateTime)
    fmt.Printf("Style Last Modify Time:%s\n", style.LastModifyTime)
}

Query all image styles

The following sample code shows how to query all image styles of a bucket:

package main

import (
	"fmt"
	"os"

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

func main()  {
    // 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. 
    // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
    clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
    clientOptions = append(clientOptions, oss.Region("yourRegion"))
    // Specify the version of the signature algorithm.
    clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
    client, err := oss.New("yourEndpoint", "", "", clientOptions...)
    if err != nil {
	    fmt.Println("Error:", err)
	    os.Exit(-1)
    }
    // Specify the name of the bucket. Example: examplebucket. 
    bucketName := "examplebucket"
    // Query all image styles of the examplebucket bucket. 
    list,err := client.ListBucketStyle(bucketName)
    if err != nil {
            fmt.Println("Error:", err)
            os.Exit(-1)
    }

    for _, style := range list.Style {
        fmt.Printf("Style Name:%s\n", style.Name)
        fmt.Printf("Style Content:%s\n", style.Content)
        fmt.Printf("Style Create Time:%s\n", style.CreateTime)
        fmt.Printf("Style Last Modify Time:%s\n", style.LastModifyTime)
    }
}

Delete a specific image style

The following sample code shows how to delete a specific image style from a bucket:

package main

import (
	"fmt"
	"os"

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

func main()  {
    /// 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. 
    // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
    clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
    clientOptions = append(clientOptions, oss.Region("yourRegion"))
    // Specify the version of the signature algorithm.
    clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
    client, err := oss.New("yourEndpoint", "", "", clientOptions...)
    if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
    }
    // Specify the name of the bucket. Example: examplebucket. 
    bucketName := "examplebucket"
    // Specify the name of the image style. Example: imagestyle. 
    styleName := "imagestyle"
    // Delete the specified image style. 
    err = client.DeleteBucketStyle(bucketName,styleName)
    if err != nil {
            fmt.Println("Error:", err)
            os.Exit(-1)
    }
    fmt.Println("Bucket Style Delete Success!")
}

References

  • For the API operation used to create an image style, see PutStyle.

  • For the API operation used to query a specific image style of a bucket, see GetStyle.

  • For the API operation used to query all image styles of a bucket, see ListStyle.

  • For the API operation used to delete a specific image style from a bucket, see DeleteStyle.