- 产品文档
- > 存储与 CDN
- > 对象存储
- > AWS S3 兼容
- > AWS S3 兼容文档
- > Multipart Upload接口
Multipart Upload接口
Initiate Multipart Upload
兼容说明
x-amz-website-redirect-location,不支持
代码示例
Java:
InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(
bucketName, key);
InitiateMultipartUploadResult initResponse =
s3Client.initiateMultipartUpload(initRequest);
Upload Part
兼容说明
接口和 S3 兼容
代码示例
Java:
UploadPartRequest uploadRequest = new UploadPartRequest()
.withBucketName(bucketName).withKey(key)
.withUploadId(initResponse.getUploadId()).withPartNumber(i)
.withFileOffset(filePosition)
.withFile(file)
.withPartSize(partSize);
s3Client.uploadPart(uploadRequest)
List Parts
兼容说明
对于 S3 中的 encoding 头,如果包含了,也不处理 因为 NOS 没有生命周期的概念,所以响应中不会加入 lifecycle 相关的响应头
代码示例
Java:
ListPartsRequest listPartsRequest = new ListPartsRequest(bucketName,key,initResponse.getUploadId());
//initResponse通过上面的init方式获取
s3Client.listParts(listPartsRequest);
List Multipart Uploads
兼容说明
request 只支持 max-uploads,key-marker 参数 response 中,因为不支持有的请求参数,所以有的元素也不会返回,详细见下表:
resp | S3 | NOS |
---|---|---|
header | ||
UploadIdMarker | √ | x |
NextUploadIdMarker | √ | x |
Encoding-Type | √ | x |
Initiator | √ | x |
CommonPrefixes | √ | x |
CommonPrefixes.Prefix | √ | x |
代码示例
Java:
ListMultipartUploadsRequest listMultipartUploadsRequest = new ListMultipartUploadsRequest(bucketName);
listMultipartUploadsRequest.setMaxUploads(2);
MultipartUploadListing multipartUploadListing = s3Client.listMultipartUploads(listMultipartUploadsRequest);
System.out.println("bucketName : " + multipartUploadListing.getBucketName());
System.out.println(multipartUploadListing.getMaxUploads());
for(MultipartUpload multipartUpload : multipartUploadListing.getMultipartUploads()){
System.out.println(multipartUpload.getInitiated() + " : " + multipartUpload.getUploadId() + " : " +
multipartUpload.getStorageClass());
}
Complete Multipart Upload
兼容说明
除了版本控制和加密,NOS 完全兼容 S3
代码示例
Java:
CompleteMultipartUploadRequest compRequest = new
CompleteMultipartUploadRequest(bucketName,
key,
initResponse.getUploadId(),//为initiateMultipartUpload的返回值
partETags); //partEtags通过上述ListParts构造
Abort Multipart Upload
兼容说明
接口和 S3 兼容
代码示例
Java:
s3Client.abortMultipartUpload(new AbortMultipartUploadRequest(
bucketName, key, initResponse.getUploadId()));//initResponse为initiateMultipartUpload的返回值
完整的分块上传示例
Java:
List<PartETag> partETags = new ArrayList<>();
// Step 1: Initialize.
InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(
bucketName, key);
InitiateMultipartUploadResult initResponse =
s3Client.initiateMultipartUpload(initRequest);
File file = new File(filePath);
long contentLength = file.length();
long partSize = 5 * 1024 * 1024; // Set part size to 5 MB.
try {
// Step 2: Upload parts.
long filePosition = 0;
for (int i = 1; filePosition < contentLength; i++) {
// Last part can be less than 5 MB. Adjust part size.
partSize = Math.min(partSize, (contentLength - filePosition));
// Create request to upload a part.
UploadPartRequest uploadRequest = new UploadPartRequest()
.withBucketName(bucketName).withKey(key)
.withUploadId(initResponse.getUploadId()).withPartNumber(i)
.withFileOffset(filePosition)
.withFile(file)
.withPartSize(partSize);
// Upload part and add response to our list.
partETags.add(s3Client.uploadPart(uploadRequest).getPartETag());
ListPartsRequest listPartsRequest = new ListPartsRequest(bucketName,key,initResponse.getUploadId());
s3Client.listParts(listPartsRequest);
filePosition += partSize;
}
// Step 3: Complete.
CompleteMultipartUploadRequest compRequest = new
CompleteMultipartUploadRequest(bucketName,
key,
initResponse.getUploadId(),
partETags);
s3Client.completeMultipartUpload(compRequest);
} catch (Exception e) {
//error,abort
s3Client.abortMultipartUpload(new AbortMultipartUploadRequest(
bucketName, key, initResponse.getUploadId()));
}