

// Bucket region abbreviation, for example, the Guangzhou region is ap-guangzhouString region = "COS_REGION" ;// Byte array of the tls client certificate, which must be in BKS formatbyte[] certificateBytes;// Password for the BKS file. If your BKS file has no password, pass nullString password = "123456";// Server host configured with mutual authenticationString dualCheckHost = "dual-check.myEdgeOne.com";CosXmlServiceConfig cosXmlServiceConfig = new CosXmlServiceConfig.Builder().isHttps(true).setRegion(region).setHost(dualCheckHost)// Set the tls client certificate.setClientCertificate(certificateBytes, password).builder();CosXmlService cosXmlService = new CosXmlService(context, cosXmlServiceConfig,credentialProvider);
QCloudServiceConfiguration* configuration = [QCloudServiceConfiguration new];QCloudCOSXMLEndPoint* endpoint = [[QCloudCOSXMLEndPoint alloc] init];// Replace with your region. The region to which the created bucket belongs can be viewed in the console at https://console.tencentcloud.com/cos5/bucket// For a list of all regions supported by COS, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1endpoint.regionName = @"COS_REGION";// Use HTTPSendpoint.useHTTPS = true;configuration.endpoint = endpoint;// Configure the client p12 certificate.NSString *path = [[NSBundle mainBundle] pathForResource:@"client" ofType:@"p12"];NSData *p12Data = [NSData dataWithContentsOfFile:path];configuration.clientCertificateData = p12Data;// Configure the certificate passwordconfiguration.password = @"123456";// Example of initializing the COS service[QCloudCOSXMLService registerDefaultCOSXMLWithConfiguration:configuration];[QCloudCOSTransferMangerService registerDefaultCOSTransferMangerWithConfiguration:configuration];
// Prepare TransferManagerTransferConfig transferConfig = new TransferConfig.Builder().build();TransferManager transferManager = new TransferManager(cosXmlService,transferConfig);// Use server-side encryption (SSE-COS) with COS-managed encryption keys to protect dataPutObjectRequest putObjectRequestCos = new PutObjectRequest(bucket, cosPath, srcPath);putObjectRequestCos.setCOSServerSideEncryption();COSXMLUploadTask cosxmlUploadTaskCos = transferManager.upload(putObjectRequestCos, uploadId);// Set server-side encryption (SSE-KMS) using customer-provided master keys to protect dataString customKey = "Customer Master Key CMK";String encryptContext = "Encryption Context";PutObjectRequest putObjectRequestKms = new PutObjectRequest(bucket, cosPath, srcPath);try {putObjectRequestKms.setCOSServerSideEncryptionWithKMS(customKey, encryptContext);} catch (CosXmlClientException e) {e.printStackTrace();}COSXMLUploadTask cosxmlUploadTaskKms = transferManager.upload(putObjectRequestKms, uploadId);// Set server-side encryption (SSE-C) using customer-provided encryption keys to protect dataString customKeyC = "Server-Side Encryption Key";PutObjectRequest putObjectRequestC = new PutObjectRequest(bucket, cosPath, srcPath);try {putObjectRequestC.setCOSServerSideEncryptionWithCustomerKey(customKey);} catch (CosXmlClientException e) {e.printStackTrace();}// Upload objectCOSXMLUploadTask cosxmlUploadTaskC = transferManager.upload(putObjectRequestC, uploadId);
// 1. Use server-side encryption (SSE-COS) with COS-managed encryption keys to protect dataQCloudCOSXMLUploadObjectRequest *request = [QCloudCOSXMLUploadObjectRequest new];[request setCOSServerSideEncyption];// 2. Use server-side encryption (SSE-C) with customer-provided encryption keys to protect dataQCloudCOSXMLUploadObjectRequest *request = [QCloudCOSXMLUploadObjectRequest new];NSString *customKey = @"123456qwertyuioplkjhgfdsazxcvbnm";[request setCOSServerSideEncyptionWithCustomerKey:customKey];// 3. Use server-side encryption (SSE-KMS) with KMS-managed encryption keys to protect dataQCloudCOSXMLUploadObjectRequest *request = [QCloudCOSXMLUploadObjectRequest new];NSString *customKey = @"123456qwertyuioplkjhgfdsazxcvbnm";NSString *arrJsonStr = @"{\\"key\\":\\"value\\"}";[request setCOSServerSideEncyptionWithKMSCustomKey:customKey jsonStr:arrJsonStr];
// Initialize TransferManagerTransferConfig transferConfig = new TransferConfig.Builder().build();TransferManager transferManager = new TransferManager(cosXmlService,transferConfig);// Upload objectString srcPath = new File(context.getCacheDir(), "exampleobject.txt").toString(); // Absolute path of the local fileCOSXMLUploadTask cosxmlUploadTask = transferManager.upload("examplebucket-1250000000", "exampleobject.txt",srcPath, null);
QCloudCOSXMLUploadObjectRequest* put = [QCloudCOSXMLUploadObjectRequest new];NSURL* url = [NSURL fileURLWithPath:@"File URL"];// Bucket name, which consists of BucketName-Appid, can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucketput.bucket = @"examplebucket-1250000000";// Object key, which is the full path of the object on COS. If including a directory, the format should be "video/xxx/movie.mp4"put.object = @"exampleobject";// The content of the object to be uploaded. You can pass in a variable of type NSData* or NSURL*put.body = url;// Monitor upload progress[put setSendProcessBlock:^(int64_t bytesSent,int64_t totalBytesSent,int64_t totalBytesExpectedToSend) {// bytesSent The number of bytes to be sent this time (a large file may need to be sent in multiple parts)// totalBytesSent The number of bytes sent// totalBytesExpectedToSend The total number of bytes to be sent in this upload (which is the size of a single file)}];// Monitor upload result[put setFinishBlock:^(QCloudUploadObjectResult *result, NSError *error) {// Obtain the download link of the uploaded file from result.location in the upload resultNSString * fileUrl = result.location;// Obtain the file crc64NSString * crc64 = [[result __originHTTPURLResponse__].allHeaderFields valueForKey:@"x-cos-hash-crc64ecma"];}];[put setInitMultipleUploadFinishBlock:^(QCloudInitiateMultipartUploadResult *multipleUploadInitResult,QCloudCOSXMLUploadObjectResumeData resumeData) {// After the multipart upload initialization is completed, this block will be called back, where you can obtain resumeData and uploadidNSString* uploadId = multipleUploadInitResult.uploadId;}];[[QCloudCOSTransferMangerService defaultCOSTransferManager] UploadObject:put];
// Initialize 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];// Bucket name, which consists of BucketName-Appid, can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucketrequest.bucket = @"examplebucket-1250000000";// Object key, which is the full path of the object on COS. If including a directory, the format should be "video/xxx/movie.mp4"request.object = @"exampleobject";// Set the download path URL. If specified, the object will be downloaded to the designated path.request.downloadingURL = [NSURL fileURLWithPath:@"Local File Path"];// The size of the locally downloaded object. If downloading from the beginning, do not specify this value.request.localCacheDownloadOffset = 100;// Monitor download result[request setFinishBlock:^(id outputObject, NSError *error) {// outputObject contains all the response http headersNSDictionary* info = (NSDictionary *) outputObject;// If no download path is specified, obtain the downloaded NSData in the following manner.NSData * data = [outputObject __originHTTPResponseData__];}];// Monitor download progress[request setDownProcessBlock:^(int64_t bytesDownload,int64_t totalBytesDownload,int64_t totalBytesExpectedToDownload) {// bytesDownload Bytes added// totalBytesDownload Total bytes received during this download// totalBytesExpectedToDownload Target bytes for this download}];[[QCloudCOSTransferMangerService defaultCOSTransferManager] DownloadObject:request];

condition: {// Restrict uploaded objects to less than 5MB'numeric_less_than_equal': {'cos:content-length': 5 * 1024 * 1024},}
var policy = JSON.stringify({...conditions: [['content-length-range', 1, 5 * 1024 * 1024], // Restrict the size range of uploaded objects, e.g., 1 - 5MB],});
condition: {// Restrict the content-type of uploaded objects to image types'string_like': {'cos:content-type': 'image/*'}}
var policy = JSON.stringify({...conditions: [// Restrict the content-type of uploaded objects to image types['starts-with', '$Content-Type', 'image/*'],],});
Was this page helpful?
You can also Contact sales or Submit a Ticket for help.
Help us improve! Rate your documentation experience in 5 mins.
Feedback