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
You can use standard HTTP GET requests to access IMG. You can configure IMG parameters in the query string of an image object 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:@"examplebucket" withObjectKey:@"exampledir/example.jpg" withParameters:@{@"x-oss-process": @"image/resize,w_50"}]; [task continueWithBlock:^id(OSSTask *task) { if (!task.error) { NSLog(@"download image success!"); OSSGetObjectResult * getResult = task.result; NSLog(@"download image data: %@", getResult.downloadedData); } else { NSLog(@"download object failed, error: %@" ,task.error); } return nil; }]; // [task waitUntilFinished];
Authorized access
If you process an image by using OSS SDK for iOS, provide IMG parameters in the
request.xOssProcess()
method when you download the image. Sample code:OSSGetObjectRequest * request = [OSSGetObjectRequest new]; // Specify the name of the bucket in which the image is stored. 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"; // Specify IMG parameters. 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.downloadedData); } else { NSLog(@"download object failed, error: %@" ,task.error); } return nil; }]; // [getTask waitUntilFinished]; // [request cancel];
Save processed images
The following sample 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 stored.
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";
// Proportionally resize the image to a width of 100 pixels and save the processed image to the specified bucket.
request.action = @"image/resize,w_100";
// request.action = @"resize,w_100";
OSSTask * getTask = [client imageActionPersist:request];
[getTask continueWithBlock:^id _Nullable(OSSTask * _Nonnull task) {
return nil;
}];
// [getTask waitUntilFinished];
Generate a signed object URL that includes IMG parameters
URLs of private objects must be signed. IMG parameters cannot be directly added to the end of a signed URL. If you want to process a private image 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 stored.
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 1,800 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.