追加上传

更新时间: 2020-03-24

AppendObjectRequest参数如下:

参数 描述 方法
bucketName 追加上传文件的桶名
key 追加上传文件的名称
file 需要追加的文件(如果没有这个文件则相当于上传文件)
input 需要追加的流数据(跟file类似)
metadata 设置追加对象的元数据
position 设置追加上传的位置 setPosition(Long position)

注意:

追加上传不允许桶开启多版本;

参数position:

  • 如果不设置position,程序会自动获取被追加文件的上传位置,但会耗时在获取位置上;
  • 如果自己设置position,需要准确的输入位置的大小,否则会报错。

以下代码用于追加上传:

// Endpoint以华北三为例,其它Region请按实际情况填写。
String endpoint = "oss.cn-north-3.inspurcloudoss.com";
String accessKey = "<yourAccessKey>";
String secretKey = "<yourSecretKey>";
String bucketName = "<yourBucketName>";
String key = "<yourObjectName>";

//创建OSSClient实例
OSSClientImpl ossClient = new OSSClientImpl(endpoint, accessKey, secretKey);

//文件追加上传
AppendObjectRequest appendObjectRequest = new AppendObjectRequest(bucketName, key, new File("*** need to be appended file ***"));

//position的值为被追加文件的末尾位置
//appendObjectRequest.setPosition(10L);

//流数据追加上传
//AppendObjectRequest appendObjectRequest = new AppendObjectRequest(bucketName, key, new ByteArrayInputStream("*** need to be appended inputStream ***".getBytes()));

//设置元数据
//ObjectMetadata objectMetadata = new ObjectMetadata();
//objectMetadata.setContentLength(1L);
//objectMetadata.setContentEncoding("*** the encoding you need to use ***");
//objectMetadata.setContentType("*** the type you need to use ***");
//appendObjectRequest.setObjectMetadata(objectMetadata);

//追加上传
AppendObjectRequest appendObjectResult = ossClient.appendObject(appendObjectRequest);
System.out.println("Appending object: " + appendObjectResult);