API | Operation | Description |
Downloading an object | Downloads an object to the local file system |
// The advanced download API supports checkpoint restart. Therefore, a HEAD request will be sent before the download to obtain the file information.// If you are using a temporary key or accessing with a sub-account, ensure that your permission list includes HeadObject.// Initialize TransferConfig. The default configuration is used here. To customize the configuration, please see the SDK API documentation.TransferConfig transferConfig = new TransferConfig.Builder().build();// Initialize TransferManagerTransferManager transferManager = new TransferManager(cosXmlService,transferConfig);// Bucket name in the format of `BucketName-APPID` (`APPID` is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket.String bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; // Location identifier of the object in the bucket, i.e., the object key// Path of the local directoryString savePathDir = context.getExternalCacheDir().toString();// File name saved locally. If not specified (null), it will be the same as the COS file nameString savedFileName = "exampleobject";Context applicationContext = context.getApplicationContext(); // application// contextCOSXMLDownloadTask cosxmlDownloadTask =transferManager.download(applicationContext,bucket, cosPath, savePathDir, savedFileName);// Set the download progress callbackcosxmlDownloadTask.setCosXmlProgressListener(new CosXmlProgressListener() {@Overridepublic void onProgress(long complete, long target) {// todo Do something to update progress...}});// Set the response callbackcosxmlDownloadTask.setCosXmlResultListener(new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest request, CosXmlResult result) {COSXMLDownloadTask.COSXMLDownloadTaskResult downloadTaskResult =(COSXMLDownloadTask.COSXMLDownloadTaskResult) result;}// If you use the Kotlin language to call this, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called back, that is:// clientException is of type CosXmlClientException? and serviceException is of type CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest request,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});// Set the job status callback to view the job progresscosxmlDownloadTask.setTransferStateListener(new TransferStateListener() {@Overridepublic void onStateChanged(TransferState state) {// todo notify transfer state}});
cosxmlDownloadTask.pause();
cosxmlDownloadTask.resume();
cosxmlDownloadTask.cancel();
// Initialize TransferConfig. The default configuration is used here. To customize the configuration, please see the SDK API documentation.// TransferManager supports checkpoint restart for download. You only need to ensure the consistency of parameters `bucket`, `cosPath`, `savePathDir`, and `savedFileName`.// Then the SDK will resume the download from where interrupted.TransferConfig transferConfig = new TransferConfig.Builder().build();// Initialize TransferManagerTransferManager transferManager = new TransferManager(cosXmlService,transferConfig);// Bucket name in the format of `BucketName-APPID` (`APPID` is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket.String bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; // Location identifier of the object in the bucket, i.e., the object key// Path of the local directoryString savePathDir = context.getExternalCacheDir().toString();// File name saved locally. If not specified (null), it will be the same as the COS file nameString savedFileName = "exampleobject";GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, cosPath, savePathDir, savedFileName);Context applicationContext = context.getApplicationContext(); // application// contextCOSXMLDownloadTask cosxmlDownloadTask =transferManager.download(applicationContext, getObjectRequest);
// The location of the object in the bucket, i.e., the object keyString[] cosPaths = new String[] {"exampleobject1","exampleobject2","exampleobject3",};for (String cosPath : cosPaths) {COSXMLDownloadTask cosxmlDownloadTask =transferManager.download(applicationContext,bucket, cosPath, savePathDir, savedFileName);// Set the response callbackcosxmlDownloadTask.setCosXmlResultListener(new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest request, CosXmlResult result) {COSXMLDownloadTask.COSXMLDownloadTaskResult downloadResult =(COSXMLDownloadTask.COSXMLDownloadTaskResult) result;}// If you use the Kotlin language to call this, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called back, that is:// clientException is of type CosXmlClientException? and serviceException is of type CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest request,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});}
boolean isTruncated = true;String marker = null;try {while (isTruncated) {GetBucketRequest getBucketRequest = new GetBucketRequest(region, bucket, directoryPath);// Configure paginationgetBucketRequest.setMarker(marker);// Configure not to query subdirectoriesgetBucketRequest.setDelimiter("/");GetBucketResult getBucketResult = cosXmlService.getBucket(getBucketRequest);// Batch downloadfor (ListBucket.Contents content : getBucketResult.listBucket.contentsList) {GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, content.key, savePathDir);transferManager.download(context,getObjectRequest);}isTruncated = getBucketResult.listBucket.isTruncated;marker = getBucketResult.listBucket.nextMarker;}} catch (CosXmlServiceException serviceException) {serviceException.printStackTrace();} catch (CosXmlClientException clientException) {clientException.printStackTrace();}
// Initialize TransferConfig. The default configuration is used here. To customize the configuration, please see the SDK API documentation.TransferConfig transferConfig = new TransferConfig.Builder().build();// Initialize TransferManagerCosXmlServiceConfig cosXmlServiceConfig = new CosXmlServiceConfig.Builder().setRegion("ap-guangzhou").builder();// The `CosXmlService` generated by anonymous download does not require passing in the key generator.CosXmlService cosXmlService = new CosXmlService(context, cosXmlServiceConfig);TransferManager transferManager = new TransferManager(cosXmlService, transferConfig);// Bucket name in the format of `BucketName-APPID` (`APPID` is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket.String bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; // Location identifier of the object in the bucket, i.e., the object key// Path of the local directoryString savePathDir = context.getExternalCacheDir().toString();// The filename saved locally. If not specified (null), it will be the same as the COS filename.String savedFileName = "exampleobject";GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, cosPath, savePathDir, savedFileName);Context applicationContext = context.getApplicationContext(); // application// contextCOSXMLDownloadTask cosxmlDownloadTask =transferManager.download(applicationContext, getObjectRequest);
String bucket = "examplebucket-1250000000"; // Bucket, formatted as BucketName-APPIDString cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e., the object keyString savePath = context.getExternalCacheDir().toString(); // Local pathGetObjectRequest getObjectRequest = new GetObjectRequest(bucket, cosPath,savePath);getObjectRequest.setProgressListener(new CosXmlProgressListener() {@Overridepublic void onProgress(long progress, long max) {// todo Do something to update progress...}});cosXmlService.getObjectAsync(getObjectRequest, new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest cosXmlRequest,CosXmlResult cosXmlResult) {GetObjectResult getObjectResult = (GetObjectResult) cosXmlResult;}// If you use the Kotlin language to call this, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called back, that is:// clientException is of type CosXmlClientException? and serviceException is of type CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
Apakah halaman ini membantu?