阿里雲OSS本地批量上傳指令碼工具

2021-10-23 18:30:08 字數 2990 閱讀 4679

使用需要提前安裝 pip install oss2 模組

上傳到目錄時,儲存到oss上key取目錄內檔案或者目錄列表的相對路徑:

如:d:/demo/ 資料夾,資料夾內有,dir1/f1, dir2/f2, f3這幾個檔案,則儲存到oss上key取dir1/f1,dir2/f2,f3

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

# author: tom

# description: ali oss tool for upload local file or dir

# date: 2023年8月24日

# usage: python alioss_uploader.py d:\workspace\web "\\resources\\|\\web-inf\\|\\wx\\|.*.jsp|.*.swf", first arg is upload path, second arg is exclude pattern

import oss2

import os

import sys

import re

import logging

# import asyncio

from concurrent import futures

console_handler = logging.streamhandler()

file_handler = logging.filehandler(filename='./upload.log', mode='w')

log_format = '%(asctime)s %(threadname)s-%(thread)d %(levelname)s: %(message)s'

formatter = logging.formatter(log_format)

logging.basicconfig(filename='./upload.log', filemode='w', format=log_format

, datefmt='%y/%m/%d %h:%m:%s'

, level=logging.info)

console_handler.setformatter(formatter)

file_handler.setformatter(formatter)

log = logging.getlogger('alioss_uploader')

log.setlevel(logging.info)

log.addhandler(console_handler)

log.addhandler(file_handler)

auth = oss2.auth('accesskey', 'secretkey')

bucket = oss2.bucket(auth, '', 'mybucket')

# bucket = oss2.bucket(auth, '', 'mybucket')

upload_path = none

def upload_to_oss(file):

with open(file, 'rb') as fileobj:

try:

upload_key = file.replace(upload_path, '').replace('\\', '/')[1:]

log.info(f'upload: , key: ')

result = bucket.put_object(upload_key, fileobj)

log.info(f'upload result: ')

except exception as e:

log.error(f'upload error: ')

def recursive_upload(path, exclude_regx_pattern, pool):

path = os.path.abspath(path)

if exclude_regx_pattern is none or exclude_regx_pattern.search(path) is none:

if os.path.isfile(path):

#upload_to_oss(path)

pool.submit(upload_to_oss, file=path)

elif os.path.isdir(path):

base_path = os.path.abspath(path)

lsdir = os.listdir(path)

pathnames = [os.path.join(base_path, filename) for filename in lsdir]

for f in pathnames:

recursive_upload(f, exclude_regx_pattern, pool)

else:

log.info(f'ignore file: ')

if __name__ == '__main__':

exclude_pattern = none

if len(sys.ar**) < 2:

raise exception('argument upload path absent!')

upload_path = os.path.abspath(str(sys.ar**[1]))

if len(sys.ar**) >= 3 and sys.ar**[2] is not none:

exclude_pattern = sys.ar**[2]

exclude_pattern = re.compile(exclude_pattern)

log.info(f'oss upload local: , ignore pattern ');

pool = futures.threadpoolexecutor(max_workers=16)

recursive_upload(upload_path, exclude_pattern, pool)

pool.shutdown()

阿里雲OSS上傳

這種方式相當於自動上傳,可以參考antd官網的給的例子 首先,後端會提供乙個介面,會返回一些上傳到oss需要的類似配置引數 下面的是我們後端同學給返回的引數 1 accessid,對應ossaccesskeyd 2 policy 3 signature 4 host,oss的上傳位址 下面是請求上傳...

阿里雲oss 檔案上傳

寫在前面 阿里雲oss的各種sdk,都是對它的api進行的封裝。所以如果我們直接呼叫api時出錯,卻又不知道 出問題時,可以直接閱讀sdk的原始碼,找到問題所在。首先貼一下阿里雲oss官網貼出的簽名計算方式 authorization oss accesskeyid signature signat...

阿里雲OSS上傳 刪除

設定檔案路徑和名稱 string fileurl filehost datestr uuid file.getoriginalfilename 上傳檔案 putobjectresult result client.putobject new putobjectrequest bucketname,f...