python向al的oss上传文件,简单上传,简单下载

刚学习了使用python向al的oss上传文件,简单上传,简单下载,和大家分享下

import os
import  yaml
import oss2
import  sys
import  tempfile
#从yml文件中获取配置
def getyml():
    # 获取当前文件路径
    filePath = os.path.dirname(__file__)
    # 获取当前文件的
    fileNamePath = os.path.split(os.path.realpath(__file__))[0]
    # 获取配置文件的路径
    yamlPath = os.path.join(fileNamePath, 'Aloss.yml')
    fileoss = open(yamlPath, 'r', encoding='utf-8')
    f = fileoss.read()
    con = yaml.load(f, Loader=yaml.FullLoader)
    # Bucket域名
    url = con.get('url')
    AccessKeyID = con.get('AccessKeyID')
    AccessKeySecret = con.get('AccessKeySecret')
    bucket = con.get('bucket')
    list=[url,AccessKeyID,AccessKeySecret,bucket]
    return list
# 当无法确定待上传的数据长度时,total_bytes的值为None。
def percentage(consumed_bytes, total_bytes):
    if total_bytes:
        rate = int(100 * (float(consumed_bytes) / float(total_bytes)))
        print('\r{0}% '.format(rate), end='')
        sys.stdout.flush()
def UploadingImg():
    list=getyml()
    url =list[0]
    AccessKeyID=list[1]
    AccessKeySecret = list[2]
    bucket = list[3]
    #上传文件地址
    uploads = "D:/AAA.png"
    auth = oss2.Auth(AccessKeyID, AccessKeySecret)
    # 阿里云的Bucket名称下的文件夹
    remotePath="test/"
    # 要存放到阿里云上后命名
    img = 'BBB.png'
    bucket1 = oss2.Bucket(auth, url, bucket)
    with open(uploads, 'rb') as fileobj:
        # 阿里上的文档这个地方有错误,从第1000开始会导致文件缺少,并且如果文件不足1000字节时,会报错。
        fileobj.seek(0, os.SEEK_SET)
        # Tell方法用于返回当前位置。
        current = fileobj.tell()
        #阿里云的位置
        res=bucket1.put_object(remotePath+img, fileobj)
        print("上传成功")
        if res.status == 200:
            # fileurl=url+"/"+remotePath+img
            #得到一个阿里云的链接
            fileurl = remotePath + img
            return fileurl
#简单下载            
def DownloadImg(imgurl):
    list=getyml()
    url =list[0]
    AccessKeyID=list[1]
    AccessKeySecret = list[2]
    bucket = list[3]
    auth = oss2.Auth(AccessKeyID, AccessKeySecret)
    bucket1 = oss2.Bucket(auth, url, bucket)
    # 下载OSS文件到本地文件。如果指定的本地文件存在会覆盖,不存在则新建。
    #  由本地文件路径加文件名包括后缀组成,例如/users/local/myfile.txt。
    #  表示下载的OSS文件的完整名称,即包含文件后缀在内的完整路径,例如abc/efg/123.jpg。
    #存放到本地的地址
    uploads = "D:/BBB.png"
    # remotePath = "test/"
    #img = 'BBB.png'
    #从上传处拿到的链接
    bucket1.get_object_to_file(imgurl, uploads)
    # bucket1.get_object_to_file(remotePath+img,uploads)
    print("下载成功")
#有进度条上传    
def UploadingProgressBar():
    list = getyml()
    url = list[0]
    AccessKeyID = list[1]
    AccessKeySecret = list[2]
    bucket = list[3]
    uploads = "D:/AAA.png"
    auth = oss2.Auth(AccessKeyID, AccessKeySecret)
    remotePath = "test/"
    img="CCC.png"
    bucket1 = oss2.Bucket(auth, url, bucket)
    with open(uploads, 'rb') as fileobj:
        # Seek方法用于指定从第0个字节位置开始读写。上传时会从您指定的第0个字节位置开始上传,直到文件结束。
        # 阿里上的文档这个地方有错误,从第1000开始会导致文件缺少,并且如果文件不足1000字节时,会报错。
        #可以不写如下代码
        # fileobj.seek(0, os.SEEK_SET)
        # current = fileobj.tell()

        fileobj.seek(0, os.SEEK_SET)
        # Tell方法用于返回当前位置。
        current = fileobj.tell()
        res=bucket1.put_object(remotePath+img, fileobj, progress_callback=percentage)
        print("上传成功")
        if res.status == 200:
            # fileurl=url+"/"+remotePath+img
            # 得到一个阿里云的链接
            fileurl = remotePath + img
            return fileurl
#有进度条下载            
def  DownloadProgressBar(fileurl):
    list = getyml()
    url = list[0]
    AccessKeyID = list[1]
    AccessKeySecret = list[2]
    bucket = list[3]
    auth = oss2.Auth(AccessKeyID, AccessKeySecret)
    # 存放到本地的地址
    uploads = "D:/CCC.png"
    bucket1 = oss2.Bucket(auth, url, bucket)
    # 从上传处拿到的链接
    # remotePath = "test/"
    # img="CCC.png"
    # progress_callback是可选参数,用于实现进度条功能。
    bucket1.get_object_to_file(fileurl, uploads, progress_callback=percentage)
    print("下载成功")
#调用方式    
class mian():
    #imgurl=UploadingImg()
    # DownloadImg(imgurl)
    # imgurl=UploadingProgressBar()
    # DownloadProgressBar(imgurl)
    pass

你可能感兴趣的:(python,oss上传,python)