全部文档
当前文档

暂无内容

如果没有找到您期望的内容,请尝试其他搜索词

文档中心

创建批量处理任务(Java)

最近更新时间:2026-07-28 15:02:28

以下代码用于创建批量处理任务:

public void createJob() {
        // 初始化 Ks3Client,参见“初始化”文档
        Ks3Client ks3Client = initKs3Client();

        try {
            // 创建 CreateJobRequest 对象
            CreateJobRequest request = new CreateJobRequest();
            // 设置任务描述
            request.setDescription("this-is-a-test-job");
            // 设置任务优先级
            request.setPriority(100);

            // 任务操作,支持 RestoreObjectOperation、DeleteObjectOperation、PutObjectAclOperation、PutObjectDataRedundancyTransitionOperation、AsyncWarmupOperation、CompressObjectOperation
            // 新建 RestoreObjectOperation 对象
            RestoreObjectOperation operation = new RestoreObjectOperation();
            // 设置存储类型
            operation.setStorageClass(StorageClass.ColdArchive.toString());
            // 设置解冻优先级
            operation.setJobTier(RestoreTier.EXPEDITED);
            // 设置解冻天数
            operation.setExpirationInDays(7);

            // 新建删除操作
            // DeleteObjectOperation operation = new DeleteObjectOperation();

            // 新建设置对象 ACL 操作
            // PutObjectAclOperation operation = new PutObjectAclOperation();
            // 设置对象 ACL
            // operation.setCannedAccessControlList(CannedAccessControlList.Private);
            // 或者设置自定义 ACL
            // AccessControlList acl = new AccessControlList();
            // acl.addGrant(new GranteeId("user-id-1"), Permission.Write);
            // acl.addGrant(new GranteeId("user-id-2"), Permission.Read);
            // operation.setAccessControlList(acl);

            // 新建同城冗余转换操作
            // PutObjectDataRedundancyTransitionOperation operation = new PutObjectDataRedundancyTransitionOperation();

            // 新建异步预热操作
            // AsyncWarmupOperation operation = new AsyncWarmupOperation();

            // 新建压缩对象操作
            // CompressObjectOperation operation = new CompressObjectOperation();
            // 设置压缩格式,默认 zip
            // operation.setFormat("zip");
            // 设置是否忽略错误
            // operation.setIgnoreError(true);
            // 设置输出配置
            // CompressOutput output = new CompressOutput();
            // output.setBucketKrn("krn:ksc:ks3:::output-bucket");
            // output.setPrefix("output/");
            // output.setObjectName("archive");
            // operation.setOutput(output);

            // 设置任务操作
            request.setOperation(operation);

            // 初始化任务操作对象的清单
            Manifest manifest = new Manifest();
            // 初始化操作对象的位置
            Location location = new Location();
            // 当 format 为 Spec.FORMAT_BUCKET 时,设置操作对象的桶和前缀
            List<Filter> filters = new ArrayList<>();
            filters.add(new Filter("krn:ksc:ks3:::bucket-1", "my_prefix_1/", "my_prefix_2/"));
            filters.add(new Filter("krn:ksc:ks3:::bucket-2", "my_prefix_1/", "my_prefix_2/"));
            location.setFilters(filters);
            // 设置任务操作对象的位置
            manifest.setLocation(location);

            // 操作对象清单的描述
            Spec spec = new Spec();
            // 设置清单格式,可选值:Spec.FORMAT_BUCKET
            // 设置任务操作对象清单的描述
            manifest.setSpec(spec);
            // 设置任务操作对象清单
            request.setManifest(manifest);

            // 初始化任务报告
            Report report = new Report();
            // 设置报告范围,可选值:Report.SCOPE_FAILED
            report.setReportScope(Report.SCOPE_FAILED);
            // 设置报告存储桶及前缀
            report.setBucketKrn("krn:ksc:ks3:::bucket-1");
            report.setPrefix("report/");
            // 设置报告状态,可选值:true,false
            report.setEnabled(true);
            // 设置任务报告
            request.setReport(report);

            // 发送请求,创建批量处理任务
            CreateJobResult createJobResult = ks3Client.createJob(request);
            System.out.println("JobId: " + createJobResult.getJobId());
        } catch (Ks3ServiceException e) {
            System.out.println("Ks3ServiceException occurred, which means the request was made to KS3, but KS3 returned an error response for some reason.");
            System.out.println("Error Message: " + e.getErrorMessage());
            System.out.println("Error Code: " + e.getErrorCode());
            System.out.println("Request ID: " + e.getRequestId());
        } catch (Ks3ClientException e) {
            System.out.println("Ks3ClientException occurred, which means the client encountered an internal error while trying to communicate with KS3.");
            System.out.println("Error Message: " + e.getMessage());
        } finally {
            // 不再使用时,关闭 Ks3Client
            ks3Client.shutdown();
        }
    }

文档导读
纯净模式常规模式

纯净模式

点击可全屏预览文档内容
文档反馈