SDK参考文档:
https://help.aliyun.com/document_detail/88434.html?spm=a2c4g.11186623.6.881.1f3046de7bliBH
#coding=utf-8
import os
from oss2 import SizedFileAdapter, determine_part_size
from oss2.models import PartInfo
import oss2
import shutil
#定义阿里云各参数变量
access_key_id = ‘yourid’
access_key_secret = ‘yoursecret’
bucket_name = ‘yourbucketname’
#endpoint = ‘oss-cn-hangzhou-internal.aliyuncs.com’
endpoint = ‘oss-cn-hangzhou.aliyuncs.com’
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)
filepath = ‘D:\soft’
destpath = ‘file/’
#定义分片上传函数
def ossfileput (fm,dm,bucket) :
#获取文件大小
total_size = os.path.getsize(fm)
# 设置分片大小
part_size = determine_part_size(total_size, preferred_size=1000 * 1024)
upload_id = bucket.init_multipart_upload(dm).upload_id
parts = []
#读取上传文件
with open(fm,‘rb’) as fileobj:
part_number = 1
offset = 0
while offset < total_size:
num_to_upload = min(part_size, total_size - offset)
result = bucket.upload_part(dm, upload_id, part_number,
SizedFileAdapter(fileobj, num_to_upload))
parts.append(PartInfo(part_number, result.etag))
#print(fm + ‘3’)
offset += num_to_upload
part_number += 1
#组合分片
bucket.complete_multipart_upload(dm, upload_id, parts)
for file_name in os.listdir(filepath):
#组合路径
fm = os.path.join(filepath, file_name)
dm = os.path.join(destpath, file_name)
#判断文件在OSS中是否已存在
exist = bucket.object_exists(dm)
#print(exist)
if exist:
print(file_name + ‘已存在’)
else:
ossfileput(fm,dm,bucket)
#########注意缩进################