主题
七牛云SDK
上传指定文件夹
python
# -*- coding: utf-8 -*-
# flake8: noqa
from qiniu import Auth, put_file, etag,BucketManager
import qiniu.config
import sys
import os
#需要填写你的 Access Key 和 Secret Key
access_key = 'LLhUKDAabQAW8Cyuu67cDHaB9Zky-4cDbzqKrIDN'
secret_key = 'BxPPEKP539ba-jJM2WFoheaGxlIzTFjpE4eGK_wZ'
#构建鉴权对象
q = Auth(access_key, secret_key)
bucket = BucketManager(q)
#要上传的空间
bucket_name = 'wxmgcs-blog'
def upload(localfile,remotefile):
#上传后保存的文件名
key = remotefile #'my-python-logo.png'
#生成上传 Token,可以指定过期时间等
token = q.upload_token(bucket_name, remotefile, 3600)
#要上传文件的本地路径
#localfile = './sync/bbb.jpg'
ret, info = put_file(token, remotefile, localfile, version='v2')
print(info)
#assert ret['key'] == key
#assert ret['hash'] == etag(localfile)
def upload_all():
local_dir = sys.argv[1]
for item in os.listdir(local_dir):
if item.startswith("."):
continue
localfile = os.path.sep.join([local_dir,item])
if get_file_stat(item):
continue
upload(localfile,item)
# print(localfile)
def get_file_stat(remote_file):
ret, info = bucket.stat(bucket_name, remote_file)
if info.text_body.find("no such file or directory") >= 0:
return False
return True
# print(get_file_stat("202112292253591.png"))
upload_all()