Data processing (Java SDK V2)

Updated at:

This topic describes how to use OSS Java SDK V2 to call the ProcessObject operation to synchronously process images and videos stored in OSS, and how to call the AsyncProcessObject operation to submit asynchronous data processing tasks.

Prerequisites

  • OSS Java SDK V2 is installed. If you use Maven, add the following dependency to the pom.xml file: com.aliyun:alibabacloud-oss-v2 .

  • Access credentials are configured by using environment variables. The sample code in this topic uses EnvironmentVariableCredentialsProvider to read credentials from the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables.

Permissions

By default, an Alibaba Cloud account has full permissions. However, the RAM users or RAM roles of the Alibaba Cloud account have no permissions. The Alibaba Cloud account or an account administrator must grant the required permissions to the RAM users or RAM roles.

API

Action

Description

ProcessObject

oss:PostProcessTask

Processes images or videos stored in OSS.

AsyncProcessObject

oss:PostProcessTask

Submits an asynchronous data processing task. This operation is suitable for time-consuming processing scenarios, such as video transcoding, video concatenation, and video frame capture.

Sample code

import com.aliyun.sdk.service.oss2.OSSClient;
import com.aliyun.sdk.service.oss2.OSSClientBuilder;
import com.aliyun.sdk.service.oss2.credentials.CredentialsProvider;
import com.aliyun.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider;
import com.aliyun.sdk.service.oss2.models.*;
import com.aliyun.sdk.service.oss2.models.internal.ImageProcessJson;

public class ProcessObjectSample {
    public static void main(String[] args) throws Exception {
        String region = "cn-hangzhou";
        String bucket = "examplebucket";
        String key = "example.jpg";
        String process = "image/resize,m_fixed,w_100,h_100|sys/saveas,o_dGVzdC5qcGc,b_ZXhhbXBsZWJ1Y2tldA";

        CredentialsProvider provider = new EnvironmentVariableCredentialsProvider();
        OSSClientBuilder clientBuilder = OSSClient.newBuilder()
                .credentialsProvider(provider)
                .region(region);

        try (OSSClient client = clientBuilder.build()) {
            ProcessObjectRequest request = ProcessObjectRequest.newBuilder()
                    .bucket(bucket)
                    .key(key)
                    .process(process)
                    .build();

            ProcessObjectResult result = client.processObject(request);

            System.out.printf("status code:%d, request id:%s%n",
                    result.statusCode(), result.requestId());

            ImageProcessJson imageProcess = result.imageProcessResult();
            if (imageProcess != null) {
                System.out.printf("Bucket: %s, Object: %s, File size: %d%n",
                        imageProcess.bucket(), imageProcess.object(), imageProcess.fileSize());
            }

        } catch (Exception e) {
            System.out.printf("error:%n%s", e);
        }
    }
}

Asynchronous data processing

For time-consuming data processing tasks, such as video transcoding, video concatenation, and video frame capture, we recommend that you use the AsyncProcessObject operation to submit an asynchronous task. After the task is submitted, OSS processes the task in the background. You can use the returned Event ID and Task ID to query the task status and progress.

Sample code

The following sample code shows how to submit an asynchronous data processing task:

import com.aliyun.sdk.service.oss2.OSSClient;
import com.aliyun.sdk.service.oss2.OSSClientBuilder;
import com.aliyun.sdk.service.oss2.credentials.CredentialsProvider;
import com.aliyun.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider;
import com.aliyun.sdk.service.oss2.models.*;
import com.aliyun.sdk.service.oss2.models.internal.AsyncProcessJson;

public class AsyncProcessObjectSample {
    public static void main(String[] args) {
        String region = "cn-hangzhou";
        String bucket = "examplebucket";
        String key = "example.mp4";
        String process = "video/convert,f_mp4";

        CredentialsProvider provider = new EnvironmentVariableCredentialsProvider();
        OSSClientBuilder clientBuilder = OSSClient.newBuilder()
                .credentialsProvider(provider)
                .region(region);

        try (OSSClient client = clientBuilder.build()) {
            AsyncProcessObjectRequest request = AsyncProcessObjectRequest.newBuilder()
                    .bucket(bucket)
                    .key(key)
                    .process(process)
                    .build();

            AsyncProcessObjectResult result = client.asyncProcessObject(request);

            System.out.printf("Status code:%d, request id:%s%n",
                    result.statusCode(), result.requestId());

            AsyncProcessJson asyncProcess = result.asyncProcessJson();
            if (asyncProcess != null) {
                System.out.printf("Event ID: %s%n", asyncProcess.eventId());
                System.out.printf("Task ID: %s%n", asyncProcess.taskId());
                System.out.printf("Process Request ID: %s%n", asyncProcess.processRequestId());
            }
        } catch (Exception e) {
            System.out.printf("error:%n%s", e);
        }
    }
}

References