This document helps you understand how to process images via URL and SDK.
You can process images by URL or SDK.
Processing Images via URL
Operation Scenarios
After enabling ZOS and uploading images, you can process images by entering a URL in the browser address bar.
There are two URL-based methods: for public read objects, you can add processing parameters directly to the URL; for private objects, generate a pre-signed URL through the SDK and then append image processing parameters to that URL.
Note:When accessing an image via URL, browsers will download the image by default.
Public Read Object
URL Format
https://bucket-name.endpoint/object-name?x-zos-process=image/action,param_value
https://bucket-name.endpoint: Bucket’s external access address. Where, bucket-name is the bucket name and endpoint is the region domain.
object-name: the name of the image to access.
x-zos-process=image/: a fixed parameter, indicating the request for image processing.
{action,param_value}: the image processing action (action), parameters (param), and values (value) used to define processing behavior. Multiple operations are separated by forward slashes (/); different parameters after operation are separated by commas; and parameters use underlines (_) to attach their values. ZOS processes images in the order of the processing parameters. For example, "image/resize,p_30/rotate,90" means scale the image to 30% of the source image, then rotate it 90°. For supported parameters, see Image Processing Parameters.
Example
https://bucket-test.changsha42.zos.ctyun.cn/test.jpg?x-zos-process=image/resize,w_500,h_500/rotate,180
The above example will process the object test.jpg in the bucket-test bucket in the Changsha-42 resource pool and download the processed image locally. Specific operations: first, resize the image to 500 px*500 px, then rotate it 180°.
Private Object
URL Format
Generate a pre-signed GET URL for the target object using the SDK’s generate_presigned_url interface, then append image processing parameters to the generated URL. URL format:
http://endpoint/bucketname/objectname?AWSAccessKeyId=xxx&Expires=xxx&Signature=xxx&x-zos-process=image/action,param_value
http://endpoint/bucketname/objectname?AWSAccessKeyId=xxx&Expires=xxx&Signature=xxx: URL generated using SDK pre-signing.
x-zos-process=image/: a fixed parameter, indicating the request for image processing.
{action,param_value}: the image processing action (action), parameters (param), and values (value) used to define processing behavior. Multiple operations are separated by forward slashes (/); different parameters after operation are separated by commas; and parameters use underlines (_) to attach their values. ZOS processes images in the order of the processing parameters. For example, "image/resize,p_30/rotate,90" means scale the image to 30% of the source image, then rotate it 90°. For supported parameters, see Image Processing Parameters.
Example
http://ip:port/bucket/mypic.jpg?AWSAccessKeyId=xxx&Expires=xxx&Signature=xxx&x-zos-process=image/resize,w_500,h_500/rotate,180
The above request will resize and rotate mypic.jpg image object in the bucket: first scale to 500 px*500 px, then rotate 180°.
Note
URL concatenation currently only supports V2 authentication. Using V4 authentication to splice URL will result in a request failure. See the corresponding SDK Developer Documentation for pre-signed URL methods.
Processing Images via SDK
Operation Scenarios
You can process images by adding parameters in the SDK.
Example
Taking Java as an example, the text below introduces simple usage for image processing. For more language image processing, see SDK Developer Documentation.
Import com.amazonaws.ClientConfiguration;
Import com.amazonaws.auth.AWSCredentials;
Import com.amazonaws.auth.AWSStaticCredentialsProvider;
Import com.amazonaws.auth.BasicAWSCredentials;
Import com.amazonaws.client.builder.AwsClientBuilder;
Import com.amazonaws.services.s3.AmazonS3;
Import com.amazonaws.services.s3.AmazonS3ClientBuilder;
Import com.amazonaws.Protocol;
Import com.amazonaws.services.s3.model.GetObjectRequest;
Import com.amazonaws.services.s3.model.S3Object;
Import java.io.*;
Import java.util.Base64;
public class picturedemo {
// Fill in user AK and SK for access credentials.
public static String ACCESS_KEY="ACCESS_KEY";
public static String SECRET_KEY="SECRET_KEY";
// Endpoint is the endpoint from the console domain information.
public static String END_POINT="END_POINT";
// Fill in the bucket name.
public static String BUCKET_NAME="BUCKET_NAME";
// Fill in the full path of the target image object to process.
public static String OBJ_KEY = "OBJ_KEY";
// Fill in the local path to save the processed image object.
public static String SAVE_PATH = "./test.png";
public static void main(String[ ] args) {
AmazonS3 s3Client;
// When using the HTTPS protocol with self-signed certificates, disable certificate check.
//System.setProperty("com.amazonaws.sdk.disableCertChecking", "true");
// Establish connection with credentials and configuration.
AWSCredentials credentials=new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);
ClientConfiguration awsClientConfig = new ClientConfiguration();
// Use "S3SignerType" for V2 signing.
awsClientConfig.setSignerOverride("S3SignerType");
// Use "AWSS3V4SignerType" for V4 signing.
// awsClientConfig.setSignerOverride("AWSS3V4SignerType");
// Connection defaults to use the HTTPS protocol. Explicitly specify HTTP if using the HTTP protocol.
awsClientConfig.setProtocol(Protocol.HTTP);
s3Client=AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withClientConfiguration(awsClientConfig)
.withEndpointConfiguration(newAwsClientBuilder.EndpointConfiguration(END_POINT,""))
.disableChunkedEncoding()
.build();
GetObjectRequest request=new GetObjectRequest(BUCKET_NAME, OBJ_KEY);
// Image processing operation: crop a 300*200 rectangle at (10,10), then rotate 90°.
String imageop="image/crop,w_300,h_200,x_10,y_10/rotate,90";
request.setZosProcess(imageop);
S3Object result=s3Client.getObject(request);
System.out.print("=====request success=====\n");
// Save the downloaded image locally.
try{
InputStream in=result.getObjectContent();
File outputFile=new File(SAVE_PATH);
FileOutputStream outputStream=new
FileOutputStream(outputFile);
byte[] read_buf=new byte[1024 * 1024];
int read_len=0;
while ((read_len = in.read(read_buf)) > 0) {
outputStream.write(read_buf, 0, read_len);
}
in.close();
outputStream.close();
} catch (IOException e){
e.printStackTrace();
}
}
}