-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcosFile.py
60 lines (51 loc) · 1.5 KB
/
cosFile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
file_name:fileOperation.py
app:Libs
funtion:operate the file of Tencent cos file
include:
1.function:up@upload file to Tencent cos
2.function:delete@delete the file on Tencent cos
data:2018/09/06
author:Ricky
"""
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
import sys
import json
import logging
import re
from django.conf import settings
'''
the client of cos
'''
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
config = CosConfig(Region=settings.REGION, SecretId=settings.SECRETID,
SecretKey=settings.SECRETKEY, Token=settings.TOKEN, Scheme=settings.SCHEMA)
# 2. 获取客户端对象
client = CosS3Client(config)
'''
function:upload file to Tencent cos
parameters@file:the object of file@file_name:the upload path and file name of file
return@str:the url of file
data:2018/09/06
author:Ricky
'''
def up(file, file_name):
response = client.put_object(Bucket='newsapp-1254170634', Body=file, Key=file_name,
StorageClass='STANDARD', ContentType='text/html; charset=utf-8')
return "https://newsapp-1254170634.cos.ap-chengdu.myqcloud.com/" + file_name
'''
function:delete file on Tencent cos
parameters@filePath:the file's path on Tencent cos
return@str:ok
data:2018/09/06
author:Ricky
'''
def delete(filePath):
try:
filePath = ('/'.join(filePath.split('/')[3:]))
response = client.delete_object(
Bucket='newsapp-1254170634', Key=filePath)
return True
except:
return False