OSS image processing lets you apply on-the-fly transformations—resize, rotate, format conversion, and more—to images stored in OSS. Transformations are expressed as a processing parameter string passed to GetObject or ProcessObject, so no extra service is required.
Usage notes
The examples in this topic use the public endpoint for the China (Hangzhou) region. To access OSS from another Alibaba Cloud service in the same region, use the internal endpoint instead. For endpoint details, see Regions and endpoints.
The examples create an
OSSClientinstance using an OSS endpoint. To create anOSSClientusing a custom domain or Security Token Service (STS), see Create an OSSClient instance.
How it works
All examples follow the same pattern:
Build a processing parameter string (e.g.,
"image/resize,m_fixed,w_100,h_100").Attach it to the request with
request.setProcess(process).Call
client.GetObject(request)to retrieve the transformed image, orclient.ProcessObject(request)to save the result to OSS.
For the complete list of supported parameters and operations, see Image processing parameters.
Prerequisites
Before you begin, ensure that you have:
Set the
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables with valid AccessKey credentialsA source image in the target bucket
Apply image processing parameters
Apply a single parameter
The following example resizes an image to a fixed 100×100 px using the resize operation.
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
// Replace with your actual endpoint, region, bucket name, and object name
std::string Endpoint = "yourEndpoint"; // e.g., https://oss-cn-hangzhou.aliyuncs.com
std::string Region = "yourRegion"; // e.g., cn-hangzhou
std::string BucketName = "examplebucket";
std::string ObjectName = "exampledir/example.jpg";
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
// Resize to a fixed 100x100 px
std::string Process = "image/resize,m_fixed,w_100,h_100";
GetObjectRequest request(BucketName, ObjectName);
request.setProcess(Process);
auto outcome = client.GetObject(request);
if (outcome.isSuccess()) {
std::cout << "Image processed successfully." << std::endl;
} else {
std::cout << "Failed to process image. Error code: " << outcome.error().Code()
<< ", Message: " << outcome.error().Message()
<< ", RequestId: " << outcome.error().RequestId() << std::endl;
}
ShutdownSdk();
return 0;
}Apply multiple parameters
Chain multiple operations by separating them with a forward slash (/).
The following example resizes the image to 100×100 px and then rotates it 90 degrees.
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
// Replace with your actual endpoint, region, bucket name, and object name
std::string Endpoint = "yourEndpoint";
std::string Region = "yourRegion";
std::string BucketName = "examplebucket";
std::string ObjectName = "exampledir/example.jpg";
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
// Resize to 100x100 px, then rotate 90 degrees
std::string Process = "image/resize,m_fixed,w_100,h_100/rotate,90";
GetObjectRequest request(BucketName, ObjectName);
request.setProcess(Process);
auto outcome = client.GetObject(request);
if (outcome.isSuccess()) {
std::cout << "Image processed successfully." << std::endl;
} else {
std::cout << "Failed to process image. Error code: " << outcome.error().Code()
<< ", Message: " << outcome.error().Message()
<< ", RequestId: " << outcome.error().RequestId() << std::endl;
}
ShutdownSdk();
return 0;
}Apply an image style
An image style bundles multiple processing parameters into a reusable, named preset. Use styles when you need to apply the same complex transformation repeatedly.
Create the style in the OSS console. For instructions, see Image styles.
Reference the style by name in the processing string:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
// Replace with your actual endpoint, region, bucket name, and object name
std::string Endpoint = "yourEndpoint";
std::string Region = "yourRegion";
std::string BucketName = "examplebucket";
std::string ObjectName = "exampledir/example.jpg";
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
// Replace yourCustomStyleName with the name of the style you created in step 1
std::string Process = "style/yourCustomStyleName";
GetObjectRequest request(BucketName, ObjectName);
request.setProcess(Process);
auto outcome = client.GetObject(request);
if (outcome.isSuccess()) {
std::cout << "Image processed successfully." << std::endl;
} else {
std::cout << "Failed to process image. Error code: " << outcome.error().Code()
<< ", Message: " << outcome.error().Message()
<< ", RequestId: " << outcome.error().RequestId() << std::endl;
}
ShutdownSdk();
return 0;
}Save a processed image to OSS
To save a processed image back to OSS, use ImgSaveAs with ProcessObject.
The ImgSaveAs directive appends to the processing string in the format:
|sys/saveas,o_<Base64UrlSafe(targetObjectName)>,b_<Base64UrlSafe(bucketName)>The following example resizes the image and saves the result as a new object in the same bucket.
#include <alibabacloud/oss/OssClient.h>
#include <sstream>
using namespace AlibabaCloud::OSS;
int main(void)
{
// Replace with your actual endpoint, region, bucket name, and object names
std::string Endpoint = "yourEndpoint";
std::string Region = "yourRegion";
std::string BucketName = "examplebucket";
std::string SourceObjectName = "example/example.jpg";
std::string TargetObjectName = "exampledir/example.jpg"; // output path
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
// Build the processing string: transform + save directive
std::string Process = "image/resize,m_fixed,w_100,h_100";
std::stringstream ss;
ss << Process
<< "|sys/saveas"
<< ",o_" << Base64EncodeUrlSafe(TargetObjectName)
<< ",b_" << Base64EncodeUrlSafe(BucketName);
ProcessObjectRequest request(BucketName, SourceObjectName, ss.str());
auto outcome = client.ProcessObject(request);
if (outcome.isSuccess()) {
std::cout << "Image processed successfully." << std::endl;
} else {
std::cout << "Failed to process image. Error code: " << outcome.error().Code()
<< ", Message: " << outcome.error().Message()
<< ", RequestId: " << outcome.error().RequestId() << std::endl;
}
ShutdownSdk();
return 0;
}Generate a signed URL with image processing parameters
A signed URL grants temporary, credential-free access to a private object. To apply image processing to a private image via a signed URL, include the processing parameters in the signature before signing—OSS does not support appending them to the URL afterward.
The maximum validity period of a signed URL is 32,400 seconds.
The following example generates a signed URL that serves the image resized to 100×100 px.
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
// Replace with your actual endpoint, region, bucket name, and object name
std::string Endpoint = "yourEndpoint";
std::string Region = "yourRegion";
std::string BucketName = "examplebucket";
std::string ObjectName = "exampledir/example.jpg";
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
// Set processing parameters before signing; they cannot be added to the URL afterward
std::string Process = "image/resize,m_fixed,w_100,h_100";
GeneratePresignedUrlRequest request(BucketName, ObjectName, Http::Get);
request.setProcess(Process);
auto outcome = client.GeneratePresignedUrl(request);
if (outcome.isSuccess()) {
std::cout << "Generated signed URL: " << outcome.result() << std::endl;
} else {
std::cout << "Failed to generate signed URL. Error code: " << outcome.error().Code()
<< ", Message: " << outcome.error().Message()
<< ", RequestId: " << outcome.error().RequestId() << std::endl;
}
ShutdownSdk();
return 0;
}What's next
Image processing parameters — full reference for supported operations and values
Image styles — create and manage reusable style presets