产品动态
产品公告
<groupId>com.qcloud</groupId><artifactId>cos_api-bundle</artifactId><version>5.6.35</version>
<groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.3</version>
<groupId>com.qcloud</groupId><artifactId>cos_api-bundle</artifactId><version>5.6.35</version>
<groupId>com.qcloud</groupId><artifactId>cos_api-bundle</artifactId><version>5.6.35</version>
/结尾的对象。创建文件时,不需要创建目录。如创建一个对象键为 xxx/yyy/zzz.txt的文件,只用把 key 设置为xxx/yyy/zzz.txt即可,不用建立xxx/yyy/这个对象。在控制台上展示时,也会以/作为分隔,展示出目录的层级效果。但这些目录对象是不存在的。如果想创建一个目录对象,可使用以下的示例代码:String bucketName = "examplebucket-1250000000";String key = "folder/images/";// 目录对象即是一个/结尾的空文件,上传一个长度为 0 的 byte 流InputStream input = new ByteArrayInputStream(new byte[0]);ObjectMetadata objectMetadata = new ObjectMetadata();objectMetadata.setContentLength(0);PutObjectRequest putObjectRequest =new PutObjectRequest(bucketName, key, input, objectMetadata);PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
// 初始化用户身份信息(secretId, secretKey)String secretId = "COS_SECRETID";String secretKey = "COS_SECRETKEY";COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);// 设置 bucket 的区域, COS 地域的简称请参照 https://www.tencentcloud.com/document/product/436/6224ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing-1"));// 配置使用 httpsclientConfig.setHttpProtocol(HttpProtocol.https);// 生成 cos 客户端COSClient cosClient = new COSClient(cred, clientConfig);
// 初始化用户身份信息(secretId, secretKey)String secretId = "COS_SECRETID";String secretKey = "COS_SECRETKEY";COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);// 设置 bucket 的区域, COS 地域的简称请参照 https://www.tencentcloud.com/document/product/436/6224ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing-1"));// 配置使用代理(IP 和端口需要同时设置)// 设置代理 IP (也可传入域名)clientConfig.setHttpProxyIp("192.168.2.3");// 设置代理端口clientConfig.setHttpProxyPort(8080);// 生成 cos 客户端COSClient cosClient = new COSClient(cred, clientConfig);
// 步骤1:实现 EndpointBuilder 接口中的两个函数class SelfDefinedEndpointBuilder implements EndpointBuilder {@Overridepublic String buildGeneralApiEndpoint(String bucketName) {return String.format("%s.%s", bucketName, "mytest.com");}@Overridepublic String buildGetServiceApiEndpoint() {return "service.mytest.com";}}// 步骤2:初始化客户端String secretId = "COS_SECRETID";String secretKey = "COS_SECRETKEY";COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);SelfDefinedEndpointBuilder selfDefinedEndpointBuilder = new SelfDefinedEndpointBuilder();ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing"));clientConfig .setEndpointBuilder(selfDefinedEndpointBuilder);COSClient cosClient = new COSClient(cred, clientConfig);
/ 前缀?/ 前缀。例如,您将对象 key 值设置为 exampleobject 上传的对象,可以通过 URL: http://cos.ap-guangzhou.myqcloud.com/exampleobject进行访问。/ 前缀的 key,这将导致对象删除失败。CRC64 localCRC = new CRC64();// 计算本地的 crc64localCRC.update();//...// 把 COS 返回的 CRC 变成 LongcosCRC = crc64ToLong(strCOSCRC);// 比较if (cosCRC == localCRC.getValue()) {xxx}// 用来将 COS 返回的 CRC64 转化成 1 个 Java 里的 Longlong crc64ToLong(String crc64) {if (crc64.charAt(0) == '-') {return negativeCrc64ToLong(crc64);} else {return positiveCrc64ToLong(crc64);}}long positiveCrc64ToLong(String strCrc64) {BigInteger crc64 = new BigInteger(strCrc64);BigInteger maxLong = new BigInteger(Long.toString(Long.MAX_VALUE));int maxCnt = 0;while (crc64.compareTo(maxLong) > 0) {crc64 = crc64.subtract(maxLong);maxCnt++;}return crc64.longValue() + Long.MAX_VALUE * maxCnt;}long negativeCrc64ToLong(String strCrc64) {BigInteger crc64 = new BigInteger(strCrc64);BigInteger minLong = new BigInteger(Long.toString(Long.MIN_VALUE));int minCnt = 0;while (crc64.compareTo(minLong) < 0) {crc64 = crc64.subtract(minLong);minCnt++;}return crc64.longValue() + Long.MIN_VALUE * minCnt;}
文档反馈