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

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. 
        // 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. 
        // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in 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, precede the image name with the path to 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 a 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 a 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. 
        // 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. 
        // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in 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, precede the image name with the path to 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 images

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.    
    // 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. 
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in 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, precede the image name with the path to the image. Example: example/example.jpg. 
    sourceImageName := "example/example.jpg"
    // Set the name of the processed image to newexample.jpg and specify the path where you want to store the processed image on your local computer. 
    targetImageName := "D:\\localpath\\newexample.jpg"
    // Use the specified image style to process the image. In this example, yourCustomStyleName is set to the name of the image style that you created 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 a 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. 
        // 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. 
        // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in 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, precede the image name with the path to the image. Example: example/example.jpg. 
        sourceImageName := "example/example.jpg"
        // Set the bucket that stores the processed image to the bucket where 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, precede the image name with the path to 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 a 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. 
        // 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. 
        // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in 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, precede the image name with the path to 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 object URL that includes IMG parameters

The URLs of private objects must be signed. IMG parameters cannot be added to the end of 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 a 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. 
    // 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. 
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in 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 image. If the image is not stored in the root directory of the bucket, precede the image name with the path to 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)
    }
}

References

For more information about the supported IMG parameters, see IMG parameters.