To perform a series of operations, such as scaling, cropping, rotating, or adding watermarks on images stored in OSS, you can create an image style. An image style lets you define multiple image processing operations. You can then apply this style to images in a bucket to quickly process and transform them.
Notes
For more information about the image processing parameters that image styles support, see Image processing.
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.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Configure OSSClient instances.
By default, an Alibaba Cloud account has the required permissions for image style operations. If you want to use a Resource Access Management (RAM) user or STS to perform image style operations, you must have the following permissions:
To create an image style, you must have the
oss:PutStylepermission.To query information about a specific image style in a bucket, you must have the
oss:GetStylepermission.To query all created image styles in a bucket, you must have the
oss:ListStylepermission.To delete a specific image style from a bucket, you must have the
oss:DeleteStylepermission.
For more information, see Grant custom access policies to a RAM user.
Create an image style
A bucket can have a maximum of 50 image styles. The following code provides an example of 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 this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Set yourEndpoint to the endpoint of the bucket. For example, for the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
// Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
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 bucket name, for example, examplebucket.
bucketName := "examplebucket"
// Set the image operations for the image style. For example, scale the image to 63% of its original size and then set the relative quality to 90%.
styleContent := "image/resize,p_63/quality,q_90"
// Set the image style name, for 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 code provides an example of how to query information about a specific image style in 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 this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Set yourEndpoint to the endpoint of the bucket. For example, for the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
// Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
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 bucket name, for example, examplebucket.
bucketName := "examplebucket"
// Specify the image style name, for example, imagestyle.
styleName := "imagestyle"
// Query information about the specified image style in the examplebucket.
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 code provides an example of how to query all created image styles in 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 this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Set yourEndpoint to the endpoint of the bucket. For example, for the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
// Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
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 bucket name, for example, examplebucket.
bucketName := "examplebucket"
// Query all created image styles in the examplebucket.
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 code provides an example of 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 this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Set yourEndpoint to the endpoint of the bucket. For example, for the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
// Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
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 bucket name, for example, examplebucket.
bucketName := "examplebucket"
// Specify the image style name, for 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 more information about the API operation to create an image style, see PutStyle.
For more information about the API operation to query a specific image style in a bucket, see GetStyle.
For more information about the API operation to query all created image styles in a bucket, see ListStyle.
For more information about the API operation to delete a specific image style from a bucket, see DeleteStyle.