oss对象云存储

时间:2024-01-04 20:51:44

import qiniu 
import uuid
import config

def qn_upload_voice(fileData):
'''上传语音到七牛云
@arg: fileData - 编码的MP3文件DATA
'''
q = qiniu.Auth(config.qiniu_access_key, config.qiniu_secret_key)
token = q.upload_token(config.qiniu_record_bucket_name, None, 3600, policy={})
key = '%s/uservoice/%s' % (uuid.uuid4().hex+".mp3", filename)
#要进行转码的转码操作。
fops = "avthumb/mp3/ab/320k/ar/44100/acodec/libmp3lame"
saveas_key = qiniu.urlsafe_base64_encode(config.qiniu_record_bucket_name+':'+key) #对转码后的文件进行使用saveas参数自定义命名
pfop = qiniu.PersistentFop(q, config.qiniu_record_bucket_name, 'mpsdemo') #mpsdemo为使用的队列名称
ops = [fops+'|saveas/'+saveas_key]
ret, info = pfop.execute(key, ops, 1)
ret, info = qiniu.put_data(token, key, fileData)
return key

import ffmpeg
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client

def tx_upload_voice(fileData):
'''上传语音到腾讯云
@arg: fileData - 编码的MP3文件DATA
'''
out, err = ffmpeg.input('pipe:', format='amr').output('pipe:', format='mp3').run(input=fileData, capture_stdout=True)   #ffmpeg 管道 语音转码 mp3 
secret_id = config.tencent_secret_id
secret_key = config.tencent_secret_key
region = 'ap-chengdu'
token = None
scheme = 'https'
txconfig = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)
client = CosS3Client(txconfig)
filename = uuid.uuid4().hex+".mp3" #客户端对象
result = client.put_object(
Bucket = config.tencent_record_bucket,
Body = fileData,
Key = 'uservoice/%s' % filename,
StorageClass = 'STANDARD',
EnableMD5 = False
)
if not result['ETag']:
raise Exception('上传失败')
else:
return filename

#oss对象统一接口函数

def uploadVoice(fileData):
'''上传语音到云存储
@arg: fileData - 编码的MP3文件DATA
'''
return tx_upload_voice(fileData)