Skip to content

Instantly share code, notes, and snippets.

@bwangelme
Created November 24, 2018 08:18
Show Gist options
  • Save bwangelme/94869277dfd146c1a2d018d1f28aca1e to your computer and use it in GitHub Desktop.
Save bwangelme/94869277dfd146c1a2d018d1f28aca1e to your computer and use it in GitHub Desktop.
上传内容到腾讯云
#!/usr/bin/env python3
# -*- coding: utf-8 -*-"
import uuid
import os
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
from qcloud_cos import CosServiceError
from qcloud_cos import CosClientError
# appid已在配置中移除,请在参数Bucket中带上appid。Bucket由bucketname-appid组成
# 1. 设置用户配置, 包括 secretId,secretKey 以及 Region
secret_id = '' # 替换为用户的 secretId
secret_key = '' # 替换为用户的 secretKey
region = '' # 替换为用户的 Region
token = '' # 使用临时秘钥需要传入Token,默认为空,可不填
config = CosConfig(
Secret_id=secret_id,
Secret_key=secret_key,
Region=region,
Token=token
)
# 2. 获取客户端对象
client = CosS3Client(config)
# 文件流 简单上传
FILE_PATH = '/Users/michaeltsui/Pictures/Life/avatar1.jpg'
file_name = "{}.jpg".format(uuid.uuid4())
with open(FILE_PATH, 'rb') as fp:
response = client.put_object(
Bucket='passage-1253400711', # Bucket由bucketname-appid组成
Body=fp,
Key=file_name,
StorageClass='STANDARD',
CacheControl='no-cache',
ContentDisposition='download.txt'
)
print response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment