12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- # -*- coding: utf-8 -*-
- import os
- import oss2
- import requests
- import uuid
- import time
- from config import *
- class TedOSS:
- def __init__(self):
- """
- """
- self.AccessKeyID = 'LTAIbkpKrzQViemJ'
- self.AccessKeySecret = '9y9mqNFMMDfUEBGvWqHt3wGas2W5ML'
- self.bucket_name = 'scxjcclub'
- self.root_name = 'say365'
- self.auth = oss2.Auth(self.AccessKeyID, self.AccessKeySecret)
- self.endpoint = 'http://oss-cn-beijing.aliyuncs.com'
- self.bucket = oss2.Bucket(self.auth, self.endpoint, self.bucket_name)
- self.domain = 'http://scxjcclub.oss-cn-beijing.aliyuncs.com'
- def upload_from_str(self,content_str=None,filename=None):
- '''
- 通过字符串上传
- byte,unicode,str
- '''
- filename = os.path.join(self.root_name,filename)
- result = self.bucket.put_object(filename,content_str)
- if result.status == 200:
- url = os.path.join(self.domain,filename)
- else:
- url = ''
- return url
- def upload_from_local(self, localfile=None, ossfile=None):
- '''
- 上传本地文件到oss
- '''
- ossfile = os.path.join(self.root_name, ossfile).replace("\\","/")
- with open(localfile, 'rb') as fileobj:
- result = self.bucket.put_object(ossfile, fileobj)
- if result.status == 200:
- url = os.path.join(self.domain, ossfile).replace("\\","/")
- else:
- url = ''
- xlog.info(url)
- return url
- def resumable_upload_from_local(self, localfile=None, ossfile=None):
- ossfile = os.path.join(self.root_name, ossfile)
- result = oss2.resumable_upload(self.bucket,ossfile,localfile,
- store=oss2.ResumableStore(root='/tmp'),
- multipart_threshold=100*1024,
- part_size=100*1024,
- num_threads=4,
- )
- print result.status
- if result.status == 200:
- url = os.path.join(self.domain,ossfile)
- else:
- url = ''
- return url
- def upload_from_url(self, url=None, ossfile=None):
- '''
- '''
- ossfile = os.path.join(self.root_name,ossfile)
-
- resp = requests.get(url)
- result = self.bucket.put_object(ossfile,resp)
- if result.status == 200:
- url = os.path.join(self.domain,ossfile)
- else:
- url = ''
- return url
- if __name__ == '__main__':
- myoss = TedOSS()
- localfile = 'D:\\docxdemo\\test.docx'
- ossfile = 'test.docx'
- url = myoss.upload_from_local(localfile,ossfile)
- print url
|