cos:GetObject. For more authorization information, see CAM-supported business APIs.cos:GetObject and cos:HeadObject. For more authorization information, see CAM-supported business APIs.Feature Name | Description | Example code |
Advanced API | The advanced interface supports pausing, resuming, and canceling download requests, and also supports the resumable download feature. | |
Simple API | The GET Object API can download objects to local files but does not support the resumable download feature. |
// Initialize TransferConfig. The default configuration is used here. If you need to customize it, see the SDK API documentation.// By default, files larger than or equal to 2 MB are automatically uploaded in parts, with a part size of -1 MB. You can modify the part size threshold using the following code.TransferConfig transferConfig = new TransferConfig.Builder() // Set the minimum object size to enable multipart upload. The default is 2 MB. .setDivisionForUpload(2097152) // Set the part size for multipart upload. The default is 1 MB. .setSliceSizeForUpload(1048576) // Set whether to force simple upload and disable multipart upload. .setForceSimpleUpload(false) .build();// Initialize TransferManagerTransferManager transferManager = new TransferManager(cosXmlService,transferConfig);
// A bucket name consists of bucketname-appid. The appid must be included. You can view bucket names in the COS console. https://console.tencentcloud.com/cos5/bucketString bucket = "examplebucket-1250000000";String cosPath = "exampleobject.txt"; // The location identifier of the object in the bucket, also known as the object key.// Local directory pathString savePathDir = context.getExternalCacheDir().toString();// The file name saved locally. If left blank (null), it will be the same as the file name on COS.String savedFileName = "exampleobject.txt";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 result callbackcosxmlDownloadTask.setCosXmlResultListener(new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest request, CosXmlResult result) {COSXMLDownloadTask.COSXMLDownloadTaskResult downloadTaskResult =(COSXMLDownloadTask.COSXMLDownloadTaskResult) result;}// If you call the API using kotlin, note that the exception in the callback method must be nullable. Otherwise, the onFail method will not be called, as follows:// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest request,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});// Set the task status callback to view the task progress.cosxmlDownloadTask.setTransferStateListener(new TransferStateListener() {@Overridepublic void onStateChanged(TransferState state) {// todo notify transfer state}});
cosxmlDownloadTask.pause();
cosxmlDownloadTask.resume();
cosxmlDownloadTask.cancel();
// TransferManager supports resumable downloads. You only need to ensure that the bucket, cosPath, savePathDir, and savedFileName// If the parameters remain the same, the SDK will resume the download from the last position.// A bucket name consists of bucketname-appid. The appid must be included. You can view bucket names in the COS console. https://console.tencentcloud.com/cos5/bucketString bucket = "examplebucket-1250000000";String cosPath = "exampleobject.txt"; // The location identifier of the object in the bucket, also known as the object key.// Local directory pathString savePathDir = context.getExternalCacheDir().toString();// The file name saved locally. If left blank (null), it will be the same as the file name on COS.String savedFileName = "exampleobject.txt";GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, cosPath, savePathDir, savedFileName);Context applicationContext = context.getApplicationContext(); // application// contextCOSXMLDownloadTask cosxmlDownloadTask =transferManager.download(applicationContext, getObjectRequest);
// The location identifier of the object in the bucket, also known as the object key.String[] cosPaths = new String[] {"exampleobject1.txt","exampleobject2.txt","exampleobject3.txt",};for (String cosPath : cosPaths) {COSXMLDownloadTask cosxmlDownloadTask =transferManager.download(applicationContext,bucket, cosPath, savePathDir, savedFileName);// Set the result callback.cosxmlDownloadTask.setCosXmlResultListener(new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest request, CosXmlResult result) {COSXMLDownloadTask.COSXMLDownloadTaskResult downloadResult =(COSXMLDownloadTask.COSXMLDownloadTaskResult) result;}// If you call the API using kotlin, note that the exception in the callback method must be nullable. Otherwise, the onFail method will not be called, as follows:// The type of clientException is CosXmlClientException?, and the type of serviceException is 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);// Set pagination information.getBucketRequest.setMarker(marker);// Do not query subdirectories.getBucketRequest.setDelimiter("/");GetBucketResult getBucketResult = cosXmlService.getBucket(getBucketRequest);// Batch download.for (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. If you need to customize it, see the SDK API documentation.TransferConfig transferConfig = new TransferConfig.Builder().build();// Initialize TransferManagerCosXmlServiceConfig cosXmlServiceConfig = new CosXmlServiceConfig.Builder().setRegion("ap-guangzhou").builder();// When generating a CosXmlService for anonymous download, you do not need to provide a key generator.CosXmlService cosXmlService = new CosXmlService(context, cosXmlServiceConfig);TransferManager transferManager = new TransferManager(cosXmlService, transferConfig);// A bucket name consists of bucketname-appid. The appid must be included. You can view bucket names in the COS console. https://console.tencentcloud.com/cos5/bucketString bucket = "examplebucket-1250000000";String cosPath = "exampleobject.txt"; // The location identifier of the object in the bucket, also known as the object key.// Local directory pathString savePathDir = context.getExternalCacheDir().toString();// The file name saved locally. If left blank (null), it will be the same as the file name on COS.String savedFileName = "exampleobject.txt";GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, cosPath, savePathDir, savedFileName);Context applicationContext = context.getApplicationContext(); // application// contextCOSXMLDownloadTask cosxmlDownloadTask =transferManager.download(applicationContext, getObjectRequest);
String bucket = "examplebucket-1250000000"; // Bucket name. Format: BucketName-APPIDString cosPath = "exampleobject.txt"; // The location identifier of the object in the bucket, also known as the object key.String 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 call the API using kotlin, note that the exception in the callback method must be nullable. Otherwise, the onFail method will not be called, as follows:// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
Esta página foi útil?
Você também pode entrar em contato com a Equipe de vendas ou Enviar um tíquete em caso de ajuda.
comentários