Tencent Cloud COS object storage

foreword

It has been coming out after a long time, and I am too busy. This time I will bring Tencent Cloud COS object storage. It is also because COS happens to be used in the project, so I recorded it, hoping to be helpful to everyone.

Open COS

First of all, you need to activate COS, the process is as follows, open COS cloud storage on Tencent Cloud: https://cloud.tencent.com/product/cos, click to use immediately, scan the code to log in to the console,
insert image description here
refer to the product documentation to activate COS: https:// cloud.tencent.com/document/product/436. Enter the console and create a storage bucket in the console Create a
insert image description here
storage bucket according to the prompts: you can choose to have read and write for more private files, such as ID card photos, and you can choose public read and private write for files that can be viewed by users.
insert image description here
For example, avatar storage can be created using public read and write , and permissions and policies can be modified according to the situation. The policy needs to add policies related to file reading and writing, otherwise it cannot be uploaded.
insert image description hereSelect read and write permissions according to the situation. If there is no permission added here, it cannot be uploaded through JavaAPI
insert image description here

Key management

Accessing COS through the Java API requires a key, which can be obtained in the key management. The
account that can be created in the key management can be used in the Java code.
insert image description here
The SecretId and SecretKey of the main account can be used to operate COS, and sub-accounts can also be created.
insert image description here
Suggestions Use a sub-account sub-account, the master account has too high authority, there are security issues
insert image description here
After creating a sub-account - authorize the created sub-account, you need to associate the read and write permissions of COS

insert image description here
insert image description here

After authorization, click on the user list - click on the new sub-account to create a key
insert image description here

JavaAPI access to COS

Java quick start reference: https://cloud.tencent.com/document/product/436/10199

<dependency>
    <groupId>com.qcloud</groupId>
    <artifactId>cos_api</artifactId>
    <version>5.6.97</version>
</dependency>

According to the file upload code modified by the official website

public static void main(String[] args) {
    
    

    // 1 初始化用户身份信息(secretId, secretKey)。
    // SECRETID 和 SECRETKEY 请登录访问管理控制台 https://console.cloud.tencent.com/cam/capi 进行查看和管理
    String secretId = "";//用户的 SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
    String secretKey = "";//用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
    COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
    // 2 设置 bucket 的地域, COS 地域的简称请参见 https://cloud.tencent.com/document/product/436/6224
    // clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
    Region region = new Region("ap-chengdu");
    ClientConfig clientConfig = new ClientConfig(region);
    // 这里建议设置使用 https 协议
    // 从 5.6.54 版本开始,默认使用了 https
    clientConfig.setHttpProtocol(HttpProtocol.https);
    // 3 生成 cos 客户端。
    COSClient cosClient = new COSClient(cred, clientConfig);

    // 指定要上传的文件
    File localFile = new File("C:\\Users\\Administrator\\Desktop\\logo.png");
    // 指定文件将要存放的存储桶
    String bucketName = "存储桶的名字";
    // 指定文件上传到 COS 上的路径,即对象键。例如对象键为 folder/picture.jpg,则表示将文件 picture.jpg 上传到 folder 路径下
    String key = "driver/test2.png";
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
    PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
	
    System.out.println(putObjectResult.getDateStr());
    cosClient.shutdown();
}

Execute the main method, and then go to the cos console to view the file
insert image description here

Image review

Sensitive image review is required for image files, which is realized through Tencent Cloud's Data Vientiane CI. First, let's activate Data Vientiane CI, find Data Vientiane in Tencent Cloud products, click to use immediately, click to activate immediately, and click to authorize now - agree
insert image description here
to
insert image description here
authorize

insert image description here
Click Bind Bucket - Paste the name of the bucket - Click OK

insert image description here
Code access image review: reference document: https://cloud.tencent.com/document/product/460/57227

//1.创建任务请求对象
ImageAuditingRequest request = new ImageAuditingRequest();
//2.添加请求参数 参数详情请见 API 接口文档
//2.1设置请求 bucket
request.setBucketName("examplebucket-1250000000");
//2.2设置审核类型
request.setDetectType("porn,ads");
//2.3设置 bucket 中的图片位置
request.setObjectKey("1.png");
//3.调用接口,获取任务响应对象
ImageAuditingResponse response = client.imageAuditing(request);

Description of the returned result: the returned response has a Result attribute, which means as follows

This field indicates the audit result of this judgment, and you can perform subsequent operations based on the result; it is recommended that you deal with different audit results according to your business needs. Valid values: 0 (approval is normal), 1 (determined as illegal sensitive file), 2 (suspected to be sensitive, manual review is recommended).

The end of the article hope to help you

Guess you like

Origin blog.csdn.net/u014494148/article/details/131875882
Recommended