python oss批量上传_python 批量上传文件到阿里云oss,并写入Excel,存到本地

最近公司要往阿里云oss上传视频,大小差不多有200G,原先让运营去一个一个的添加,但是这能麻烦死人,所以就让技术去批量上传。所以研究了一下用python往oss上传视频

首先需引用以下几个模块

pip install oss2

pip install tablib

pip install pyexcel-xlsx

其次因为本地的视频都是比如中文.mp4这样的,所以引入一个随机字符串

import random

def generate_random_str(randomlength=8):

"""

生成一个指定长度的随机字符串

"""

random_str = ''

base_str = 'ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789'

length = len(base_str) - 1

for i in range(randomlength):

random_str += base_str[random.randint(0, length)]

return random_str

因为是文件夹里面套文件夹,但是只上传文件,所以需要获取子文件夹下面的视频

代码如下:

def upload(dir):

fs = os.listdir(dir)

for f in fs:

file = dir + "/" + f

if os.path.isdir(file):

upload(file)

else:

if 'DS_Store' not in file and 'png' not in f and 'JPG' not in f:

putAliyun(file, f)

备注:因为文件夹里面还有图片,所以去除后缀为png、JPG的图片。因为用的是mac上传,所以文件夹里面有.DS_Store,所以也需要去除

所以这个的整个代码如下:

#!/usr/bin/env python

# ! -*- coding:utf-8 -*-

import oss2

import random

import os

import tablib

import time

ossDir = '/Users/FQY/Desktop/upload'

key = XXX

secret = XXX

bucketname = XXX

dataset = tablib.Dataset()

header = ('title', 'url')

dataset.headers = header

def generate_random_str(randomlength=8):

"""

生成一个指定长度的随机字符串

"""

random_str = ''

base_str = 'ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789'

length = len(base_str) - 1

for i in range(randomlength):

random_str += base_str[random.randint(0, length)]

return random_str

def getmkname(path):

remoteName = path.replace(ossDir, '')

dir_names = remoteName.split('/')

dir_names.pop()

res = filter(None, dir_names)

mkdir_name = '-'.join(res)

return mkdir_name

#获得上传的时长

def progress_callback(bytes_consumed, total_bytes):

print('bytes_consumed is {}'.format(bytes_consumed))

print('total_bytes is {}'.format(total_bytes))

def putAliyun(path, f):

key = 'language/' + str(int(time.time())) + generate_random_str() + '.mp4'

auth = oss2.Auth(key, secret)

bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', bucketname)

result = bucket.put_object_from_file(key=key, filename=path,progress_callback=progress_callback)

if result.status == 200:

aliyun = 'http://video.oss-cn-hangzhou.aliyuncs.com/{}'.format(key)

title = '【{}】{}'.format(getmkname(path), f.split('.mp4')[0])

dataset.append([title, aliyun])

else:

print('upload fail,error code', result.status)

def upload(dir):

fs = os.listdir(dir)

for f in fs:

file = dir + "/" + f

if os.path.isdir(file):

upload(file)

else:

if 'DS_Store' not in file and 'png' not in f and 'JPG' not in f:

putAliyun(file, f)

upload(ossDir)

myfile = open('/Users/FQY/Desktop/mydata_video.xlsx', 'wb')

myfile.write(dataset.xlsx)

myfile.close()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值