// UserSpecifiedEndpointBuilder 构造函数com.qcloud.cos.endpoint.UserSpecifiedEndpointBuilder(String generalApiEndpoint, String getServiceApiEndpoint);
import java.io.File;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import com.qcloud.cos.COSClient;import com.qcloud.cos.ClientConfig;import com.qcloud.cos.auth.BasicCOSCredentials;import com.qcloud.cos.auth.COSCredentials;import com.qcloud.cos.endpoint.UserSpecifiedEndpointBuilder;import com.qcloud.cos.exception.CosClientException;import com.qcloud.cos.exception.CosServiceException;import com.qcloud.cos.http.HttpProtocol;import com.qcloud.cos.model.PutObjectRequest;import com.qcloud.cos.model.UploadResult;import com.qcloud.cos.region.Region;import com.qcloud.cos.transfer.TransferManager;import com.qcloud.cos.transfer.Upload;public static void uploadFile() {// 1 初始化用户身份信息 (secretId, secretKey)COSCredentials cred = new BasicCOSCredentials("secretId", "secretKey");// 2 设置 bucket 的地域, COS_REGION 请参照 https://www.qcloud.com/document/product/436/6224ClientConfig clientConfig = new ClientConfig(new Region("COS_REGION"));// 设置请求协议,推荐使用 https 协议clientConfig.setHttpProtocol(HttpProtocol.https);//若设置自定义源站域名时未上传 https 证书,则改为 clientConfig.setHttpProtocol(HttpProtocol.http);// 设置自定义的域名UserSpecifiedEndpointBuilder endpointBuilder = new UserSpecifiedEndpointBuilder("generalApiEndpoint", "getServiceApiEndpoint");clientConfig.setEndpointBuilder(endpointBuilder);// 3 生成 cos 客户端COSClient cosclient = new COSClient(cred, clientConfig);// bucket 名需包含 appidString bucketName = "BucketName-APPID";ExecutorService threadPool = Executors.newFixedThreadPool(32);// 传入一个 threadpool, 若不传入线程池, 默认 TransferManager 中会生成一个单线程的线程池。TransferManager transferManager = new TransferManager(cosclient, threadPool);// 设置对象的 keyString key = "exampleobject";// 本地文件路径File localFile = new File("/path/to/localFile");PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);try {// 返回一个异步结果 Upload , 可同步的调用 waitForUploadResult 等待 upload 结束, 成功返回 UploadResult , 失败抛出异常.Upload upload = transferManager.upload(putObjectRequest);UploadResult uploadResult = upload.waitForUploadResult();System.out.printf("RequestId : %s\\n",uploadResult.getRequestId());System.out.println(uploadResult.getETag());System.out.println(uploadResult.getCrc64Ecma());} catch (CosServiceException e) {e.printStackTrace();} catch (CosClientException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();}transferManager.shutdownNow();}
参数名 | 描述 | 类型 |
generalApiEndpoint | 自定义源站域名 | String |
getServiceApiEndpoint | 代表列举存储桶列表时的域名,推荐填 service.cos.myqcloud.com | String |
// UserSpecifiedEndpointBuilder 构造函数com.qcloud.cos.endpoint.UserSpecifiedEndpointBuilder(String generalApiEndpoint, String getServiceApiEndpoint);
import java.io.File;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import com.qcloud.cos.COSClient;import com.qcloud.cos.ClientConfig;import com.qcloud.cos.auth.BasicCOSCredentials;import com.qcloud.cos.auth.COSCredentials;import com.qcloud.cos.endpoint.UserSpecifiedEndpointBuilder;import com.qcloud.cos.exception.CosClientException;import com.qcloud.cos.exception.CosServiceException;import com.qcloud.cos.http.HttpProtocol;import com.qcloud.cos.model.GetObjectRequest;import com.qcloud.cos.region.Region;import com.qcloud.cos.transfer.Download;import com.qcloud.cos.transfer.TransferManager;public static void downLoadFile() {// 1 初始化用户身份信息(secretId, secretKey)COSCredentials cred = new BasicCOSCredentials("secretId", "secretKey");// 2 设置 bucket 的地域, COS_REGION 请参照 https://www.qcloud.com/document/product/436/6224ClientConfig clientConfig = new ClientConfig(new Region("COS_REGION"));// 设置请求协议,推荐使用 https 协议clientConfig.setHttpProtocol(HttpProtocol.https);//若设置自定义源站域名时未上传 https 证书,则改为 clientConfig.setHttpProtocol(HttpProtocol.http);// 设置自定义的域名UserSpecifiedEndpointBuilder endpointBuilder = new UserSpecifiedEndpointBuilder("generalApiEndpoint", "getServiceApiEndpoint");clientConfig.setEndpointBuilder(endpointBuilder);// 3 生成 cos 客户端COSClient cosclient = new COSClient(cred, clientConfig);// bucket 名需包含 appidString bucketName = "BucketName-APPID";ExecutorService threadPool = Executors.newFixedThreadPool(32);// 传入一个 threadpool, 若不传入线程池, 默认 TransferManager 中会生成一个单线程的线程池。TransferManager transferManager = new TransferManager(cosclient, threadPool);// 设置对象的 keyString key = "exampleobject";// 本地文件路径File downloadFile = new File("/path/to/localFile");GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);try {// 返回一个异步结果 download, 可同步的调用 waitForCompletion 等待 download 结束, 成功返回 void , 失败抛出异常.Download download = transferManager.download(getObjectRequest, downloadFile);download.waitForCompletion();System.out.printf("RequestId : %s\\n",download.getObjectMetadata().getRequestId());} catch (CosServiceException e) {e.printStackTrace();} catch (CosClientException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();}transferManager.shutdownNow();}
参数名 | 描述 | 类型 |
generalApiEndpoint | 自定义源站域名 | String |
getServiceApiEndpoint | 代表列举存储桶列表时的域名,推荐填 service.cos.myqcloud.com | String |
本页内容是否解决了您的问题?