|
| 1 | +# -*- coding=utf-8 |
| 2 | +from qcloud_cos import CosConfig |
| 3 | +from qcloud_cos import CosS3Client |
| 4 | +from qcloud_cos import CosServiceError |
| 5 | +from qcloud_cos import CosClientError |
| 6 | + |
| 7 | +import sys |
| 8 | +import logging |
| 9 | + |
| 10 | +# 腾讯云COSV5Python SDK, 目前可以支持Python2.6与Python2.7以及Python3.x |
| 11 | + |
| 12 | +# pip安装指南:pip install -U cos-python-sdk-v5 |
| 13 | + |
| 14 | +# cos最新可用地域,参照https://www.qcloud.com/document/product/436/6224 |
| 15 | + |
| 16 | +logging.basicConfig(level=logging.INFO, stream=sys.stdout) |
| 17 | + |
| 18 | +# 设置用户属性, 包括secret_id, secret_key, region |
| 19 | +# appid已在配置中移除,请在参数Bucket中带上appid。Bucket由bucketname-appid组成 |
| 20 | +secret_id = 'AKID15IsskiBQACGbAo6WhgcQbVls7HmuG00' # 替换为用户的secret_id |
| 21 | +secret_key = 'csivKvxxrMvSvQpMWHuIz12pThQQlWRW' # 替换为用户的secret_key |
| 22 | +region = 'ap-beijing' # 替换为用户的region |
| 23 | +token = None # 使用临时密钥需要传入Token,默认为空,可不填 |
| 24 | +config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象 |
| 25 | +client = CosS3Client(config) |
| 26 | + |
| 27 | +# 文件流 简单上传 |
| 28 | +file_name = 'test.txt' |
| 29 | +with open('test.txt', 'rb') as fp: |
| 30 | + response = client.put_object( |
| 31 | + Bucket='test04-123456789', # Bucket由bucketname-appid组成 |
| 32 | + Body=fp, |
| 33 | + Key=file_name, |
| 34 | + StorageClass='STANDARD', |
| 35 | + ContentType='text/html; charset=utf-8' |
| 36 | + ) |
| 37 | + print(response['ETag']) |
| 38 | + |
| 39 | +# 字节流 简单上传 |
| 40 | +response = client.put_object( |
| 41 | + Bucket='test04-123456789', |
| 42 | + Body=b'abcdefg', |
| 43 | + Key=file_name |
| 44 | +) |
| 45 | +print(response['ETag']) |
| 46 | + |
| 47 | +# 本地路径 简单上传 |
| 48 | +response = client.put_object_from_local_file( |
| 49 | + Bucket='test04-123456789', |
| 50 | + LocalFilePath='local.txt', |
| 51 | + Key=file_name, |
| 52 | +) |
| 53 | +print(response['ETag']) |
| 54 | + |
| 55 | +# 设置HTTP头部 简单上传 |
| 56 | +response = client.put_object( |
| 57 | + Bucket='test04-123456789', |
| 58 | + Body=b'test', |
| 59 | + Key=file_name, |
| 60 | + ContentType='text/html; charset=utf-8' |
| 61 | +) |
| 62 | +print(response['ETag']) |
| 63 | + |
| 64 | +# 设置自定义头部 简单上传 |
| 65 | +response = client.put_object( |
| 66 | + Bucket='test04-123456789', |
| 67 | + Body=b'test', |
| 68 | + Key=file_name, |
| 69 | + Metadata={ |
| 70 | + 'x-cos-meta-key1': 'value1', |
| 71 | + 'x-cos-meta-key2': 'value2' |
| 72 | + } |
| 73 | +) |
| 74 | +print(response['ETag']) |
| 75 | + |
| 76 | +# 高级上传接口(推荐) |
| 77 | +response = client.upload_file( |
| 78 | + Bucket='test04-123456789', |
| 79 | + LocalFilePath='local.txt', |
| 80 | + Key=file_name, |
| 81 | + PartSize=10, |
| 82 | + MAXThread=10 |
| 83 | +) |
| 84 | +print(response['ETag']) |
| 85 | + |
| 86 | +# 文件下载 获取文件到本地 |
| 87 | +response = client.get_object( |
| 88 | + Bucket='test04-123456789', |
| 89 | + Key=file_name, |
| 90 | +) |
| 91 | +response['Body'].get_stream_to_file('output.txt') |
| 92 | + |
| 93 | +# 文件下载 获取文件流 |
| 94 | +response = client.get_object( |
| 95 | + Bucket='test04-123456789', |
| 96 | + Key=file_name, |
| 97 | +) |
| 98 | +fp = response['Body'].get_raw_stream() |
| 99 | +print(fp.read(2)) |
| 100 | + |
| 101 | +# 文件下载 设置Response HTTP 头部 |
| 102 | +response = client.get_object( |
| 103 | + Bucket='test04-123456789', |
| 104 | + Key=file_name, |
| 105 | + ResponseContentType='text/html; charset=utf-8' |
| 106 | +) |
| 107 | +print(response['Content-Type']) |
| 108 | +fp = response['Body'].get_raw_stream() |
| 109 | +print(fp.read(2)) |
| 110 | + |
| 111 | +# 文件下载 指定下载范围 |
| 112 | +response = client.get_object( |
| 113 | + Bucket='test04-123456789', |
| 114 | + Key=file_name, |
| 115 | + Range='bytes=0-10' |
| 116 | +) |
| 117 | +fp = response['Body'].get_raw_stream() |
| 118 | +print(fp.read()) |
| 119 | + |
| 120 | +# 文件下载 捕获异常 |
| 121 | +try: |
| 122 | + response = client.get_object( |
| 123 | + Bucket='test04-123456789', |
| 124 | + Key='not_exist.txt', |
| 125 | + ) |
| 126 | + fp = response['Body'].get_raw_stream() |
| 127 | + print(fp.read(2)) |
| 128 | +except CosServiceError as e: |
| 129 | + print(e.get_origin_msg()) |
| 130 | + print(e.get_digest_msg()) |
| 131 | + print(e.get_status_code()) |
| 132 | + print(e.get_error_code()) |
| 133 | + print(e.get_error_msg()) |
| 134 | + print(e.get_resource_location()) |
| 135 | + print(e.get_trace_id()) |
| 136 | + print(e.get_request_id()) |
0 commit comments