Cross-domain Access

Last updated: 2020-03-25 16:06:32

PDF

Note

This document provides an overview of API on cross-domain Access and sample SDK code.

API Operation name pedagogical operation
PUT Bucket cors Set cross-domain configuration Set up Bucket's cross-domain name Access and Permission
GET Bucket cors Query cross-domain configuration Query Bucket's cross-domain name Access configuration information
DELETE Bucket cors Delete cross-domain configuration Delete Bucket's cross-domain name Access configuration information

Set cross-domain configuration

Function description

Set the cross-domain name Access configuration information (PUT Bucket cors) of specified Bucket.

Method prototype

PutBucketCORSResult PutBucketCORS(PutBucketCORSRequest request);

void PutBucketCORS(PutBucketCORSRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);

Request Sample

CosXmlConfig config = new CosXmlConfig.Builder()
  .SetConnectionTimeoutMs(60000)  
  .SetReadWriteTimeoutMs(40000)  
  .IsHttps(true)  
  .SetAppid("1250000000") 
  .SetRegion("COS_REGION") 
  .Build();

string secretId = "COS_SECRETID";   
string secretKey = "COS_SECRETKEY"; 
long durationSecond = 600;         
QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, 
  secretKey, durationSecond);

CosXml cosXml = new CosXmlServer(config, qCloudCredentialProvider);

try
{
  string bucket = "examplebucket-1250000000"; 
  PutBucketCORSRequest request = new PutBucketCORSRequest(bucket);
  request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
  COSXML.Model.Tag.CORSConfiguration.CORSRule corsRule = 
    new COSXML.Model.Tag.CORSConfiguration.CORSRule();
  corsRule.id = "corsconfigureId";
  corsRule.maxAgeSeconds = 6000;
  corsRule.allowedOrigin = "http://intl.cloud.tencent.com";

  corsRule.allowedMethods = new List<string>();
  corsRule.allowedMethods.Add("PUT");

  corsRule.allowedHeaders = new List<string>();
  corsRule.allowedHeaders.Add("Host");

  corsRule.exposeHeaders = new List<string>();
  corsRule.exposeHeaders.Add("x-cos-meta-x1");

  request.SetCORSRule(corsRule);

  PutBucketCORSResult result = cosXml.PutBucketCORS(request);
  Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
  Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
  Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}

Parameter description

Parameter name Setting method Description Type
Bucket Construction method Bucket's name, format: BucketName-APPID String
CorsRule SetCORSRule Set up Bucket's cross-domain Access match CORSConfiguration.CORSRule
SignStartTimeSecond SetSign Start time of signature validity (Unix timestamp), for example, 1557902800 Long
DurationSecond SetSign The validity period of the signature (in seconds). For example, the validity period of the signature is 1 minute: 60. Long
HeaderKeys SetSign Signed request headerList<string> QueryParameterKeys

Request result is returned through PutBucketCORSResult.

Member variable Type Description
HttpCode Int HTTP Code, [200,300) indicates that the operation was successful, otherwise it indicates that the operation failed.## Query cross-domain configuration

Function description

Query the cross-domain name Access configuration information (GET Bucket cors) of specified Bucket.

Method prototype

GetBucketCORSResult GetBucketCORS(GetBucketCORSRequest request);

void GetBucketCORS(GetBucketCORSRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);

Request Sample

CosXmlConfig config = new CosXmlConfig.Builder()
  .SetConnectionTimeoutMs(60000)  
  .SetReadWriteTimeoutMs(40000)  
  .IsHttps(true) 
  .SetAppid("1250000000") 
  .SetRegion("COS_REGION") 
  .Build();

string secretId = "COS_SECRETID";   
string secretKey = "COS_SECRETKEY"; 
long durationSecond = 600;          
QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, 
  secretKey, durationSecond);

CosXml cosXml = new CosXmlServer(config, qCloudCredentialProvider);

try
{
  string bucket = "examplebucket-1250000000"; 
  GetBucketCORSRequest request = new GetBucketCORSRequest(bucket);
  request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
  GetBucketCORSResult result = cosXml.GetBucketCORS(request);
  CORSConfiguration conf = result.corsConfiguration;
}
catch (COSXML.CosException.CosClientException clientEx)
{
  Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
  Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}

Parameter description

Parameter name Setting method Description Type
Bucket Construction method Bucket's name, format: BucketName-APPID String
SignStartTimeSecond SetSign Start time of signature validity (Unix timestamp), for example, 1557902800 Long
DurationSecond SetSign The validity period of the signature (in seconds). For example, the validity period of the signature is 1 minute: 60. Long
HeaderKeys SetSign Signed request headerList<string> QueryParameterKeys

Request result is returned through GetBucketCORSResult.

Member variable Type Description
HttpCode Int HTTP Code, [200,300) indicates that the operation was successful, otherwise it indicates that the operation failed.

Delete cross-domain configuration

Function description

Delete the cross-domain name Access configuration (DELETE Bucket cors) of the specified Bucket.

Method prototype

DeleteBucketCORSResult DeleteBucketCORS(DeleteBucketCORSRequest request);

void DeleteBucketCORS(DeleteBucketCORSRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);

Request Sample

CosXmlConfig config = new CosXmlConfig.Builder()
  .SetConnectionTimeoutMs(60000)  
  .SetReadWriteTimeoutMs(40000)  
  .IsHttps(true)  
  .SetAppid("1250000000") 
  .SetRegion("COS_REGION") 
  .Build();

string secretId = "COS_SECRETID";   
string secretKey = "COS_SECRETKEY"; 
long durationSecond = 600;          
QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, 
  secretKey, durationSecond);

CosXml cosXml = new CosXmlServer(config, qCloudCredentialProvider);

try
{
  string bucket = "examplebucket-1250000000"; 
  DeleteBucketCORSRequest request = new DeleteBucketCORSRequest(bucket);
  request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
  DeleteBucketCORSResult result = cosXml.DeleteBucketCORS(request);
  Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
  Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
  Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}

Parameter description

Parameter name Setting method Description Type
Bucket Construction method Bucket's name, format: BucketName-APPID String
SignStartTimeSecond SetSign Start time of signature validity (Unix timestamp), for example, 1557902800 Long
DurationSecond SetSign The validity period of the signature (in seconds). For example, the validity period of the signature is 1 minute: 60. Long
HeaderKeys SetSign Signed request headerList<string> QueryParameterKeys

Request result is returned through DeleteBucketCORSResult.

Member variable Type Description
HttpCode Int HTTP Code, [200,300) indicates that the operation was successful, otherwise it indicates that the operation failed.