阿里云OSS 上传文件 Python版本

单文件:

python 复制代码
import alibabacloud_oss_v2 as oss

# 配置

accessKeyId = "***************"
accessKeySecret = "#########"
customRegion = "cn-%%%%%%"
customeBucket = "&&&&&&&&&"

# 配置静态访问凭证(生产环境不推荐硬编码)
static_cred = oss.credentials.StaticCredentialsProvider(
 access_key_id=accessKeyId,
 access_key_secret=accessKeySecret,
)


def init_client():
 cfg = oss.config.load_default()
 cfg.credentials_provider = static_cred
 cfg.region = customRegion

 return oss.Client(cfg)


def multi_upload(files_map):
 """
    :param files_map: 字典类型 { "目标object路径": "本地文件绝对路径" }
    """
 client = init_client()
 uploader = client.uploader()

 for obj_key, local_path in files_map.items():
 try:
 request = oss.PutObjectRequest(bucket=customeBucket, key=obj_key)
 result = uploader.upload_file(
 request,
 filepath=local_path,
 part_size=10 * 1024 * 1024,  # 分片大小10MB
 part_num=3,  # 并发分片数
            )
 print(f"文件 {local_path} 上传成功,ETag: {result.etag}")
 except oss as e:
 print(f"上传失败:{e.message}")


if __name__ == "__main__":

 # 定义多文件映射(目标路径:本地路径)
 file_mapping = {
 "sunwukongtest/index.html": "C:/Users/57307/Desktop/Test/TestHtml/index.html",
 "sunwukongtest/index1.html": "C:/Users/57307/Desktop/Test/TestHtml/index1.html",
 "sunwukongtest/index2.html": "C:/Users/57307/Desktop/Test/TestHtml/index2.html",
 "sunwukongtest/index3.html": "C:/Users/57307/Desktop/Test/TestHtml/index3.html",
 "sunwukongtest/index4.html": "C:/Users/57307/Desktop/Test/TestHtml/index4.html",
    }

 multi_upload(file_mapping)

分片上传

python 复制代码
import os
import alibabacloud_oss_v2 as oss

# 配置

accessKeyId = "LTAI5tAVun2iydm8tWDd1JYg"
accessKeySecret = "pjb1rDErI3TJTC91ko6sJvabnApcWf"
customRegion = "cn-beijing"
customeBucket = "uattxgiswww"

# 配置静态访问凭证(生产环境不推荐硬编码)
static_cred = oss.credentials.StaticCredentialsProvider(
 access_key_id=accessKeyId,
 access_key_secret=accessKeySecret,
)


def init_client():
 cfg = oss.config.load_default()
 cfg.credentials_provider = static_cred
 cfg.region = customRegion

 return oss.Client(cfg)


def multi_upload(files_map):
 """
    :param files_map: 字典类型 { "目标object路径": "本地文件绝对路径" }
    """
 client = init_client()

 for obj_key, local_path in files_map.items():
 # 初始化分片上传请求,获取upload_id用于后续分片上传
 result = client.initiate_multipart_upload(
 oss.InitiateMultipartUploadRequest(
 bucket=customeBucket,
 key=obj_key,
            )
        )

 # 定义每个分片的大小为5MB
 part_size = 5 * 1024 * 1024

 # 获取要上传文件的总大小
 data_size = os.path.getsize(local_path)

 # 初始化分片编号,从1开始
 part_number = 1

 # 存储每个分片上传的结果
 upload_parts = []

 # 打开文件以二进制模式读取
 with open(local_path, "rb") as f:
 # 遍历文件,按照part_size分片上传
 for start in range(0, data_size, part_size):
 n = part_size
 if start + n > data_size:  # 处理最后一个分片可能小于part_size的情况
 n = data_size - start

 # 创建SectionReader来读取文件的特定部分
 reader = oss.io_utils.SectionReader(oss.io_utils.ReadAtReader(f), start, n)

 # 上传分片
 up_result = client.upload_part(
 oss.UploadPartRequest(
 bucket=customeBucket,
 key=obj_key,
 upload_id=result.upload_id,
 part_number=part_number,
 body=reader,
                )
            )

 # 打印每个分片上传的结果信息
 print(
 f"status code: {up_result.status_code},"
 f" request id: {up_result.request_id},"
 f" part number: {part_number},"
 f" content md5: {up_result.content_md5},"
 f" etag: {up_result.etag},"
 f" hash crc64: {up_result.hash_crc64},"
            )

 # 将分片上传结果保存到列表中
 upload_parts.append(
 oss.UploadPart(part_number=part_number, etag=up_result.etag)
            )

 # 增加分片编号
 part_number += 1

 # 对上传的分片按照分片编号排序
 parts = sorted(upload_parts, key=lambda p: p.part_number)

 # 发送完成分片上传请求,合并所有分片为一个完整的对象
 result = client.complete_multipart_upload(
 oss.CompleteMultipartUploadRequest(
 bucket=customeBucket,
 key=obj_key,
 upload_id=result.upload_id,
 complete_multipart_upload=oss.CompleteMultipartUpload(parts=parts),
        )
    )

 # 下面的代码是另一种方式,通过服务器端列出并合并所有分片数据为一个完整的对象
 # 这种方法适用于当您不确定所有分片是否都已成功上传时
 # Merge fragmented data into a complete Object through the server-side List method
 # result = client.complete_multipart_upload(oss.CompleteMultipartUploadRequest(
 #     bucket=args.bucket,
 #     key=args.key,
 #     upload_id=result.upload_id,
 #     complete_all='yes'
 # ))

 # 输出完成分片上传的结果信息
 print(
 f"status code: {result.status_code},"
 f" request id: {result.request_id},"
 f" bucket: {result.bucket},"
 f" key: {result.key},"
 f" location: {result.location},"
 f" etag: {result.etag},"
 f" encoding type: {result.encoding_type},"
 f" hash crc64: {result.hash_crc64},"
 f" version id: {result.version_id},"
    )


if __name__ == "__main__":

 # 定义多文件映射(目标路径:本地路径)
 file_mapping = {
 "sunwukongtest/indx.html": "C:/Users/57307/Desktop/Test/TestHtml/index.html",
    }

 multi_upload(file_mapping)
相关推荐
king_harry5 分钟前
Java程序-OceanBase Connector/J 示例
开发语言
yzx9910138 分钟前
关于网络协议
网络·人工智能·python·网络协议
fangeqin9 分钟前
ubuntu源码安装python3.13遇到Could not build the ssl module!解决方法
linux·python·ubuntu·openssl
傻啦嘿哟1 小时前
Python 办公实战:用 python-docx 自动生成 Word 文档
开发语言·c#
翻滚吧键盘1 小时前
js代码09
开发语言·javascript·ecmascript
Jay Kay1 小时前
TensorFlow源码深度阅读指南
人工智能·python·tensorflow
q567315231 小时前
R语言初学者爬虫简单模板
开发语言·爬虫·r语言·iphone
会的全对٩(ˊᗜˋ*)و1 小时前
【数据挖掘】数据挖掘综合案例—银行精准营销
人工智能·经验分享·python·数据挖掘
___波子 Pro Max.2 小时前
GitHub Actions配置python flake8和black
python·black·flake8
rzl022 小时前
java web5(黑马)
java·开发语言·前端