

// 存储桶所在地域简称,例如广州地区是 ap-guangzhouString region = "COS_REGION" ;// tls 客户端证书字节数组, 需要为 BKS 格式byte[] certificateBytes;// BKS 文件的密码,如果你的 BKS 文件没有密码,请传入 nullString password = "123456";// 配置了双向认证的服务端 hostString dualCheckHost = "dual-check.myEdgeOne.com";CosXmlServiceConfig cosXmlServiceConfig = new CosXmlServiceConfig.Builder().isHttps(true).setRegion(region).setHost(dualCheckHost)// 设置 tls 客户端证书.setClientCertificate(certificateBytes, password).builder();CosXmlService cosXmlService = new CosXmlService(context, cosXmlServiceConfig,credentialProvider);
QCloudServiceConfiguration* configuration = [QCloudServiceConfiguration new];QCloudCOSXMLEndPoint* endpoint = [[QCloudCOSXMLEndPoint alloc] init];// 替换为用户的 region,已创建桶归属的 region 可以在控制台查看,https://console.tencentcloud.com/cos5/bucket// COS 支持的所有 region 列表参见 https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1endpoint.regionName = @"COS_REGION";// 使用 HTTPSendpoint.useHTTPS = true;configuration.endpoint = endpoint;// 配置客户端 p12证书。NSString *path = [[NSBundle mainBundle] pathForResource:@"client" ofType:@"p12"];NSData *p12Data = [NSData dataWithContentsOfFile:path];configuration.clientCertificateData = p12Data;// 配置证书密码configuration.password = @"123456";// 初始化 COS 服务示例[QCloudCOSXMLService registerDefaultCOSXMLWithConfiguration:configuration];[QCloudCOSTransferMangerService registerDefaultCOSTransferMangerWithConfiguration:configuration];
// 准备 TransferManagerTransferConfig transferConfig = new TransferConfig.Builder().build();TransferManager transferManager = new TransferManager(cosXmlService,transferConfig);// 使用 COS 托管加密密钥的服务端加密(SSE-COS)保护数据PutObjectRequest putObjectRequestCos = new PutObjectRequest(bucket, cosPath, srcPath);putObjectRequestCos.setCOSServerSideEncryption();COSXMLUploadTask cosxmlUploadTaskCos = transferManager.upload(putObjectRequestCos, uploadId);// 设置使用客户提供的用户主密钥的服务端加密(SSE-KMS)保护数据String customKey = "用户主密钥 CMK";String encryptContext = "加密上下文";PutObjectRequest putObjectRequestKms = new PutObjectRequest(bucket, cosPath, srcPath);try {putObjectRequestKms.setCOSServerSideEncryptionWithKMS(customKey, encryptContext);} catch (CosXmlClientException e) {e.printStackTrace();}COSXMLUploadTask cosxmlUploadTaskKms = transferManager.upload(putObjectRequestKms, uploadId);// 设置使用客户提供的加密密钥的服务端加密(SSE-C)保护数据String customKeyC = "服务端加密密钥";PutObjectRequest putObjectRequestC = new PutObjectRequest(bucket, cosPath, srcPath);try {putObjectRequestC.setCOSServerSideEncryptionWithCustomerKey(customKey);} catch (CosXmlClientException e) {e.printStackTrace();}// 上传对象COSXMLUploadTask cosxmlUploadTaskC = transferManager.upload(putObjectRequestC, uploadId);
// 1、使用 COS 托管加密密钥的服务端加密(SSE-COS)保护数据QCloudCOSXMLUploadObjectRequest *request = [QCloudCOSXMLUploadObjectRequest new];[request setCOSServerSideEncyption];// 2、使用客户提供的加密密钥的服务端加密(SSE-C)保护数据QCloudCOSXMLUploadObjectRequest *request = [QCloudCOSXMLUploadObjectRequest new];NSString *customKey = @"123456qwertyuioplkjhgfdsazxcvbnm";[request setCOSServerSideEncyptionWithCustomerKey:customKey];//3、使用 KMS 托管加密密钥的服务端加密(SSE-KMS)保护数据QCloudCOSXMLUploadObjectRequest *request = [QCloudCOSXMLUploadObjectRequest new];NSString *customKey = @"123456qwertyuioplkjhgfdsazxcvbnm";NSString *arrJsonStr = @"{\\"key\\":\\"value\\"}";[request setCOSServerSideEncyptionWithKMSCustomKey:customKey jsonStr:arrJsonStr];
// 初始化 TransferManagerTransferConfig transferConfig = new TransferConfig.Builder().build();TransferManager transferManager = new TransferManager(cosXmlService,transferConfig);// 上传对象String srcPath = new File(context.getCacheDir(), "exampleobject.txt").toString(); //本地文件的绝对路径COSXMLUploadTask cosxmlUploadTask = transferManager.upload("examplebucket-1250000000", "exampleobject.txt",srcPath, null);
QCloudCOSXMLUploadObjectRequest* put = [QCloudCOSXMLUploadObjectRequest new];NSURL* url = [NSURL fileURLWithPath:@"文件的URL"];// 存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.tencentcloud.com/cos5/bucketput.bucket = @"examplebucket-1250000000";// 对象键,是对象在 COS 上的完整路径,如果带目录的话,格式为 "video/xxx/movie.mp4"put.object = @"exampleobject";// 需要上传的对象内容。可以传入 NSData*或者 NSURL*类型的变量put.body = url;// 监听上传进度[put setSendProcessBlock:^(int64_t bytesSent,int64_t totalBytesSent,int64_t totalBytesExpectedToSend) {// bytesSent 本次要发送的字节数(一个大文件可能要分多次发送)// totalBytesSent 已发送的字节数// totalBytesExpectedToSend 本次上传要发送的总字节数(即一个文件大小)}];// 监听上传结果[put setFinishBlock:^(QCloudUploadObjectResult *result, NSError *error) {// 在上传结果 result.location 中获取已上传文件的下载链接NSString * fileUrl = result.location;// 获取文件 crc64NSString * crc64 = [[result __originHTTPURLResponse__].allHeaderFields valueForKey:@"x-cos-hash-crc64ecma"];}];[put setInitMultipleUploadFinishBlock:^(QCloudInitiateMultipartUploadResult *multipleUploadInitResult,QCloudCOSXMLUploadObjectResumeData resumeData) {// 在初始化分块上传完成以后会回调该 block,在这里可以获取 resumeData,uploadidNSString* uploadId = multipleUploadInitResult.uploadId;}];[[QCloudCOSTransferMangerService defaultCOSTransferManager] UploadObject:put];
// 初始化 TransferManagerTransferConfig transferConfig = new TransferConfig.Builder().build();TransferManager transferManager = new TransferManager(cosXmlService,transferConfig);COSXMLDownloadTask cosxmlDownloadTask =transferManager.download(context,"examplebucket-1250000000", "exampleobject.txt", context.getExternalCacheDir().toString(), "exampleobject.txt");
QCloudCOSXMLDownloadObjectRequest * request = [QCloudCOSXMLDownloadObjectRequest new];// 存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.tencentcloud.com/cos5/bucketrequest.bucket = @"examplebucket-1250000000";// 对象键,是对象在 COS 上的完整路径,如果带目录的话,格式为 "video/xxx/movie.mp4"request.object = @"exampleobject";// 设置下载的路径 URL,如果设置了,对象将会被下载到指定路径中request.downloadingURL = [NSURL fileURLWithPath:@"Local File Path"];// 本地已下载的对象大小,如果是从头开始下载,请不要设置request.localCacheDownloadOffset = 100;// 监听下载结果[request setFinishBlock:^(id outputObject, NSError *error) {// outputObject 包含所有的响应 http 头部NSDictionary* info = (NSDictionary *) outputObject;// 若未指定下载路径,则通过下面方式获取下载的 NSData 数据。NSData * data = [outputObject __originHTTPResponseData__];}];// 监听下载进度[request setDownProcessBlock:^(int64_t bytesDownload,int64_t totalBytesDownload,int64_t totalBytesExpectedToDownload) {// bytesDownload 新增字节数// totalBytesDownload 本次下载接收的总字节数// totalBytesExpectedToDownload 本次下载的目标字节数}];[[QCloudCOSTransferMangerService defaultCOSTransferManager] DownloadObject:request];

condition: {// 限制上传对象必须小于 5MB'numeric_less_than_equal': {'cos:content-length': 5 * 1024 * 1024},}
var policy = JSON.stringify({...conditions: [['content-length-range', 1, 5 * 1024 * 1024], // 可限制上传对象大小范围比如1 - 5MB],});
condition: {// 限制上传对象 content-type 必须为图片类型'string_like': {'cos:content-type': 'image/*'}}
var policy = JSON.stringify({...conditions: [// 限制上传对象 content-type 必须为图片类型['starts-with', '$Content-Type', 'image/*'],],});
文档反馈