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

You can use standard HTTP GET requests to access IMG. You can configure IMG parameters in the query string of a URL.

If the access control list (ACL) of an image object is private, only authorized users can access the image object.

  • Anonymous access
    OSSTask * task = [_client presignPublicURLWithBucketName:_publicBucketName
                                              withObjectKey:@"hasky.jpeg"
                                             withParameters:@{@"x-oss-process": @"image/resize,w_50"}];
  • Authorized access

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

    OSSGetObjectRequest * request = [OSSGetObjectRequest new];
    // Specify the name of the bucket in which the image is located. 
    request.bucketName = @"examplebucket";
    // Specify the name of the image. If the image is not stored in the root directory of the bucket, you must specify the full path of the image. Example: exampledir/example.jpg.    
    request.objectKey = @"exampledir/example.jpg";
    // Process the image. 
    request.xOssProcess = @"image/resize,m_lfit,w_100,h_100";
    OSSTask * getTask = [client getObject:request];
    [getTask continueWithBlock:^id(OSSTask *task) {
        if (!task.error) {
            NSLog(@"download image success!");
            OSSGetObjectResult * getResult = task.result;
            NSLog(@"download image data: %@", getResult.dowloadedData);
        } else {
            NSLog(@"download object failed, error: %@" ,task.error);
        }
        return nil;
    }];
    // [getTask waitUntilFinished];
    // [request cancel];
  • Access an image by using OSS SDKs
    OSSGetObjectRequest * request = [OSSGetObjectRequest new];
    // Specify the name of the bucket in which the image is located. 
    request.bucketName = @"examplebucket";
    // Specify the name of the image. If the image is not stored in the root directory of the bucket, you must specify the full path of the image. Example: exampledir/example.jpg.    
    request.objectKey = @"exampledir/example.jpg";
    // Resize the image to 100 x 100 pixels.       
    request.xOssProcess = @"image/resize,m_lfit,w_100,h_100";   
    OSSTask * task = [ossClient getObject:request];

Save processed images

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

OSSImagePersistRequest *request = [OSSImagePersistRequest new];
// Specify the name of the bucket in which the source image is located. 
request.fromBucket = @"srcbucket";
// Specify the name of the source image. If the image is not stored in the root directory of the bucket, you must specify the full path of the image. Example: exampledir/src.jpg. 
request.fromObject = @"exampledir/src.jpg";
// Specify the name of the bucket that stores the processed image. The bucket must be located in the same region as the bucket in which the source image is stored. 
request.toBucket = @"destbucket";
// Specify the name of the processed image.
request.toObject = @"exampledir/dest.jpg";
// Resize the image to 100 pixels in width and save the processed image to the current bucket. 
request.action = @"image/resize,w_100";
//request.action = @"resize,w_100";

[[[ossClient imageActionPersist:request] continueWithBlock:^id _Nullable(OSSTask * _Nonnull task) {

    return nil;
}] waitUntilFinished];

Generate a signed URL that includes IMG parameters for an object

The URLs of private objects must be signed. IMG parameters cannot be added to a signed URL. If you want to process a private object, add IMG parameters to the signature.

The following sample code provides an example on how to generate a signed URL that includes IMG parameters:

// Specify the name of the bucket in which the image is located. 
NSString *bucketName = @"examplebucket";
// Specify the name of the image. If the image is not stored in the root directory of the bucket, you must specify the full path of the image. Example: exampledir/example.jpg. 
NSString *objectKey = @"exampledir/example.jpg";
// Generate a signed URL that includes IMG parameters. Set the validity period of the signed URL to 1800 seconds. 
OSSTask *task = [_client presignConstrainURLWithBucketName:bucketName withObjectKey:objectKey withExpirationInterval:30 * 60 withParameters:@{@"x-oss-process": @"image/resize,w_50"}];
NSLog(@"url: %@", task.result);

References

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