tencent cloud

Cloud Object Storage

Release Notes and Announcements
Release Notes
Announcements
Product Introduction
Overview
Features
Use Cases
Strengths
Concepts
Regions and Access Endpoints
Specifications and Limits
Service Regions and Service Providers
Billing
Billing Overview
Billing Method
Billable Items
Free Tier
Billing Examples
Viewing and Downloading Bill
Payment Overdue
FAQs
Getting Started
Console
Getting Started with COSBrowser
User Guide
Creating Request
Bucket
Object
Data Management
Batch Operation
Global Acceleration
Monitoring and Alarms
Operations Center
Data Processing
Content Moderation
Smart Toolbox
Data Processing Workflow
Application Integration
User Tools
Tool Overview
Installation and Configuration of Environment
COSBrowser
COSCLI (Beta)
COSCMD
COS Migration
FTP Server
Hadoop
COSDistCp
HDFS TO COS
GooseFS-Lite
Online Tools
Diagnostic Tool
Use Cases
Overview
Access Control and Permission Management
Performance Optimization
Accessing COS with AWS S3 SDK
Data Disaster Recovery and Backup
Domain Name Management Practice
Image Processing
Audio/Video Practices
Workflow
Direct Data Upload
Content Moderation
Data Security
Data Verification
Big Data Practice
COS Cost Optimization Solutions
Using COS in the Third-party Applications
Migration Guide
Migrating Local Data to COS
Migrating Data from Third-Party Cloud Storage Service to COS
Migrating Data from URL to COS
Migrating Data Within COS
Migrating Data Between HDFS and COS
Data Lake Storage
Cloud Native Datalake Storage
Metadata Accelerator
GooseFS
Data Processing
Data Processing Overview
Image Processing
Media Processing
Content Moderation
File Processing Service
File Preview
Troubleshooting
Obtaining RequestId
Slow Upload over Public Network
403 Error for COS Access
Resource Access Error
POST Object Common Exceptions
API Documentation
Introduction
Common Request Headers
Common Response Headers
Error Codes
Request Signature
Action List
Service APIs
Bucket APIs
Object APIs
Batch Operation APIs
Data Processing APIs
Job and Workflow
Content Moderation APIs
Cloud Antivirus API
SDK Documentation
SDK Overview
Preparations
Android SDK
C SDK
C++ SDK
.NET(C#) SDK
Flutter SDK
Go SDK
iOS SDK
Java SDK
JavaScript SDK
Node.js SDK
PHP SDK
Python SDK
React Native SDK
Mini Program SDK
Error Codes
Harmony SDK
Endpoint SDK Quality Optimization
Security and Compliance
Data Disaster Recovery
Data Security
Cloud Access Management
FAQs
Popular Questions
General
Billing
Domain Name Compliance Issues
Bucket Configuration
Domain Names and CDN
Object Operations
Logging and Monitoring
Permission Management
Data Processing
Data Security
Pre-signed URL Issues
SDKs
Tools
APIs
Agreements
Service Level Agreement
Privacy Policy
Data Processing And Security Agreement
Contact Us
Glossary

Bucket Policy

PDF
Mode fokus
Ukuran font
Terakhir diperbarui: 2025-06-12 17:43:52

Overview

This document provides an API overview of bucket policies and SDK example code.

Notes

COS C# SDK v5.4.39 or above is required.

Related Examples

Feature Name
Description
Sample code
PUT Bucket policy
Set the permission policy for the specified bucket.
GET Bucket policy
Query the permission policy for the specified bucket.
Deleting a Bucket Policy
Delete the permission policy for the specified bucket.

Preliminary Preparation: Initialize COS Service Instance

public class AccessManageModel
{
private CosXml cosXml; //Set the service user as a data member
// Initialize COS service instance
private void InitCosXml()
{
string region = Environment.GetEnvironmentVariable("COS_REGION");
CosXmlConfig config = new CosXmlConfig.Builder()
.SetRegion(region) // Set default region. For abbreviations of COS regions, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1
.Build();
string secretId = Environment.GetEnvironmentVariable("SECRET_ID"); // Cloud API key SecretId, see https://console.tencentcloud.com/cam/capi to obtain API key
string secretKey = Environment.GetEnvironmentVariable("SECRET_KEY"); // cloud API key SecretKey, obtain API key see https://console.tencentcloud.com/cam/capi
long durationSecond = 600; //Request signature valid duration for each request, in seconds
QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond);
this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);
}
}

Case

PUT Bucket policy

The PUT Bucket policy API can write a permission policy to a bucket. If the bucket already has a permission policy, the policy uploaded in this request will overwrite the existing one.
public void PutBucketPolicy()
{
string bucket = "examplebucket-1250000000";
PutBucketPolicyRequest request = new PutBucketPolicyRequest(bucket);
string policy = @"{
""Statement"": [
{
""Action"": [
""name/cos:PutBucketPolicy"",
""name/cos:GetBucketPolicy"",
""name/cos:DeleteBucketPolicy""
],
""Effect"": ""Allow"",
""Principal"": {
""qcs"": [
""qcs::cam::uin/100000000001:uin/100000000002""
]
},
""Resource"": [
""qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*""
]
}
],
""Version"": ""2.0""
}";
request.SetBucketPolicy(policy);
PutBucketPolicyResult result = cosXml.PutBucketPolicy(request);
Console.WriteLine(result.GetResultInfo());
}

Parameter Description

Parameter Name
Description
Required
Statement
Describe the detailed information of one or more permissions
Yes
Version
Syntax version of a policy, which is 2.0 by default.
Yes
Principal
Describe the entity authorized by the policy. For details, see Overview of Access Policy Language.
Yes
Action
Here refers to the COS API. Specify a combination of one or a sequence of operations or all operations (*) as required, for example, the action is name/cos:GetService. Please note that the case is case-sensitive.
Yes
Effect
The options include allow (permission) and deny (explicit deny).
Yes
Resource
Authorized data to be operated, which can be any resources, resources with a specified path prefix, resources with a specified absolute path, or resources with combinations of the above conditions.
Yes
Condition
Constraint conditions, which can be left blank. For details, see condition description.
No

GET Bucket policy

The GET Bucket policy API is used to read the permission policy of a bucket.
public void GetBucketPolicy()
{
string bucket = "examplebucket-1250000000";
GetBucketPolicyRequest request = new GetBucketPolicyRequest(bucket);
GetBucketPolicyResult result = cosXml.GetBucketPolicy(request);
Console.WriteLine(result.Data); //Return Data, in json format
Console.WriteLine(result.GetResultInfo());
}

Deleting a Bucket Policy

The DELETE Bucket policy request can be used to delete the permission policy for a bucket.
public void DeleteBucketPolicy()
{
string bucket = "examplebucket-1250000000";
DeleteBucketPolicyRequest request = new DeleteBucketPolicyRequest(bucket);
DeleteBucketPolicyResult result = cosXml.DeleteBucketPolicy(request);
Console.WriteLine(result.GetResultInfo());
}

API Operations

For the API interface description of setting a bucket policy, please see the PUT Bucket policy document.
For the API interface description of querying a bucket policy, please see the GET Bucket policy document.
For the API interface description of deleting a bucket policy, please see the DELETE Bucket policy document.

Bantuan dan Dukungan

Apakah halaman ini membantu?

masukan