Image Processing (IMG) provided by Object Storage Service (OSS) is a secure, cost-effective, and highly reliable image processing service that can process large numbers of images. After you upload source images to OSS, you can call RESTful API operations to process the images on a device that is connected over the Internet anytime, anywhere.

For more information about IMG parameters, see Parameters.

Use IMG parameters to process images

  • Use a single IMG parameter to process an image and save the image 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() {
        // Create an OSSClient instance. 
        client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
        if err != nil {
        HandleError(err)
        }
    
        // Specify the bucket in which the source image is stored. 
        bucketName := "<yourBucketName>"
        bucket, err := client.Bucket(bucketName)
        if err != nil {
        HandleError(err)
        }
    
        // Specify the name of the source image. If the image is not stored in the root directory of the bucket, you must specify the path of the image. Example: example/example.jpg. 
        sourceImageName := "<yourObjectName>"
        // Specify the name of the processed image. 
        targetImageName := "<LocalFileName>"
        // Resize the image to a height and width of 100 pixels and save the image to your local computer. 
        style := "image/resize,m_fixed,w_100,h_100"
        err = bucket.GetObjectToFile(sourceImageName, targetImageName, oss.Process(style))
        if err != nil {
        HandleError(err)
        }
    }
  • Use multiple IMG parameters to process an image at the same time and save the image to your local computer
    The following code provides an example on how to use multiple IMG parameters to process an image at the same time. Separate the parameters 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() {
        // Create an OSSClient instance. 
        // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the actual endpoint. 
        // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to access OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
        client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
        if err != nil {
        HandleError(err)
        }
    
        // Specify the name of the bucket in which the source image is stored. 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 stored in the root directory of the bucket, you must specify the full path of the image. Example: exampledir/example.jpg. 
        sourceImageName := "exampledir/example.jpg"
        // Specify the name of the processed image. 
        targetImageName := "exampledir/newexample.jpg"
        // Resize the image to a height and width of 100 pixels, rotate the image 90 degrees, and save the image to your local computer. 
        style := "image/resize,m_fixed,w_100,h_100/rotate,90"
        err = bucket.GetObjectToFile(sourceImageName, targetImageName, oss.Process(style))
        if err != nil {
         HandleError(err)
        }
    }

Use an image style to process an image

You can encapsulate multiple IMG parameters within a style, and then use the style to process images. For more information, see Image styles. The following code provides an example on how to use an image style to process an image:
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() {
    // Create an OSSClient instance.    
    // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the actual endpoint. 
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to access OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
    if err != nil {
    HandleError(err)
    }

    // Specify the name of the bucket in which the source image is stored. 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 stored in the root directory of the bucket, you must specify the full path of the image. Example: example/example.jpg. 
    sourceImageName := "example/example.jpg"
    // Name the processed image newexample.jpg and save the image to your local computer. 
    targetImageName := "D:\\localpath\\newexample.jpg"
    // Use the image style to process the image. In this example, set yourCustomStyleName to the name of the image style that you create in the OSS console. 
    style := "style/yourCustomStyleName"
    // Save the processed image to your local computer. 
    err = bucket.GetObjectToFile(sourceImageName, targetImageName, oss.Process(style))
    if err != nil {
    HandleError(err)
    }
}

Save processed images

You can call the ImgSaveAs operation to save the processed images to the specified bucket. The following code provides an example on how to save a processed image:
  • Save the processed image to the current bucket
    package main
    
    import (
        "fmt"
        "os"
        "encoding/base64"
        "github.com/aliyun/aliyun-oss-go-sdk/oss"
    )
    
    func HandleError(err error) {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    
    func main() {
        // Create an OSSClient instance. 
        // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the actual endpoint. 
        // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to access OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
        client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
        if err != nil {
        HandleError(err)
        }    
    
        // Specify the name of the bucket in which the source image is stored. Example: srcbucket. 
        bucketName := "srcbucket"
        bucket, err := client.Bucket(bucketName)
        if err != nil {
         HandleError(err)
        }
        // Specify the name of the source image. If the image is not stored in the root directory of the bucket, you must specify the full path of the image. Example: example/example.jpg. 
        sourceImageName := "example/example.jpg"
        // Specify the bucket that stores the processed image. The bucket must be within the same region as the bucket in which the source image is stored. 
        targetBucketName := "destbucket"
        // Specify the name of the processed image. If the image is not stored in the root directory of the bucket, you must specify the full path of the image. Example: exampledir/example.jpg. 
        targetImageName = "exampledir/example.jpg"
        // Resize the image to a height and width of 100 pixels and save the image 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)
        }
    }                      
  • Save the processed image to the specified bucket
    package main
    
    import (
        "fmt"
        "os"
        "encoding/base64"
        "github.com/aliyun/aliyun-oss-go-sdk/oss"
    )
    
    func HandleError(err error) {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    
    func main() {
        // Create an OSSClient instance. 
        client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
        if err != nil {
        HandleError(err)
        }    
    
        // Specify the bucket in which the source image is stored. 
        bucketName := "<yourSourceBucketName>"
        bucket, err := client.Bucket(bucketName)
        if err != nil {
         HandleError(err)
        }
        // Specify the name of the source image. If the image is not stored in the root directory of the bucket, you must specify the path of the image. Example: example/example.jpg. 
        sourceImageName := "<yourObjectName>"
        // Specify the bucket that stores the processed image. The bucket must be within the same region as the bucket in which the source image is stored. 
        targetBucketName := "<TargetBucketName>"
        // Specify the name of the processed image. 
        targetImageName = "<TargetObjectName>"
        // Resize the image to a height and width of 100 pixels and save the image 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 that includes IMG parameters for an object

URLs of private objects must be signed. IMG parameters cannot be added to a signed URL. If you want to process a private object, add IMG parameters to the signature. The following code provides an example on how to add IMG parameters to the signature:
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() {
    // Create an OSSClient instance. 
    // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the actual endpoint. 
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to access OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
    if err != nil {
    HandleError(err)
    }

    // Specify the name of the bucket in which the image is stored. 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 stored in the root directory of the bucket, you must specify the full path of the image. Example: exampledir/example.jpg. 
    ossImageName := "exampledir/example.jpg"
    // Generate a signed URL that includes IMG parameters. Set the validity period of the signed URL to 600 seconds. 
    signedURL, err := bucket.SignURL(ossImageName, oss.HTTPGet, 600, oss.Process("image/format,png"))
    if err != nil {
    HandleError(err)
    } else {
    fmt.Println(signedURL)
    }
}