Image Processing is a feature of Object Storage Service (OSS) that provides a massive, secure, cost-effective, and highly reliable image processing service. After you upload an image to OSS, you can use simple RESTful interfaces to process it at any time, from any location, and on any device connected to the Internet.
Usage notes
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.
Process images using image processing parameters
Process an image with a single image processing parameter and save it as a local file
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() { // 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 a bucket in 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 a bucket in 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 { HandleError(err) } // Specify the name of the bucket where the source image is stored, for example, examplebucket. bucketName := "examplebucket" bucket, err := client.Bucket(bucketName) if err != nil { HandleError(err) } // The name of the source image. If the image is not in the root directory of the bucket, include the file path, such as example/example.jpg. sourceImageName := "yourObjectName" // Specify the name for the processed image. targetImageName := "LocalFileName" // Resize the image to a fixed width and height of 100 px and save it locally. style := "image/resize,m_fixed,w_100,h_100" err = bucket.GetObjectToFile(sourceImageName, targetImageName, oss.Process(style)) if err != nil { HandleError(err) } }Process an image with multiple image processing parameters and save it as a local file
When you use multiple image processing parameters, separate them with forward slashes (/).
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() { // 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 a bucket in 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 a bucket in 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 { HandleError(err) } // Specify the name of the bucket where the source image is stored, for example, examplebucket. bucketName := "examplebucket" bucket, err := client.Bucket(bucketName) if err != nil { HandleError(err) } // Specify the name of the source image. If the image is not in the root directory of the bucket, include the full path of the image, such as exampledir/example.jpg. sourceImageName := "exampledir/example.jpg" // Specify the name for the processed image. targetImageName := "exampledir/newexample.jpg" // Resize the image to a fixed width and height of 100 px, rotate it 90 degrees, and then save it locally. style := "image/resize,m_fixed,w_100,h_100/rotate,90" err = bucket.GetObjectToFile(sourceImageName, targetImageName, oss.Process(style)) if err != nil { HandleError(err) } }
Process images using styles
You can encapsulate multiple image processing parameters into a style and then use the style to process images in batches. For more information, see Image styles. The following code shows how to process an image using a style:
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() {
// 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 a bucket in 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 a bucket in 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 name of the bucket where the source image is stored, for example, examplebucket.
bucketName := "examplebucket"
bucket, err := client.Bucket(bucketName)
if err != nil {
HandleError(err)
}
// Specify the name of the source image. If the image is not in the root directory of the bucket, include the full path of the file, such as example/example.jpg.
sourceImageName := "example/example.jpg"
// Name the processed image newexample.jpg and save it locally.
targetImageName := "D:\\localpath\\newexample.jpg"
// Process the image using a style. Set yourCustomStyleName to the name of the image style that you created in the OSS console.
style := "style/yourCustomStyleName"
// Save the processed image locally.
err = bucket.GetObjectToFile(sourceImageName, targetImageName, oss.Process(style))
if err != nil {
HandleError(err)
}
}
Image processing persistence
You can use the ImgSaveAs operation to save the processed image to a specified bucket.
package main
import (
"encoding/base64"
"fmt"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func HandleError(err error) {
fmt.Println("Error:", err)
os.Exit(-1)
}
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 a bucket in 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 a bucket in 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 {
HandleError(err)
}
// Specify the name of the bucket where the source image is stored, for example, srcbucket.
bucketName := "SourceBucketName"
bucket, err := client.Bucket(bucketName)
if err != nil {
HandleError(err)
}
// Specify the name of the source image. If the image is not in the root directory of the bucket, include the full path of the file, such as example/example.jpg.
sourceImageName := "yourObjectName"
// Specify the name of the bucket to store the processed image. This bucket must be in the same region as the source bucket.
targetBucketName := "TargetBucketName"
// Specify the name for the processed image. If the image is not in the root directory of the bucket, include the full access path of the file, such as exampledir/example.jpg.
targetImageName := "TargetObjectName"
// Resize the image to a fixed width and height of 100 px and save it to the specified bucket.
style := "image/resize,m_fixed,w_100,h_100"
process := fmt.Sprintf("%s|sys/saveas,o_%v,b_%v", style, base64.URLEncoding.EncodeToString([]byte(targetImageName)), base64.URLEncoding.EncodeToString([]byte(targetBucketName)))
result, err := bucket.ProcessObject(sourceImageName, process)
if err != nil {
HandleError(err)
} else {
fmt.Println(result)
}
}
Generate a signed URL for a file with image processing parameters
The access URL for a private file contains a signature. OSS does not support appending image processing parameters directly to a signed URL. To process a private file, you must add the image processing parameters to the signature. The following code provides an example:
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() {
// 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 a bucket in 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 a bucket in 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 {
HandleError(err)
}
// Specify the name of the bucket where the image is stored, for example, examplebucket.
bucketName := "examplebucket"
bucket, err := client.Bucket(bucketName)
if err != nil {
HandleError(err)
}
// Specify the name of the image. If the image is not in the root directory of the bucket, include the full path of the file, such as exampledir/example.jpg.
ossImageName := "exampledir/example.jpg"
// Generate a signed URL that expires in 600 seconds. The maximum validity period is 32,400 seconds.
signedURL, err := bucket.SignURL(ossImageName, oss.HTTPGet, 600, oss.Process("image/format,png"))
if err != nil {
HandleError(err)
} else {
fmt.Println(signedURL)
}
}
References
For more information about the parameters that are supported by image processing, see Image processing.