|
| 1 | +package com.qcloud.cos.demo; |
| 2 | + |
| 3 | +import com.qcloud.cos.COSClient; |
| 4 | +import com.qcloud.cos.ClientConfig; |
| 5 | +import com.qcloud.cos.auth.BasicCOSCredentials; |
| 6 | +import com.qcloud.cos.auth.COSCredentials; |
| 7 | +import com.qcloud.cos.endpoint.UserSpecifiedEndpointBuilder; |
| 8 | +import com.qcloud.cos.exception.CosClientException; |
| 9 | +import com.qcloud.cos.exception.CosServiceException; |
| 10 | +import com.qcloud.cos.http.HttpProtocol; |
| 11 | +import com.qcloud.cos.model.GetObjectRequest; |
| 12 | +import com.qcloud.cos.model.PutObjectRequest; |
| 13 | +import com.qcloud.cos.model.UploadResult; |
| 14 | +import com.qcloud.cos.region.Region; |
| 15 | +import com.qcloud.cos.transfer.Download; |
| 16 | +import com.qcloud.cos.transfer.TransferManager; |
| 17 | +import com.qcloud.cos.transfer.Upload; |
| 18 | + |
| 19 | +import java.util.concurrent.ExecutorService; |
| 20 | +import java.util.concurrent.Executors; |
| 21 | +import java.io.File; |
| 22 | + |
| 23 | +public class UserSpecifiedEndpointDemo { |
| 24 | + private static String bucketName = "mybucket-12500000000"; |
| 25 | + |
| 26 | + private static String key = "exampleobject"; |
| 27 | + |
| 28 | + private static TransferManager transferManager = createTransferManager(); |
| 29 | + |
| 30 | + public static void main(String[] args) { |
| 31 | + try { |
| 32 | + putObjectDemo(); |
| 33 | + getObjectDemo(); |
| 34 | + } catch (Exception e) { |
| 35 | + e.printStackTrace(); |
| 36 | + } finally { |
| 37 | + transferManager.shutdownNow(); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + private static void putObjectDemo() { |
| 42 | + File localFile = new File("/path/to/localFile"); |
| 43 | + PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile); |
| 44 | + try { |
| 45 | + // 返回一个异步结果 Upload , 可同步的调用 waitForUploadResult 等待 upload 结束, 成功返回 UploadResult , 失败抛出异常. |
| 46 | + Upload upload = transferManager.upload(putObjectRequest); |
| 47 | + UploadResult uploadResult = upload.waitForUploadResult(); |
| 48 | + System.out.printf("RequestId : %s\n", uploadResult.getRequestId()); |
| 49 | + System.out.println(uploadResult.getETag()); |
| 50 | + System.out.println(uploadResult.getCrc64Ecma()); |
| 51 | + } catch (CosServiceException e) { |
| 52 | + e.printStackTrace(); |
| 53 | + } catch (CosClientException e) { |
| 54 | + e.printStackTrace(); |
| 55 | + } catch (InterruptedException e) { |
| 56 | + e.printStackTrace(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private static void getObjectDemo() { |
| 61 | + File downloadFile = new File("/path/to/downloadFile"); |
| 62 | + GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key); |
| 63 | + try { |
| 64 | + // 返回一个异步结果 download, 可同步的调用 waitForCompletion 等待 download 结束, 成功返回 void , 失败抛出异常. |
| 65 | + Download download = transferManager.download(getObjectRequest, downloadFile); |
| 66 | + download.waitForCompletion(); |
| 67 | + System.out.printf("RequestId : %s\n", download.getObjectMetadata().getRequestId()); |
| 68 | + } catch (CosServiceException e) { |
| 69 | + e.printStackTrace(); |
| 70 | + } catch (CosClientException e) { |
| 71 | + e.printStackTrace(); |
| 72 | + } catch (InterruptedException e) { |
| 73 | + e.printStackTrace(); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private static TransferManager createTransferManager() { |
| 78 | + // 初始化用户身份信息(secretId, secretKey) |
| 79 | + COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX","1A2Z3YYYYYYYYYY"); |
| 80 | + // 2 设置 bucket 的地域, COS_REGION 请参见 https://www.qcloud.com/document/product/436/6224 |
| 81 | + ClientConfig clientConfig = new ClientConfig(new Region("COS_REGION")); |
| 82 | + // 设置请求协议,推荐使用 https 协议 |
| 83 | + clientConfig.setHttpProtocol(HttpProtocol.https); |
| 84 | + //若设置自定义源站域名时未上传 https 证书,则改为 clientConfig.setHttpProtocol(HttpProtocol.http); |
| 85 | + // 设置自定义的域名 |
| 86 | + UserSpecifiedEndpointBuilder endpointBuilder = new UserSpecifiedEndpointBuilder("generalApiEndpoint", "getServiceApiEndpoint"); |
| 87 | + clientConfig.setEndpointBuilder(endpointBuilder); |
| 88 | + // 3 生成 cos 客户端 |
| 89 | + COSClient cosclient = new COSClient(cred, clientConfig); |
| 90 | + |
| 91 | + ExecutorService threadPool = Executors.newFixedThreadPool(4); |
| 92 | + // 传入一个 threadpool, 若不传入线程池, 默认 TransferManager 中会生成一个单线程的线程池。 |
| 93 | + TransferManager transferManager = new TransferManager(cosclient, threadPool); |
| 94 | + |
| 95 | + return transferManager; |
| 96 | + } |
| 97 | +} |
0 commit comments