追加上传(Java SDK V2)

更新时间:
复制 MD 格式

通过 OSS Java SDK V2 调用 AppendObject 接口以追加写的方式上传文件,并可通过 SealAppendObject 接口对追加完成的文件进行封写,封写后文件变为只读状态。

前提条件

  • 已安装 OSS Java SDK V2。使用 Maven 时,请在 pom.xml 中添加依赖 com.aliyun:alibabacloud-oss-v2。

  • 已通过环境变量配置访问凭证。本文示例使用 EnvironmentVariableCredentialsProvider 从环境变量 OSS_ACCESS_KEY_ID 和 OSS_ACCESS_KEY_SECRET 读取凭证。

权限说明

阿里云账号默认拥有全部权限。阿里云账号下的 RAM 用户或 RAM 角色默认没有任何权限,需要阿里云账号或账号管理员授予操作权限。

API

Action

说明

AppendObject

oss:PutObject

以追加方式上传文件。

SealAppendObject

oss:PutObject

封写追加类型的文件,封写后文件进入只读状态,不允许再追加数据。

示例代码

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.transport.BinaryData;

public class AppendObjectSample {
    public static void main(String[] args) throws Exception {
        String region = "cn-hangzhou";
        String bucket = "examplebucket";
        String key = "exampleobject";

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

        try (OSSClient client = clientBuilder.build()) {

            String data = "hello world";

            AppendObjectResult result = client.appendObject(AppendObjectRequest.newBuilder()
                            .bucket(bucket)
                            .key(key)
                            .position(0L)
                            .body(BinaryData.fromString(data))
                    .build());

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

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

封写文件

追加写入完成后,您可以调用 SealAppendObject 接口对文件进行封写。封写后文件变为只读状态,无法继续追加数据。封写请求中的 position 参数必须传入文件当前的长度(即最后一次追加返回的 nextAppendPosition)。

说明

封写功能自 0.2.0-beta 版本起支持,建议使用最新版本的 OSS Java SDK V2。

示例代码

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.transport.BinaryData;

public class SealAppendObjectSample {
    public static void main(String[] args) throws Exception {
        String region = "cn-hangzhou";
        String bucket = "examplebucket";
        String key = "exampleobject";

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

        try (OSSClient client = clientBuilder.build()) {
            // 先追加写入数据
            AppendObjectResult appendResult = client.appendObject(AppendObjectRequest.newBuilder()
                    .bucket(bucket)
                    .key(key)
                    .position(0L)
                    .body(BinaryData.fromString("hello world"))
                    .build());
            System.out.printf("Append success, next append position: %d%n",
                    appendResult.nextAppendPosition());

            // 以当前文件长度为position进行封写
            SealAppendObjectResult sealResult = client.sealAppendObject(SealAppendObjectRequest.newBuilder()
                    .bucket(bucket)
                    .key(key)
                    .position(appendResult.nextAppendPosition().toString())
                    .build());

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

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

相关文档