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 amounts of data. 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.

Usage

  • Anonymous access
    String url = oss.presignPublicObjectURL(testBucket, testObject);
            OSSLog.logDebug("signPublicURL", "get url: " + url);
    Add the x-oss-process:operation parameter to the generated URL, in which operation indicates the operation performed on the image.
  • Authorized access

    When you process images by using OSS SDKs, you can call the request.setxOssProcess() operation to configure IMG parameters. Example:

    // Construct an image download request. 
    // Specify the bucket name such as examplebucket and the full path of the object such as exampledir/exampleobject.txt. The full path cannot contain the bucket name. 
    GetObjectRequest get = new GetObjectRequest("examplebucket", "exampledir/exampleobject.txt");
    
    // Process the image. 
    request.setxOssProcess("image/resize,m_fixed,w_100,h_100");
    
    OSSAsyncTask task = oss.asyncGetObject(get, new OSSCompletedCallback<GetObjectRequest, GetObjectResult>() {
        @Override
        public void onSuccess(GetObjectRequest request, GetObjectResult result) {
            // The request is successful. 
            InputStream inputStream = result.getObjectContent();
    
            byte[] buffer = new byte[2048];
            int len;
    
            try {
                while ((len = inputStream.read(buffer)) != -1) {
                    // Process the downloaded data. 
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        @Override
        public void onFailure(GetObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
            // Handle any exception that occurs. You can add your own code. 
        }
    });
    Note To perform other operations on the image, replace the parameters of request.setxOssProcess().
  • Access by using OSS SDKs
    GetObjectRequest request = new GetObjectRequest("bucket-name", "image-name");
    request.setxOssProcess("image/resize,m_lfit,w_100,h_100"); // Configure IMG parameters. 
    OSSAsyncTask task = ossClient.asyncGetObject(request, getCallback);

Save processed images

The following code provides an example on how to save processed images:

// fromBucket indicates the name of the source bucket. toBucket indicates the name of the destination bucket. 
// fromObjectKey indicates the name of the source object. toObjectkey indicates the name of the destination object. The names of the source object and the destination object must be full paths that include the suffixes of the image objects. Example: abc/efg/123.jpg. 
// action indicates the IMG operation. 
ImagePersistRequest request = new ImagePersistRequest(fromBucket,fromObjectKey,toBucket,toObjectkey,action);

        OSSAsyncTask task = mOss.asyncImagePersist(request, new OSSCompletedCallback<ImagePersistRequest, ImagePersistResult>() {
            @Override
            public void onSuccess(ImagePersistRequest request, ImagePersistResult result) {
                // sucess callback
            }

            @Override
            public void onFailure(ImagePersistRequest request, ClientException clientException, ServiceException serviceException) {
                  // error callback
            }
        });

References

  • For more information about the supported IMG parameters, see IMG parameters.
  • For the complete sample code for IMG, visit GitHub.