OSS提供的分片上傳(Multipart Upload)功能,將要上傳的較大文件(Object)分成多個分片(Part)來分別上傳,上傳完成后再調用CompleteMultipartUpload接口將這些Part組合成一個Object來達到斷點續傳的效果。
注意事項
本文以本地文件分片上傳為例進行介紹,如果您需要實現網絡流或者數據流分片上傳,請參見數據流分片上傳。
本文以華東1(杭州)外網Endpoint為例。如果您希望通過與OSS同地域的其他阿里云產品訪問OSS,請使用內網Endpoint。關于OSS支持的Region與Endpoint的對應關系,請參見OSS訪問域名、數據中心、開放端口。
本文以從環境變量讀取訪問憑證為例。如何配置訪問憑證,請參見Java配置訪問憑證。
本文以OSS域名新建OSSClient為例。如果您希望通過自定義域名、STS等方式新建OSSClient,請參見新建OSSClient。
要分片上傳,您必須有
oss:PutObject
權限。具體操作,請參見為RAM用戶授權自定義的權限策略。
分片上傳流程
分片上傳(Multipart Upload)分為以下三個步驟:
初始化一個分片上傳事件。
調用ossClient.initiateMultipartUpload方法返回OSS創建的全局唯一的uploadId。
上傳分片。
調用ossClient.uploadPart方法上傳分片數據。
說明對于同一個uploadId,分片號(PartNumber)標識了該分片在整個文件內的相對位置。如果使用同一個分片號上傳了新的數據,則OSS上該分片已有的數據將會被覆蓋。
OSS將收到的分片數據的MD5值放在ETag頭內返回給用戶。
OSS計算上傳數據的MD5值,并與SDK計算的MD5值比較,如果不一致則返回InvalidDigest錯誤碼。
完成分片上傳。
所有分片上傳完成后,調用ossClient.completeMultipartUpload方法將所有分片合并成完整的文件。
關于分片上傳的說明及適用場景,請參見分片上傳。
分片上傳完整示例
以下通過一個完整的示例對分片上傳的流程進行逐步解析:
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.internal.Mimetypes;
import com.aliyun.oss.model.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class Demo {
public static void main(String[] args) throws Exception {
// Endpoint以華東1(杭州)為例,其它Region請按實際情況填寫。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 從環境變量中獲取訪問憑證。運行本代碼示例之前,請確保已設置環境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// 填寫Bucket名稱,例如examplebucket。
String bucketName = "examplebucket";
// 填寫Object完整路徑,例如exampledir/exampleobject.txt。Object完整路徑中不能包含Bucket名稱。
String objectName = "exampledir/exampleobject.txt";
// 待上傳本地文件路徑。
String filePath = "D:\\localpath\\examplefile.txt";
// 填寫Bucket所在地域。以華東1(杭州)為例,Region填寫為cn-hangzhou。
String region = "cn-hangzhou";
// 創建OSSClient實例。
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// 創建InitiateMultipartUploadRequest對象。
InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, objectName);
// 如果需要在初始化分片時設置請求頭,請參考以下示例代碼。
ObjectMetadata metadata = new ObjectMetadata();
// metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
// 指定該Object的網頁緩存行為。
// metadata.setCacheControl("no-cache");
// 指定該Object被下載時的名稱。
// metadata.setContentDisposition("attachment;filename=oss_MultipartUpload.txt");
// 指定該Object的內容編碼格式。
// metadata.setContentEncoding(OSSConstants.DEFAULT_CHARSET_NAME);
// 指定初始化分片上傳時是否覆蓋同名Object。此處設置為true,表示禁止覆蓋同名Object。
// metadata.setHeader("x-oss-forbid-overwrite", "true");
// 指定上傳該Object的每個part時使用的服務器端加密方式。
// metadata.setHeader(OSSHeaders.OSS_SERVER_SIDE_ENCRYPTION, ObjectMetadata.KMS_SERVER_SIDE_ENCRYPTION);
// 指定Object的加密算法。如果未指定此選項,表明Object使用AES256加密算法。
// metadata.setHeader(OSSHeaders.OSS_SERVER_SIDE_DATA_ENCRYPTION, ObjectMetadata.KMS_SERVER_SIDE_ENCRYPTION);
// 指定KMS托管的用戶主密鑰。
// metadata.setHeader(OSSHeaders.OSS_SERVER_SIDE_ENCRYPTION_KEY_ID, "9468da86-3509-4f8d-a61e-6eab1eac****");
// 指定Object的存儲類型。
// metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard);
// 指定Object的對象標簽,可同時設置多個標簽。
// metadata.setHeader(OSSHeaders.OSS_TAGGING, "a:1");
// request.setObjectMetadata(metadata);
// 根據文件自動設置ContentType。如果不設置,ContentType默認值為application/oct-srream。
if (metadata.getContentType() == null) {
metadata.setContentType(Mimetypes.getInstance().getMimetype(new File(filePath), objectName));
}
// 初始化分片。
InitiateMultipartUploadResult upresult = ossClient.initiateMultipartUpload(request);
// 返回uploadId。
String uploadId = upresult.getUploadId();
// 根據uploadId執行取消分片上傳事件或者列舉已上傳分片的操作。
// 如果您需要根據uploadId執行取消分片上傳事件的操作,您需要在調用InitiateMultipartUpload完成初始化分片之后獲取uploadId。
// 如果您需要根據uploadId執行列舉已上傳分片的操作,您需要在調用InitiateMultipartUpload完成初始化分片之后,且在調用CompleteMultipartUpload完成分片上傳之前獲取uploadId。
// System.out.println(uploadId);
// partETags是PartETag的集合。PartETag由分片的ETag和分片號組成。
List<PartETag> partETags = new ArrayList<PartETag>();
// 每個分片的大小,用于計算文件有多少個分片。單位為字節。
final long partSize = 1 * 1024 * 1024L; //1 MB。
// 根據上傳的數據大小計算分片數。以本地文件為例,說明如何通過File.length()獲取上傳數據的大小。
final File sampleFile = new File(filePath);
long fileLength = sampleFile.length();
int partCount = (int) (fileLength / partSize);
if (fileLength % partSize != 0) {
partCount++;
}
// 遍歷分片上傳。
for (int i = 0; i < partCount; i++) {
long startPos = i * partSize;
long curPartSize = (i + 1 == partCount) ? (fileLength - startPos) : partSize;
UploadPartRequest uploadPartRequest = new UploadPartRequest();
uploadPartRequest.setBucketName(bucketName);
uploadPartRequest.setKey(objectName);
uploadPartRequest.setUploadId(uploadId);
// 設置上傳的分片流。
// 以本地文件為例說明如何創建FIleInputstream,并通過InputStream.skip()方法跳過指定數據。
InputStream instream = new FileInputStream(sampleFile);
instream.skip(startPos);
uploadPartRequest.setInputStream(instream);
// 設置分片大小。除了最后一個分片沒有大小限制,其他的分片最小為100 KB。
uploadPartRequest.setPartSize(curPartSize);
// 設置分片號。每一個上傳的分片都有一個分片號,取值范圍是1~10000,如果超出此范圍,OSS將返回InvalidArgument錯誤碼。
uploadPartRequest.setPartNumber( i + 1);
// 每個分片不需要按順序上傳,甚至可以在不同客戶端上傳,OSS會按照分片號排序組成完整的文件。
UploadPartResult uploadPartResult = ossClient.uploadPart(uploadPartRequest);
// 每次上傳分片之后,OSS的返回結果包含PartETag。PartETag將被保存在partETags中。
partETags.add(uploadPartResult.getPartETag());
}
// 創建CompleteMultipartUploadRequest對象。
// 在執行完成分片上傳操作時,需要提供所有有效的partETags。OSS收到提交的partETags后,會逐一驗證每個分片的有效性。當所有的數據分片驗證通過后,OSS將把這些分片組合成一個完整的文件。
CompleteMultipartUploadRequest completeMultipartUploadRequest =
new CompleteMultipartUploadRequest(bucketName, objectName, uploadId, partETags);
// 如果需要在完成分片上傳的同時設置文件訪問權限,請參考以下示例代碼。
// completeMultipartUploadRequest.setObjectACL(CannedAccessControlList.Private);
// 指定是否列舉當前UploadId已上傳的所有Part。僅在Java SDK為3.14.0及以上版本時,支持通過服務端List分片數據來合并完整文件時,將CompleteMultipartUploadRequest中的partETags設置為null。
// Map<String, String> headers = new HashMap<String, String>();
// 如果指定了x-oss-complete-all:yes,則OSS會列舉當前UploadId已上傳的所有Part,然后按照PartNumber的序號排序并執行CompleteMultipartUpload操作。
// 如果指定了x-oss-complete-all:yes,則不允許繼續指定body,否則報錯。
// headers.put("x-oss-complete-all","yes");
// completeMultipartUploadRequest.setHeaders(headers);
// 完成分片上傳。
CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(completeMultipartUploadRequest);
System.out.println(completeMultipartUploadResult.getETag());
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
取消分片上傳事件
您可以調用ossClient.abortMultipartUpload方法來取消分片上傳事件。當一個分片上傳事件被取消后,無法再使用該uploadId進行任何操作,已上傳的分片數據會被刪除。
以下代碼用于取消分片上傳事件。
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.*;
public class Demo {
public static void main(String[] args) throws Exception {
// Endpoint以華東1(杭州)為例,其它Region請按實際情況填寫。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 從環境變量中獲取訪問憑證。運行本代碼示例之前,請確保已設置環境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// 填寫Bucket名稱,例如examplebucket。
String bucketName = "examplebucket";
// 填寫Object完整路徑,例如exampledir/exampleobject.txt。Object完整路徑中不能包含Bucket名稱。
String objectName = "exampledir/exampleobject.txt";
// 填寫uploadId,例如0004B999EF518A1FE585B0C9360D****。uploadId來源于調用InitiateMultipartUpload完成初始化分片之后的返回結果。
String uploadId = "0004B999EF518A1FE585B0C9360D****";
// 填寫Bucket所在地域。以華東1(杭州)為例,Region填寫為cn-hangzhou。
String region = "cn-hangzhou";
// 創建OSSClient實例。
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// 取消分片上傳。
AbortMultipartUploadRequest abortMultipartUploadRequest =
new AbortMultipartUploadRequest(bucketName, objectName, uploadId);
ossClient.abortMultipartUpload(abortMultipartUploadRequest);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
列舉已上傳的分片
調用ossClient.listParts方法列舉出指定uploadId下所有已經上傳成功的分片。
簡單列舉已上傳的分片
以下代碼用于簡單列舉已上傳的分片:
import com.aliyun.oss.ClientException; import com.aliyun.oss.OSS; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.OSSException; import com.aliyun.oss.model.*; public class Demo { public static void main(String[] args) throws Exception { // Endpoint以華東1(杭州)為例,其它Region請按實際情況填寫。 String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // 從環境變量中獲取訪問憑證。運行本代碼示例之前,請確保已設置環境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。 EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // 填寫Bucket名稱,例如examplebucket。 String bucketName = "examplebucket"; // 填寫Object完整路徑,例如exampledir/exampleobject.txt。Object完整路徑中不能包含Bucket名稱。 String objectName = "exampledir/exampleobject.txt"; // 填寫uploadId,例如0004B999EF518A1FE585B0C9360D****。uploadId來源于調用InitiateMultipartUpload完成初始化分片之后,且在調用CompleteMultipartUpload完成分片上傳之前的返回結果。 String uploadId = "0004B999EF518A1FE585B0C9360D****"; // 填寫Bucket所在地域。以華東1(杭州)為例,Region填寫為cn-hangzhou。 String region = "cn-hangzhou"; // 創建OSSClient實例。 ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration(); clientBuilderConfiguration.setSignatureVersion(SignVersion.V4); OSS ossClient = OSSClientBuilder.create() .endpoint(endpoint) .credentialsProvider(credentialsProvider) .clientConfiguration(clientBuilderConfiguration) .region(region) .build(); try { // 列舉已上傳的分片。 ListPartsRequest listPartsRequest = new ListPartsRequest(bucketName, objectName, uploadId); // 設置uploadId。 //listPartsRequest.setUploadId(uploadId); // 設置分頁時每一頁中分片數量為100個。默認列舉1000個分片。 listPartsRequest.setMaxParts(100); // 指定List的起始位置。只有分片號大于此參數值的分片會被列舉。 listPartsRequest.setPartNumberMarker(2); PartListing partListing = ossClient.listParts(listPartsRequest); for (PartSummary part : partListing.getParts()) { // 獲取分片號。 System.out.println(part.getPartNumber()); // 獲取分片數據大小。 System.out.println(part.getSize()); // 獲取分片的ETag。 System.out.println(part.getETag()); // 獲取分片的最后修改時間。 System.out.println(part.getLastModified()); } } catch (OSSException oe) { System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); System.out.println("Error Message:" + oe.getErrorMessage()); System.out.println("Error Code:" + oe.getErrorCode()); System.out.println("Request ID:" + oe.getRequestId()); System.out.println("Host ID:" + oe.getHostId()); } catch (ClientException ce) { System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network."); System.out.println("Error Message:" + ce.getMessage()); } finally { if (ossClient != null) { ossClient.shutdown(); } } } }
列舉所有已上傳的分片
默認情況下,listParts只能一次列舉1000個分片。當分片數量大于1000時,請使用以下代碼列舉所有已上傳的分片。
import com.aliyun.oss.ClientException; import com.aliyun.oss.OSS; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.OSSException; import com.aliyun.oss.model.*; public class Demo { public static void main(String[] args) throws Exception { // Endpoint以華東1(杭州)為例,其它Region請按實際情況填寫。 String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // 從環境變量中獲取訪問憑證。運行本代碼示例之前,請確保已設置環境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。 EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // 填寫Bucket名稱,例如examplebucket。 String bucketName = "examplebucket"; // 填寫Object完整路徑,例如exampledir/exampleobject.txt。Object完整路徑中不能包含Bucket名稱。 String objectName = "exampledir/exampleobject.txt"; // 填寫uploadId,例如0004B999EF518A1FE585B0C9360D****。uploadId來源于調用InitiateMultipartUpload完成初始化分片之后,且在調用CompleteMultipartUpload完成分片上傳之前的返回結果。 String uploadId = "0004B999EF518A1FE585B0C9360D****"; // 填寫Bucket所在地域。以華東1(杭州)為例,Region填寫為cn-hangzhou。 String region = "cn-hangzhou"; // 創建OSSClient實例。 ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration(); clientBuilderConfiguration.setSignatureVersion(SignVersion.V4); OSS ossClient = OSSClientBuilder.create() .endpoint(endpoint) .credentialsProvider(credentialsProvider) .clientConfiguration(clientBuilderConfiguration) .region(region) .build(); try { // 列舉所有已上傳的分片。 PartListing partListing; ListPartsRequest listPartsRequest = new ListPartsRequest(bucketName, objectName, uploadId); do { partListing = ossClient.listParts(listPartsRequest); for (PartSummary part : partListing.getParts()) { // 獲取分片號。 System.out.println(part.getPartNumber()); // 獲取分片數據大小。 System.out.println(part.getSize()); // 獲取分片的ETag。 System.out.println(part.getETag()); // 獲取分片的最后修改時間。 System.out.println(part.getLastModified()); } // 指定List的起始位置,只有分片號大于此參數值的分片會被列出。 listPartsRequest.setPartNumberMarker(partListing.getNextPartNumberMarker()); } while (partListing.isTruncated()); } catch (OSSException oe) { System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); System.out.println("Error Message:" + oe.getErrorMessage()); System.out.println("Error Code:" + oe.getErrorCode()); System.out.println("Request ID:" + oe.getRequestId()); System.out.println("Host ID:" + oe.getHostId()); } catch (ClientException ce) { System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network."); System.out.println("Error Message:" + ce.getMessage()); } finally { if (ossClient != null) { ossClient.shutdown(); } } } }
分頁列舉所有已上傳的分片
以下代碼用于指定每頁分片的數量、分頁列舉所有分片。
import com.aliyun.oss.ClientException; import com.aliyun.oss.OSS; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.OSSException; import com.aliyun.oss.model.*; public class Demo { public static void main(String[] args) throws Exception { // Endpoint以華東1(杭州)為例,其它Region請按實際情況填寫。 String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // 從環境變量中獲取訪問憑證。運行本代碼示例之前,請確保已設置環境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。 EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // 填寫Bucket名稱,例如examplebucket。 String bucketName = "examplebucket"; // 填寫Object完整路徑,例如exampledir/exampleobject.txt。Object完整路徑中不能包含Bucket名稱。 String objectName = "exampledir/exampleobject.txt"; // 填寫uploadId,例如0004B999EF518A1FE585B0C9360D****。uploadId來源于調用InitiateMultipartUpload完成初始化分片之后,且在調用CompleteMultipartUpload完成分片上傳之前的返回結果。 String uploadId = "0004B999EF518A1FE585B0C9360D****"; // 填寫Bucket所在地域。以華東1(杭州)為例,Region填寫為cn-hangzhou。 String region = "cn-hangzhou"; // 創建OSSClient實例。 ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration(); clientBuilderConfiguration.setSignatureVersion(SignVersion.V4); OSS ossClient = OSSClientBuilder.create() .endpoint(endpoint) .credentialsProvider(credentialsProvider) .clientConfiguration(clientBuilderConfiguration) .region(region) .build(); try { // 分頁列舉已上傳的分片。 PartListing partListing; ListPartsRequest listPartsRequest = new ListPartsRequest(bucketName, objectName, uploadId); // 設置分頁列舉時,每頁列舉100個分片。 listPartsRequest.setMaxParts(100); do { partListing = ossClient.listParts(listPartsRequest); for (PartSummary part : partListing.getParts()) { // 獲取分片號。 System.out.println(part.getPartNumber()); // 獲取分片數據大小。 System.out.println(part.getSize()); // 獲取分片的ETag。 System.out.println(part.getETag()); // 獲取分片的最后修改時間。 System.out.println(part.getLastModified()); } listPartsRequest.setPartNumberMarker(partListing.getNextPartNumberMarker()); } while (partListing.isTruncated()); } catch (OSSException oe) { System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); System.out.println("Error Message:" + oe.getErrorMessage()); System.out.println("Error Code:" + oe.getErrorCode()); System.out.println("Request ID:" + oe.getRequestId()); System.out.println("Host ID:" + oe.getHostId()); } catch (ClientException ce) { System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network."); System.out.println("Error Message:" + ce.getMessage()); } finally { if (ossClient != null) { ossClient.shutdown(); } } } }
列舉分片上傳事件
調用ossClient.listMultipartUploads方法列舉出所有執行中的分片上傳事件,即已初始化但尚未完成或已取消的分片上傳事件。可設置的參數請參見下表。
參數 | 作用 | 設置方法 |
prefix | 限定返回的文件名稱必須以指定的prefix作為前綴。注意使用prefix查詢時,返回的文件名稱中仍會包含prefix。 | ListMultipartUploadsRequest.setPrefix(String prefix) |
delimiter | 用于對文件名稱進行分組的一個字符。所有名稱包含指定的前綴且第一次出現delimiter字符之間的文件作為一組元素。 | ListMultipartUploadsRequest.setDelimiter(String delimiter) |
maxUploads | 限定此次返回分片上傳事件的最大個數,默認值和最大值均為1000。 | ListMultipartUploadsRequest.setMaxUploads(Integer maxUploads) |
keyMarker | 所有文件名稱的字典序大于keyMarker參數值的分片上傳事件。與uploadIdMarker參數一同使用,用于指定返回結果的起始位置。 | ListMultipartUploadsRequest.setKeyMarker(String keyMarker) |
uploadIdMarker | 與keyMarker參數一同使用,用于指定返回結果的起始位置。 如果未設置keyMarker參數,則此參數無效。如果設置了keyMarker參數,則查詢結果中包含:
| ListMultipartUploadsRequest.setUploadIdMarker(String uploadIdMarker) |
簡單列舉分片上傳事件
以下代碼用于列舉分片上傳事件。
import com.aliyun.oss.ClientException; import com.aliyun.oss.OSS; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.OSSException; import com.aliyun.oss.model.*; public class Demo { public static void main(String[] args) throws Exception { // Endpoint以華東1(杭州)為例,其它Region請按實際情況填寫。 String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // 從環境變量中獲取訪問憑證。運行本代碼示例之前,請確保已設置環境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。 EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // 填寫Bucket名稱,例如examplebucket。 String bucketName = "examplebucket"; // 填寫Bucket所在地域。以華東1(杭州)為例,Region填寫為cn-hangzhou。 String region = "cn-hangzhou"; // 創建OSSClient實例。 ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration(); clientBuilderConfiguration.setSignatureVersion(SignVersion.V4); OSS ossClient = OSSClientBuilder.create() .endpoint(endpoint) .credentialsProvider(credentialsProvider) .clientConfiguration(clientBuilderConfiguration) .region(region) .build(); try { // 列舉分片上傳事件。默認列舉1000個分片。 ListMultipartUploadsRequest listMultipartUploadsRequest = new ListMultipartUploadsRequest(bucketName); MultipartUploadListing multipartUploadListing = ossClient.listMultipartUploads(listMultipartUploadsRequest); for (MultipartUpload multipartUpload : multipartUploadListing.getMultipartUploads()) { // 獲取uploadId。 System.out.println(multipartUpload.getUploadId()); // 獲取Key。 System.out.println(multipartUpload.getKey()); // 獲取分片上傳的初始化時間。 System.out.println(multipartUpload.getInitiated()); } } catch (OSSException oe) { System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); System.out.println("Error Message:" + oe.getErrorMessage()); System.out.println("Error Code:" + oe.getErrorCode()); System.out.println("Request ID:" + oe.getRequestId()); System.out.println("Host ID:" + oe.getHostId()); } catch (ClientException ce) { System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network."); System.out.println("Error Message:" + ce.getMessage()); } finally { if (ossClient != null) { ossClient.shutdown(); } } } }
返回結果中isTruncated為true,返回nextKeyMarker和nextUploadIdMarker作為下次讀取的起點。如果無法一次性獲取所有的上傳事件,可以采用分頁列舉的方式。
列舉全部分片上傳事件
默認情況下,listMultipartUploads只能一次列舉1000個分片。當分片數量大于1000時,請使用以下代碼列舉全部分片上傳事件。
import com.aliyun.oss.ClientException; import com.aliyun.oss.OSS; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.OSSException; import com.aliyun.oss.model.*; public class Demo { public static void main(String[] args) throws Exception { // Endpoint以華東1(杭州)為例,其它Region請按實際情況填寫。 String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // 從環境變量中獲取訪問憑證。運行本代碼示例之前,請確保已設置環境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。 EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // 填寫Bucket名稱,例如examplebucket。 String bucketName = "examplebucket"; // 填寫Bucket所在地域。以華東1(杭州)為例,Region填寫為cn-hangzhou。 String region = "cn-hangzhou"; // 創建OSSClient實例。 ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration(); clientBuilderConfiguration.setSignatureVersion(SignVersion.V4); OSS ossClient = OSSClientBuilder.create() .endpoint(endpoint) .credentialsProvider(credentialsProvider) .clientConfiguration(clientBuilderConfiguration) .region(region) .build(); try { // 列舉分片上傳事件。 MultipartUploadListing multipartUploadListing; ListMultipartUploadsRequest listMultipartUploadsRequest = new ListMultipartUploadsRequest(bucketName); do { multipartUploadListing = ossClient.listMultipartUploads(listMultipartUploadsRequest); for (MultipartUpload multipartUpload : multipartUploadListing.getMultipartUploads()) { // 獲取uploadId。 System.out.println(multipartUpload.getUploadId()); // 獲取文件名稱。 System.out.println(multipartUpload.getKey()); // 獲取分片上傳的初始化時間。 System.out.println(multipartUpload.getInitiated()); } listMultipartUploadsRequest.setKeyMarker(multipartUploadListing.getNextKeyMarker()); listMultipartUploadsRequest.setUploadIdMarker(multipartUploadListing.getNextUploadIdMarker()); } while (multipartUploadListing.isTruncated()); } catch (OSSException oe) { System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); System.out.println("Error Message:" + oe.getErrorMessage()); System.out.println("Error Code:" + oe.getErrorCode()); System.out.println("Request ID:" + oe.getRequestId()); System.out.println("Host ID:" + oe.getHostId()); } catch (ClientException ce) { System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network."); System.out.println("Error Message:" + ce.getMessage()); } finally { if (ossClient != null) { ossClient.shutdown(); } } } }
分頁列舉全部上傳事件
以下代碼用于分頁列舉所有上傳事件。
import com.aliyun.oss.ClientException; import com.aliyun.oss.OSS; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.OSSException; import com.aliyun.oss.model.*; public class Demo { public static void main(String[] args) throws Exception { // Endpoint以華東1(杭州)為例,其它Region請按實際情況填寫。 String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // 從環境變量中獲取訪問憑證。運行本代碼示例之前,請確保已設置環境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。 EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // 填寫Bucket名稱,例如examplebucket。 String bucketName = "examplebucket"; // 填寫Bucket所在地域。以華東1(杭州)為例,Region填寫為cn-hangzhou。 String region = "cn-hangzhou"; // 創建OSSClient實例。 ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration(); clientBuilderConfiguration.setSignatureVersion(SignVersion.V4); OSS ossClient = OSSClientBuilder.create() .endpoint(endpoint) .credentialsProvider(credentialsProvider) .clientConfiguration(clientBuilderConfiguration) .region(region) .build(); try { // 列舉分片上傳事件。 MultipartUploadListing multipartUploadListing; ListMultipartUploadsRequest listMultipartUploadsRequest = new ListMultipartUploadsRequest(bucketName); // 設置每頁列舉的分片上傳事件個數。 listMultipartUploadsRequest.setMaxUploads(50); do { multipartUploadListing = ossClient.listMultipartUploads(listMultipartUploadsRequest); for (MultipartUpload multipartUpload : multipartUploadListing.getMultipartUploads()) { // 獲取uploadId。 System.out.println(multipartUpload.getUploadId()); // 獲取Key。 System.out.println(multipartUpload.getKey()); // 獲取分片上傳的初始化時間。 System.out.println(multipartUpload.getInitiated()); } listMultipartUploadsRequest.setKeyMarker(multipartUploadListing.getNextKeyMarker()); listMultipartUploadsRequest.setUploadIdMarker(multipartUploadListing.getNextUploadIdMarker()); } while (multipartUploadListing.isTruncated()); } catch (OSSException oe) { System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); System.out.println("Error Message:" + oe.getErrorMessage()); System.out.println("Error Code:" + oe.getErrorCode()); System.out.println("Request ID:" + oe.getRequestId()); System.out.println("Host ID:" + oe.getHostId()); } catch (ClientException ce) { System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network."); System.out.println("Error Message:" + ce.getMessage()); } finally { if (ossClient != null) { ossClient.shutdown(); } } } }
相關文檔
關于分片上傳的完整示例代碼,請參見GitHub示例。
分片上傳的完整實現涉及三個API接口,詳情如下:
關于初始化分片上傳事件的API接口說明,請參見InitiateMultipartUpload。
關于分片上傳Part的API接口說明,請參見UploadPart。
關于完成分片上傳的API接口說明,請參見CompleteMultipartUpload。
關于取消分片上傳事件的API接口說明,請參見AbortMultipartUpload。
關于列舉已上傳分片的API接口說明,請參見ListParts。
關于列舉所有執行中的分片上傳事件(即已初始化但尚未完成或已取消的分片上傳事件)的API接口說明,請參見ListMultipartUploads。