产品动态
产品公告
[root@VM_centos /data/home/]# ping cos.ap-guangzhou.myqcloud.comPING cos.ap-guangzhou.myqcloud.com (9.*.*.*) xx(xx) bytes of data.64 bytes from 9.*.*.* (9.*.*.*): icmp_seq=1 ttl=xx time=0.xxx ms64 bytes from 9.*.*.* (9.*.*.*): icmp_seq=2 ttl=xx time=0.xxx ms64 bytes from 9.*.*.* (9.*.*.*): icmp_seq=3 ttl=xx time=0.xxx ms64 bytes from 9.*.*.* (9.*.*.*): icmp_seq=4 ttl=xx time=0.xxx ms64 bytes from 9.*.*.* (9.*.*.*): icmp_seq=5 ttl=xx time=0.xxx ms64 bytes from 9.*.*.* (9.*.*.*): icmp_seq=6 ttl=xx time=0.xxx ms64 bytes from 9.*.*.* (9.*.*.*): icmp_seq=7 ttl=xx time=0.xxx ms^C--- cos.ap-guangzhou.myqcloud.com ping statistics ---x packets transmitted, x received, 0% packet loss, time xxxmsrtt min/avg/max/mdev = 0.xxx/0.xxx/0.xxx/0.xxx ms
ping cos.ap-guangzhou.myqcloud.com 并回车进行测试。[root@VM_centos /data/home/]# curl http://cos.ap-guangzhou.myqcloud.com -v* About to connect() to cos.ap-guangzhou.myqcloud.com port 80 (#0)* Trying 9.*.*.*...* Connected to cos.ap-guangzhou.myqcloud.com (9.*.*.*) port 80 (#0)> GET / HTTP/1.1> User-Agent: curl/*.*.0> Host: cos.ap-guangzhou.myqcloud.com> Accept: */*>< HTTP/1.1 403 Forbidden< Content-Type: application/xml< Content-Length: XXX< Connection: keep-alive< Date: XXX XXX GMT< Server: tencent-cos< x-cos-request-id: NWE2MWQ5MjZfMTBhYzM1MGFfMTA5ODVfMTVj****<<?xml version='1.0' encoding='utf-8' ?><Error><Code>AccessDenied</Code><Message>Check auth failure because signture empty.</Message><ServerTime>XXX XXX</ServerTime><Resource>cos.ap-guangzhou.myqcloud.com/</Resource><RequestId>NWE2MWQ5MjZfMTBhYzM1MGFfMTA5ODVfMTVj****==</RequestId></Error>* Connection #0 to host cos.ap-guangzhou.myqcloud.com left intact
[root@VM_centos /data/home/]# curl https://cos.ap-guangzhou.myqcloud.com -vk* About to connect() to cos.ap-guangzhou.myqcloud.com port 443 (#0)* Trying 9.*.*.*...* Connected to cos.ap-guangzhou.myqcloud.com (9.*.*.*) port 443 (#0)* Initializing NSS with certpath: XXXX* skipping SSL peer certificate verification* SSL connection using ******* Server certificate:* subject: CN=*.*.*.* start date: XXX XXX GMT* expire date: XXX XXX GMT* common name: *.cos.ap-guangzhou.myqcloud.com* issuer: XXX> GET / HTTP/1.1> User-Agent: curl/*.*.0> Host: cos.ap-guangzhou.myqcloud.com> Accept: */*>< HTTP/1.1 403 Forbidden< Content-Type: application/xml< Content-Length: XXX< Connection: keep-alive< Date: XXX XXX GMT< Server: tencent-cos< x-cos-request-id: NWE2MWQ5MjZfMTBhYzM1MGFfMTA5ODVfMTVj****<<?xml version='1.0' encoding='utf-8' ?><Error><Code>AccessDenied</Code><Message>Check auth failure because signture empty.</Message><ServerTime>XXX XXX</ServerTime><Resource>cos.ap-guangzhou.myqcloud.com/</Resource><RequestId>NWE2MWQ5MjZfMTBhYzM1MGFfMTA5ODVfMTVj****</RequestId></Error>* Connection #0 to host cos.ap-guangzhou.myqcloud.com left intact
<dependency><groupId>com.qcloud</groupId><artifactId>cos_api</artifactId><version>5.6.227</version></dependency>
com.qcloud.cos.*,您可以通过 Eclipse 或者 IntelliJ 等 IDE 工具,导入程序运行所需要的类。// 1 传入获取到的临时密钥 (tmpSecretId, tmpSecretKey, sessionToken)String tmpSecretId = "SECRETID";String tmpSecretKey = "SECRETKEY";String sessionToken = "TOKEN";BasicSessionCredentials cred = new BasicSessionCredentials(tmpSecretId, tmpSecretKey, sessionToken);// 2 设置 bucket 的地域// clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分Region region = new Region("COS_REGION"); //COS_REGION 参数:配置成存储桶 bucket 的实际地域,例如 ap-beijing,更多 COS 地域的简称请参见 https://www.tencentcloud.com/document/product/436/6224ClientConfig clientConfig = new ClientConfig(region);// 3 生成 cos 客户端COSClient cosClient = new COSClient(cred, clientConfig);
// 1 初始化用户身份信息(secretId, secretKey)。// SECRETID 和 SECRETKEY 请登录访问管理控制台 https://console.tencentcloud.com/cam/capi 进行查看和管理String secretId = System.getenv("secretId");//用户的 SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1String secretKey = System.getenv("secretKey");//用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);// 2 设置 bucket 的地域, COS 地域的简称请参见 https://www.tencentcloud.com/document/product/436/6224// clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。Region region = new Region("COS_REGION");ClientConfig clientConfig = new ClientConfig(region);// 这里建议设置使用 https 协议// 从 5.6.54 版本开始,默认使用了 httpsclientConfig.setHttpProtocol(HttpProtocol.https);// 3 生成 cos 客户端。COSClient cosClient = new COSClient(cred, clientConfig);
成员名 | 设置方法 | 描述 | 类型 |
region | 构造函数或 set 方法 | Region | |
httpProtocol | set 方法 | 请求所使用的协议,默认使用 HTTPS 协议与 COS 交互 | HttpProtocol |
signExpired | set 方法 | 请求签名的有效时间,单位:秒,默认为3600s | long |
connectionTimeout | set 方法 | 连接 COS 服务的超时时间,单位:毫秒,默认为30000ms | int |
socketTimeout | set 方法 | 客户端读取数据的超时时间,单位:毫秒,默认为30000ms | int |
httpProxyIp | set 方法 | 代理服务器的 IP | String |
httpProxyPort | set 方法 | 代理服务器的端口 | int |
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.exception.CosClientException;import com.qcloud.cos.exception.CosServiceException;import com.qcloud.cos.model.Bucket;import com.qcloud.cos.model.CannedAccessControlList;import com.qcloud.cos.model.CreateBucketRequest;import com.qcloud.cos.region.Region;public class BucketDemo {private static String secretId = System.getenv("SECRETID");private static String secretKey = System.getenv("SECRETKEY");private static String bucketName = System.getenv("BUCKET_NAME");private static String cosRegion = System.getenv("REGION");private static COSClient cosClient = createCli();public static void main(String[] args) {try {createBucketDemo();//createMAZBucketDemo();} catch (Exception e) {e.printStackTrace();} finally {cosClient.shutdown();}}private static COSClient createCli() {// 1 初始化用户身份信息(appid, secretId, secretKey)COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224ClientConfig clientConfig = new ClientConfig(new Region(cosRegion));// 3 生成cos客户端COSClient cosclient = new COSClient(cred, clientConfig);return cosclient;}// 创建bucketprivate static void createBucketDemo() {CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);// 设置bucket的权限为PublicRead(公有读私有写), 其他可选有私有读写, 公有读私有写createBucketRequest.setCannedAcl(CannedAccessControlList.PublicRead);Bucket bucket = cosClient.createBucket(createBucketRequest);System.out.println("create bucket, bucketName is " + bucket.getName());}//创多AZ桶private static void createMAZBucketDemo() {CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);try {Bucket bucket = cosClient.createMAZBucket(createBucketRequest);System.out.println("create MAZ bucket, bucketName is " + bucket.getName());} catch (CosServiceException cse) {cse.printStackTrace();} catch (CosClientException cce) {cce.printStackTrace();}}}
import java.util.ArrayList;import java.util.List;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.exception.CosClientException;import com.qcloud.cos.exception.CosServiceException;import com.qcloud.cos.model.Bucket;import com.qcloud.cos.model.ListBucketsRequest;import com.qcloud.cos.model.ListBucketsResult;import com.qcloud.cos.region.Region;public class BucketDemo {private static String secretId = System.getenv("SECRETID");private static String secretKey = System.getenv("SECRETKEY");private static String bucketName = System.getenv("BUCKET_NAME");private static String cosRegion = System.getenv("REGION");private static COSClient cosClient = createCli();public static void main(String[] args) {try {listBuckets();} catch (Exception e) {e.printStackTrace();} finally {cosClient.shutdown();}}private static COSClient createCli() {// 1 初始化用户身份信息(appid, secretId, secretKey)COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224ClientConfig clientConfig = new ClientConfig(new Region(cosRegion));// 3 生成cos客户端COSClient cosclient = new COSClient(cred, clientConfig);return cosclient;}private static void listBuckets() {List<Bucket> buckets = cosClient.listBuckets();for (Bucket bucket : buckets) {System.out.println(bucket.getName());System.out.println(bucket.getLocation());System.out.println(bucket.getOwner());System.out.println(bucket.getType());System.out.println(bucket.getBucketType());}}}
com.qcloud.cos.transfer.\\* 子包下。examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/images/picture.jpg 中,对象键为 images/picture.jpg,详情请参见 对象键 的说明。import java.io.File;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.exception.CosClientException;import com.qcloud.cos.exception.CosServiceException;import com.qcloud.cos.model.PutObjectRequest;import com.qcloud.cos.model.PutObjectResult;import com.qcloud.cos.region.Region;public class PutObjectDemo {private static String secretId = System.getenv("SECRETID");private static String secretKey = System.getenv("SECRETKEY");private static String bucketName = System.getenv("BUCKET_NAME");private static String region = System.getenv("REGION");private static COSClient cosClient = createCli();public static void main(String[] args) {try {putLocalFileDemo();} catch (Exception e) {e.printStackTrace();} finally {cosClient.shutdown();}}private static COSClient createCli() {// 初始化用户身份信息(secretId, secretKey)COSCredentials cred = new BasicCOSCredentials(secretId,secretKey);// 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224ClientConfig clientConfig = new ClientConfig(new Region(region));// 生成cos客户端return new COSClient(cred, clientConfig);}private static void putLocalFileDemo() {String key = "abc/abc.txt";String localPath = "abc.txt";PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, new File(localPath));try {PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);System.out.println(putObjectResult.getRequestId());} catch (CosServiceException cse) {cse.printStackTrace();} catch (CosClientException cce) {cce.printStackTrace();}}}
import java.util.List;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.exception.CosClientException;import com.qcloud.cos.exception.CosServiceException;import com.qcloud.cos.model.COSObjectSummary;import com.qcloud.cos.model.COSVersionSummary;import com.qcloud.cos.model.ListObjectsRequest;import com.qcloud.cos.model.ListVersionsRequest;import com.qcloud.cos.model.ObjectListing;import com.qcloud.cos.model.VersionListing;import com.qcloud.cos.region.Region;public class ListObjectsDemo {private static String secretId = System.getenv("SECRETID");private static String secretKey = System.getenv("SECRETKEY");private static String bucketName = System.getenv("BUCKET_NAME");private static String region = System.getenv("REGION");private static COSClient cosClient = createClient();public static void main(String[] args) {try {listObjectsVersions();//listObjectsDemo();//listAllObjects();} catch (Exception e) {e.printStackTrace();} finally {cosClient.shutdown();}}private static COSClient createClient() {// 初始化用户身份信息(secretId, secretKey)COSCredentials cred = new BasicCOSCredentials(secretId,secretKey);// 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224ClientConfig clientConfig = new ClientConfig(new Region(region));// 生成cos客户端COSClient cosclient = new COSClient(cred, clientConfig);return cosclient;}private static void listObjectsDemo() {ListObjectsRequest listObjectsRequest = new ListObjectsRequest();// 设置bucket名称listObjectsRequest.setBucketName(bucketName);// prefix表示列出的object的key以prefix开始listObjectsRequest.setPrefix("");// 设置最大遍历出多少个对象, 一次listobject最大支持1000listObjectsRequest.setMaxKeys(1000);// listObjectsRequest.setDelimiter("/");ObjectListing objectListing = null;try {objectListing = cosClient.listObjects(listObjectsRequest);} catch (CosServiceException e) {e.printStackTrace();} catch (CosClientException e) {e.printStackTrace();}// common prefix表示表示被delimiter截断的路径, 如delimter设置为/, common prefix则表示所有子目录的路径List<String> commonPrefixs = objectListing.getCommonPrefixes();// object summary表示所有列出的object列表List<COSObjectSummary> cosObjectSummaries = objectListing.getObjectSummaries();for (COSObjectSummary cosObjectSummary : cosObjectSummaries) {// 文件的路径keyString key = cosObjectSummary.getKey();// 文件的etagString etag = cosObjectSummary.getETag();// 文件的长度long fileSize = cosObjectSummary.getSize();// 文件的存储类型String storageClasses = cosObjectSummary.getStorageClass();System.out.println("key: " + key);}}// 如果要获取超过maxkey数量的object或者获取所有的object, 则需要循环调用listobject, 用上一次返回的next marker作为下一次调用的marker,// 直到返回的truncated为falseprivate static void listAllObjects() {ListObjectsRequest listObjectsRequest = new ListObjectsRequest();// 设置bucket名称listObjectsRequest.setBucketName(bucketName);// prefix表示列出的object的key以prefix开始listObjectsRequest.setPrefix("");// deliter表示分隔符, 设置为/表示列出当前目录下的object, 设置为空表示列出所有的objectlistObjectsRequest.setDelimiter("");// 设置最大遍历出多少个对象, 一次listobject最大支持1000listObjectsRequest.setMaxKeys(1000);ObjectListing objectListing = null;do {try {objectListing = cosClient.listObjects(listObjectsRequest);} catch (CosServiceException e) {e.printStackTrace();return;} catch (CosClientException e) {e.printStackTrace();return;}// common prefix表示表示被delimiter截断的路径, 如delimter设置为/, common prefix则表示所有子目录的路径List<String> commonPrefixs = objectListing.getCommonPrefixes();// object summary表示所有列出的object列表List<COSObjectSummary> cosObjectSummaries = objectListing.getObjectSummaries();for (COSObjectSummary cosObjectSummary : cosObjectSummaries) {// 文件的路径keyString key = cosObjectSummary.getKey();// 文件的etagString etag = cosObjectSummary.getETag();// 文件的长度long fileSize = cosObjectSummary.getSize();// 文件的存储类型String storageClasses = cosObjectSummary.getStorageClass();}String nextMarker = objectListing.getNextMarker();listObjectsRequest.setMarker(nextMarker);} while (objectListing.isTruncated());}private static void listObjectsVersions() {ListVersionsRequest listVersionsRequest = new ListVersionsRequest();listVersionsRequest.setBucketName(bucketName);listVersionsRequest.setPrefix("");VersionListing versionListing = null;do {try {versionListing = cosClient.listVersions(listVersionsRequest);} catch (CosServiceException e) {e.printStackTrace();return;} catch (CosClientException e) {e.printStackTrace();return;}List<COSVersionSummary> cosVersionSummaries = versionListing.getVersionSummaries();for (COSVersionSummary cosVersionSummary : cosVersionSummaries) {System.out.println(cosVersionSummary.getKey() + ":" + cosVersionSummary.getVersionId());}String keyMarker = versionListing.getNextKeyMarker();String versionIdMarker = versionListing.getNextVersionIdMarker();listVersionsRequest.setKeyMarker(keyMarker);listVersionsRequest.setVersionIdMarker(versionIdMarker);} while (versionListing.isTruncated());}}
import java.io.File;import java.io.IOException;import java.io.InputStream;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.exception.CosClientException;import com.qcloud.cos.exception.CosServiceException;import com.qcloud.cos.model.COSObject;import com.qcloud.cos.model.GetObjectRequest;import com.qcloud.cos.model.ObjectMetadata;import com.qcloud.cos.region.Region;import com.qcloud.cos.utils.IOUtils;public class GetObjectDemo {private static String secretId = System.getenv("SECRETID");private static String secretKey = System.getenv("SECRETKEY");private static String bucketName = System.getenv("BUCKET_NAME");private static String region = System.getenv("REGION");private static COSClient cosClient = createClient();public static void main(String[] args) {try {getObjectToFileDemo();//getObjectInputStreamDemo();} catch (Exception e) {e.printStackTrace();} finally {cosClient.shutdown();}}private static COSClient createClient() {// 初始化用户身份信息(secretId, secretKey)COSCredentials cred = new BasicCOSCredentials(secretId,secretKey);// 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224ClientConfig clientConfig = new ClientConfig(new Region(region));// 生成cos客户端COSClient cosclient = new COSClient(cred, clientConfig);return cosclient;}private static void getObjectToFileDemo() {String key = "test/my_test.json";GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);// 设置下载的单链接限速(如有需要),不需要可忽略getObjectRequest.setTrafficLimit(8*1024*1024);try {File localFile = new File("my_test.json");ObjectMetadata objectMetadata = cosClient.getObject(getObjectRequest, localFile);System.out.println(objectMetadata.getContentLength());} catch (CosServiceException cse) {cse.printStackTrace();} catch (CosClientException cce) {cce.printStackTrace();}}private static void getObjectInputStreamDemo() throws IOException {String key = "test/my_test.json";GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);InputStream cosObjectInput = null;try {COSObject cosObject = cosClient.getObject(getObjectRequest);cosObjectInput = cosObject.getObjectContent();} catch (CosServiceException e) {e.printStackTrace();} catch (CosClientException e) {e.printStackTrace();}// 处理下载到的流// 这里是直接读取,按实际情况来处理byte[] bytes = null;try {bytes = IOUtils.toByteArray(cosObjectInput);} catch (IOException e) {e.printStackTrace();} finally {// 用完流之后一定要调用 close()cosObjectInput.close();}}}
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.exception.CosClientException;import com.qcloud.cos.exception.CosServiceException;import com.qcloud.cos.region.Region;public class DelFileDemo {private static String secretId = System.getenv("SECRETID");private static String secretKey = System.getenv("SECRETKEY");private static String bucketName = System.getenv("BUCKET_NAME");private static String region = System.getenv("REGION");private static COSClient cosClient = createCli();public static void main(String[] args) {delSingleFile();if (cosClient != null) {cosClient.shutdown();}}private static COSClient createCli() {// 1 初始化用户身份信息(secretId, secretKey)COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224ClientConfig clientConfig = new ClientConfig(new Region(region));// 3 生成cos客户端COSClient cosclient = new COSClient(cred, clientConfig);return cosclient;}// 删除单个文件(不带版本号, 即bucket未开启多版本)private static void delSingleFile() {try {String key = "aaa/bbb.txt";cosClient.deleteObject(bucketName, key);} catch (CosServiceException e) { // 如果是其他错误, 比如参数错误, 身份验证不过等会抛出CosServiceExceptione.printStackTrace();} catch (CosClientException e) { // 如果是客户端错误,比如连接不上COSe.printStackTrace();}}}
Region region = new Region("COS_REGION");ClientConfig clientConfig = new ClientConfig(region);// 设置最大重试次数为 4 次clientConfig.setMaxErrorRetry = 4;
// 自定义重试策略public class OnlyIOExceptionRetryPolicy extends RetryPolicy {@Overridepublic <X extends CosServiceRequest> boolean shouldRetry(CosHttpRequest<X> request,HttpResponse response,Exception exception,int retryIndex) {// 如果是客户端的 IOException 异常则重试,否则不重试if (exception.getCause() instanceof IOException) {return true;}return false;}}Region region = new Region("COS_REGION");ClientConfig clientConfig = new ClientConfig(region);RetryPolicy myRetryPolicy = new OnlyIOExceptionRetryPolicy();// 设置自定义的重试策略clientConfig.setRetryPolicy(myRetryPolicy);
文档反馈