Skip to content

Commit

Permalink
Merge pull request #18 from Sean10/master
Browse files Browse the repository at this point in the history
support TentcentCOS upload module
  • Loading branch information
kingname committed Oct 21, 2018
2 parents e085cc1 + d62bf2d commit 66845f6
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 25 deletions.
6 changes: 1 addition & 5 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
[[source]]

url = "https://pypi.python.org/simple"
verify_ssl = true
name = "pypi"


[packages]

pyparsing = "==2.1.10"
qiniu = "==7.0.7"
requests = "==2.10.0"

"cos-python-sdk-v5" = "*"

[dev-packages]

48 changes: 30 additions & 18 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions config/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
picture_folder = pic
picture_suffix = png
# now support SmUploader or QiniuUploader, the default is SmUploader
picture_host = SmUploader
picture_host = TencentCOSUploader

[QiniuUploader]
access_key = Q6sS422O0fafadfasdfahqCcCpF36tqvyQ75Zvzw
Expand All @@ -11,4 +11,10 @@ container_name = picturebed
url = http://7sbpmp.com1.z0.glb.clouddn.com/{}

[SmUploader]
url = https://sm.ms/api/upload
url = https://sm.ms/api/upload

[TencentCOSUploader]
secret_id =
secret_key =
region =
bucket =
55 changes: 55 additions & 0 deletions uploader/TencentCOSUploader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# -*- coding=utf-8
from qcloud_cos import CosConfig, CosS3Client
import sys
import os

class Uploader(object):
def __init__(self, config_info):
self.region = config_info.get('region')
self.config = CosConfig(Region=self.region,
SecretId=config_info.get('secret_id'),
SecretKey=config_info.get('secret_key'))
self.client = CosS3Client(self.config)
self.bucket = config_info.get('bucket')
self.url = 'https://'+self.bucket+'.cos.'+self.region+'.myqcloud.com/{}'


def upload(self, picture_path_list, link_only=False):
for picture_path in picture_path_list:
print(picture_path)
picture_name = os.path.basename(picture_path)
with open(picture_path, 'rb') as fp:
response = self.client.put_object(Bucket=self.bucket,
Body=fp,
Key=picture_name)
print(response['ETag'])
self.write_markdown_picture_url(picture_path_list, link_only)

def write_markdown_picture_url(self, picture_path_list, link_only=False):
uploaded_url_list = []
for picture_path in picture_path_list:
picture_name = os.path.basename(picture_path)
if link_only:
markdown_picture_url = self.url.format(picture_name)
uploaded_url_list.append(markdown_picture_url)
else:
markdown_picture_url = '![]({})'.format(self.url.format(picture_name))
uploaded_url_list.append(markdown_picture_url)
platform = sys.platform
command = ''
if platform == 'win32':
command = 'echo {} | clip'.format('\n'.join(uploaded_url_list))
elif platform == 'darwin':
command = 'echo "{}" | pbcopy'.format('\n'.join(uploaded_url_list))
os.system(command)
print('the url is already in your clipboard!')

if __name__ == '__main__':
config_info = {
"secret_id": "",
"secret_key": "",
"region": "",
"bucket": ""
}
uploader = Uploader(config_info)
uploader.upload(['fire.png'])

0 comments on commit 66845f6

Please sign in to comment.