文章目录
- [1. 开通服务](#1. 开通服务)
- [2. 创建存储桶](#2. 创建存储桶)
- [3. 手动上传文件并查看](#3. 手动上传文件并查看)
- [4. python上传文件](#4. python上传文件)
-
- [4.1 找到sdk文档](#4.1 找到sdk文档)
- [4.2 初始化代码](#4.2 初始化代码)
- [4.3 region获取](#4.3 region获取)
- [4.4 secret_id和secret_key获取](#4.4 secret_id和secret_key获取)
- [4.5 上传对象代码](#4.5 上传对象代码)
- [4.6 python实现上传文件](#4.6 python实现上传文件)
- [5 python创建桶](#5 python创建桶)
首先来到腾讯云官网
https://cloud.tencent.com/
1. 开通服务

来到如下页面



2. 创建存储桶


根据自己的需要进行选择


创建好后就会来到这个页面

3. 手动上传文件并查看
点击"上传图片"

上传完成后继续点击




4. python上传文件
4.1 找到sdk文档





然后我们就可以看到对象存储SDK的使用文档了,跟着它一步一步做就能实现。
下面直接介绍代码:
终端安装sdk
pip install -U cos-python-sdk-v5
4.2 初始化代码
python
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
import sys
import os
# 1. 设置用户属性, 包括 secret_id, secret_key, region等。Appid 已在 CosConfig 中移除,请在参数 Bucket 中带上 Appid。Bucket 由 BucketName-Appid 组成
secret_id = os.environ['COS_SECRET_ID'] # 用户的 SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
secret_key = os.environ['COS_SECRET_KEY'] # 用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
region = 'ap-beijing' # 替换为用户的 region,已创建桶归属的 region 可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket
# COS 支持的所有 region 列表参见 https://cloud.tencent.com/document/product/436/6224
token = None # 如果使用永久密钥不需要填入 token,如果使用临时密钥需要填入,临时密钥生成和使用指引参见 https://cloud.tencent.com/document/product/436/14048
scheme = 'https' # 指定使用 http/https 协议来访问 COS,默认为 https,可不填
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)
client = CosS3Client(config)
上面需要做改动的就只有secret_id、secret_key和region
4.3 region获取
region就是下面这个东西(就是你在创建桶时选择的区域,我上面选择的是中国-重庆)

4.4 secret_id和secret_key获取

这里会跳出一个框框,选择继续使用就好

点击新建密钥
注意:密钥不能公开,只能自己知道。

4.5 上传对象代码
这只是上传文件的其中一部分代码,不要混淆
python
#### 高级上传接口(推荐)
# 根据文件大小自动选择简单上传或分块上传,分块上传具备断点续传功能。
response = client.upload_file(
Bucket='examplebucket-1250000000', # 桶的名字
LocalFilePath='local.txt', # 本地文件路径
Key='picture.jpg', # 上传到桶之后的文件名
# 后面这三个是参数的默认值,可以删了
PartSize=1,
MAXThread=10,
EnableMD5=False
)
print(response['ETag'])

4.6 python实现上传文件
纯净版代码:
python
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
secret_id = '填入你的id'
secret_key = '填入你的key'
region = 'ap-chongqing'
token = None
scheme = 'https'
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)
client = CosS3Client(config)
response = client.upload_file(
Bucket='ly-1321630892', # 桶的名字
LocalFilePath='02.jpg', # 本地文件路径
Key='02.jpg', # 上传到桶之后的文件名
)
print(response['ETag'])
执行代码后去腾讯查看

5 python创建桶
这是sdk文档的内容,其实相对于上传文件的代码,就只改变了最后一点response等于的那里。
python
# -*- coding=utf-8
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
import sys
import os
import logging
# 正常情况日志级别使用 INFO,需要定位时可以修改为 DEBUG,此时 SDK 会打印和服务端的通信信息
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
# 1. 设置用户属性, 包括 secret_id, secret_key, region等。Appid 已在 CosConfig 中移除,请在参数 Bucket 中带上 Appid。Bucket 由 BucketName-Appid 组成
secret_id = os.environ['COS_SECRET_ID'] # 用户的 SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
secret_key = os.environ['COS_SECRET_KEY'] # 用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
region = 'ap-beijing' # 替换为用户的 region,已创建桶归属的 region 可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket
# COS 支持的所有 region 列表参见 https://cloud.tencent.com/document/product/436/6224
token = None # 如果使用永久密钥不需要填入 token,如果使用临时密钥需要填入,临时密钥生成和使用指引参见 https://cloud.tencent.com/document/product/436/14048
scheme = 'https' # 指定使用 http/https 协议来访问 COS,默认为 https,可不填
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)
client = CosS3Client(config)
response = client.create_bucket(
Bucket='examplebucket-1250000000'
)
纯净版:
python
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
secret_id = '--'
secret_key = '--'
region = 'ap-chengdu'
# 这两个也是参数的默认值,可以去掉
token = None
scheme = 'https'
# 去掉后下面这里传参的时候也要去掉
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)
client = CosS3Client(config)
response = client.create_bucket(
Bucket='test-1250000000', # 存储桶的名称
ACL='public-read' # 访问权限 有 private/public-read/public-read-write
)
执行多次一直出现下面这个报错。
报错:
qcloud_cos.cos_exception.CosServiceError: {'code': 'AccessDenied', 'message': 'Access Denied.', 'resource': '/', 'requestid': 'NjU0ZGQwODdfZmNiNTU4NjRfMWFkNV83ZThkZmE1', 'traceid': 'OGVmYzZiMmQzYjA2OWNhODk0NTRkMTBiOWVmMDAxODc0OWRkZjk0ZDM1NmI1M2E2MTRlY2MzZDhmNmI5MWI1OTA2NzIxMzRkNDExNDJiYWZmM2ExNTVhMjIxMzhjNDI2M2YzZjRmYTFmYjgxMGRlNzZmMDhlZTgzMjhmODA1Mjc='}
查了后发现是Bucket后面那一串数字没有改,应该用自己的。在创建存储桶那里可以看到

创建桶的最终代码:
python
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
secret_id = '--'
secret_key = '--'
region = 'ap-chengdu'
token = None
scheme = 'https'
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)
client = CosS3Client(config)
response = client.create_bucket(
Bucket='test-1321630892',
ACL='public-read'
)
print(response)
