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
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); // Append the x-oss-process:operation parameter to the generated URL. // The operation parameter specifies the image processing operation.Authorized access
When you use the software development kit (SDK) for image processing, you can set the processing parameters by calling the
request.setxOssProcess()method during an image download. The following code provides an example:// Create an image download request. // Specify the bucket name, for example, examplebucket, and the full path of the object, for example, exampledir/exampleobject.txt. The full path of the object cannot contain the bucket name. GetObjectRequest request = 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 the exception as needed. } });NoteTo perform other operations on the image, replace the parameters of
request.setxOssProcess().Access using the OSS SDK
GetObjectRequest request = new GetObjectRequest("bucket-name", "image-name"); request.setxOssProcess("image/resize,m_lfit,w_100,h_100"); // Set image processing parameters. OSSAsyncTask task = ossClient.asyncGetObject(request, getCallback);
Save processed images
The following sample code provides an example on how to save processed images:
// fromBucket and toBucket specify the source and destination bucket names.
// fromObjectKey and toObjectKey specify the source and destination object names. Specify the full path including the file extension, for example, abc/efg/123.jpg.
// action specifies the image processing operation, such as "image/resize,m_lfit,w_100,h_100" from the example above.
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) {
// Success callback
log.i("info", "Success");
}
@Override
public void onFailure(ImagePersistRequest request, ClientException clientException, ServiceException serviceException) {
// The request failed.
if (clientException != null) {
// A client exception occurred, such as a network exception.
clientException.printStackTrace();
}
if (serviceException != null) {
// A server exception occurred.
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.