You can use object URLs, API operations, or SDKs to process images in Object Storage Service (OSS). This topic describes how to use these methods to process images.

Add parameters to the URL of an image object

You can add Image Processing (IMG) parameters or image style parameters to the end of the URL of an image object.
Notice By default, when you use a URL to access an image object in a bucket, the image is downloaded. To ensure that an image object is previewed when you access the image object, you must map a custom domain name to your bucket and add a CNAME record. For more information, see Map custom domain names.
Image processing mode Add IMG parameters Add image style parameters
IMG URL https://bucketname.endpoint/objectname?x-oss-process=image/action,parame_value https://bucketname.endpoint/objectname?x-oss-process=style/stylename
Parameter description
  • https://bucketname.endpoint/objectname: the URL of the image object. For more information about how to obtain the URL of an object, see How do I obtain the URL of an uploaded object?
  • x-oss-process=image/: the fixed parameter, which indicates that the image object is processed by adding IMG parameters.
  • action, param_value: the action, parameter, and value of an IMG operation. These parameters determine the IMG operation that is used to process the image object. Separate multiple operations with forward slashes (/). OSS processes images in the order of IMG parameters. For example, image/resize,w_200/rotate,90 indicates that OSS resizes the image to a width of 200 pixels, and rotates the image 90°. For more information about the supported IMG parameters, see Parameters.
  • https://bucketname.endpoint/objectname: the URL of the image object. For more information about how to obtain the URL of an object, see How do I obtain the URL of an uploaded object?
  • x-oss-process=style/: the fixed parameter, which indicates that the image object is processed by adding image style parameters.
  • stylename: the name of the style that you set in the OSS console. For more information, see Create a style.

If you specify a custom delimiter, you can use the delimiter to replace ?x-oss-process=style/ to simplify the IMG URL. For example, if you set the delimiter to an exclamation point (!), the URL of the processed image object is <https://bucketname.endpoint/objectname!stylename. For more information about how to configure custom delimiters, see the "Configure source image protection" section in Source image protection.

Examples https://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,w_300/quality,q_90 https://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=style/panda_style

The preceding description applies only to objects that can be anonymously accessed. If your object does not allow anonymous access, you must add the IMG operation to the signature by using SDKs. For more information, see the following topics:

Use OSS SDKs to process images

You can configure IMG or image style parameters in SDKs to process images. The following code provides an example on how to use OSS SDK for Java to process images.
  • Configure IMG parameters

    When you use IMG parameters to process images, separate multiple IMG operations with forward slashes (/).

    // The China (Hangzhou) region is used in this example as the endpoint. Specify the actual endpoint based on your requirements. 
    String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    String accessKeyId = "<yourAccessKeyId>";
    String accessKeySecret = "<yourAccessKeySecret>";
    String bucketName = "<yourBucketName>";
    String objectName = "<yourObjectName>";
    // Create an OSSClient instance. 
    OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    // Resize the image to a height and width of 100 pixels, and then add text watermarks. The content of the watermark is Hello image service. 
    String style = "image/resize,m_fixed,w_100,h_100/watermark,text_SGVsbG8g5Zu-54mH5pyN5YqhIQ";
    GetObjectRequest request = new GetObjectRequest(bucketName, objectName);
    request.setProcess(style);
    ossClient.getObject(request, new File("<example.jpg>"));
    // Shut down the OSSClient instance. 
    ossClient.shutdown();
                            
  • Configure image style parameters
    // The China (Hangzhou) region is used in this example as the endpoint. Specify the actual endpoint based on your requirements. 
    String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    String accessKeyId = "<yourAccessKeyId>";
    String accessKeySecret = "<yourAccessKeySecret>";
    String bucketName = "<yourBucketName>";
    String objectName = "<yourObjectName>";
    
    // Create an OSSClient instance. 
    OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    
    // Customize the image style. 
    String style = "style/<yourCustomStyleName>";
    GetObjectRequest request = new GetObjectRequest(bucketName, objectName);
    request.setProcess(style);
    
    ossClient.getObject(request, new File("<example.jpg>"));
    
    // Shut down the OSSClient instance. 
    ossClient.shutdown();
For more information about SDK demos for other programming languages, see the following topics:

Call API operations to process images

You can add IMG or image style parameters to GetObject requests to process images.
  • Add IMG parameters

    The following code provides a sample request:

    GET /oss.jpg?x-oss-process=image/resize,w_100 HTTP/1.1
    Host: oss-example.oss-cn-hangzhou.aliyuncs.com
    Date: Fri, 24 Feb 2012 06:38:30 GMT
    Authorization: OSS qn6qrrqxo2oawuk53otfjbyc:UNQDb7GapEgJkcde6OhZ9J*****
  • Add image style parameters

    The following code provides a sample request:

    GET /oss.jpg?x-oss-process=style/styleexample HTTP/1.1
    Host: oss-example.oss-cn-hangzhou.aliyuncs.com
    Date: Fri, 24 Feb 2012 06:40:10 GMT
    Authorization: OSS qn6qrrawuk53oqxo2otfjbyc:UNapEgQDb7GJkcde6OhZ9J*****

Save processed images

By default, IMG does not save processed images. However, OSS allows you to add the saveas parameter in an IMG request to save the processed image as an object in a specified bucket. For more information, see Save processed images.