All Products
Search
Document Center

Object Storage Service:IMG

Last Updated:Mar 12, 2024

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

Usage notes

  • Before you run the sample code in this topic, you must create an OSSClient instance by using methods such as using a custom domain name or Security Token Service (STS). For more information, see Initialization.

Usage of IMG

Note

IMG parameters apply only to the downloaded image data and do not affect the source images. To replace source images with processed images, see Save processed images.

  • 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 is the operation to perform on the image.
  • Authorized access

    To process an image by using the OSS SDK, call the request.setxOssProcess() method to specify IMG parameters when you download the image. The following sample code provides an example on how to process an image by using OSS SDK for Android:

    // Construct an image download request. 
    // Specify the name of the bucket and the full path of the object. In this example, the bucket name is examplebucket and the full path of the object is exampledir/exampleobject.txt. Do not include the bucket name in the full path. 
    GetObjectRequest request = new GetObjectRequest("examplebucket", "exampledir/exampleobject.txt");
    
    // Specify IMG parameters. 
    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 exceptions. You can add your own code. 
        }
    });
    Note

    To perform other operations on the image, replace the parameters specified in request.setxOssProcess().

  • Access by using the OSS SDK

    GetObjectRequest request = new GetObjectRequest("bucket-name", "image-name");
    request.setxOssProcess("image/resize,m_lfit,w_100,h_100"); // Specify IMG parameters. 
    OSSAsyncTask task = ossClient.asyncGetObject(request, getCallback);

Save processed images

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

// fromBucket specifies the name of the source bucket and toBucket specifies the name of the destination bucket. 
// fromObjectKey specifies the name of the source object. toObjectkey specifies 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 object names. Example: abc/efg/123.jpg. 
// action specifies the IMG action to perform, such as "image/resize,m_lfit,w_100,h_100" in the preceding example. 
ImagePersistRequest request = new ImagePersistRequest(fromBucket,fromObjectKey,toBucket,toObjectkey,action);

        OSSAsyncTask task = oss.asyncImagePersist(request, new OSSCompletedCallback<ImagePersistRequest, ImagePersistResult>() {
            @Override
            public void onSuccess(ImagePersistRequest request, ImagePersistResult result) {
                // sucess callback
                log.i("info", "Success");
            }

            @Override
            public void onFailure(ImagePersistRequest request, ClientException clientException, ServiceException serviceException) {
              // Handle request exceptions. 
              if (clientException != null) {
                  // Handle client-side exceptions, such as network exceptions. 
                  clientException.printStackTrace();
              }
              if (serviceException != null) {
                  // Handle server-side exceptions. 
                  Log.e("ErrorCode", serviceException.getErrorCode());
                  Log.e("RequestId", serviceException.getRequestId());
                  Log.e("HostId", serviceException.getHostId());
                  Log.e("RawMessage", serviceException.getRawMessage());
              }
            }
        });

References

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

  • For the complete sample code that is used to perform IMG operations, visit GitHub.

  • For more information about how to initialize an OSSClient instance, see Initialization.