阿里云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)
相关推荐
guodingdingh1 小时前
软件开发工作问题总结0718
java·开发语言·数据库
优橙教育3 小时前
5G网优培训 vs Java开发:转行选哪个?
java·开发语言·5g
想会飞的蒲公英4 小时前
计算机怎样读取中文文本:编码、清洗与标准化
人工智能·python·自然语言处理
SeaTunnel5 小时前
从 Python Script 地狱到标准化数据集成框架
大数据·开发语言·python·程序员·代码·seatunnel
深度研习笔记5 小时前
OpenCV工业视觉实战09|项目EXE打包+工控机无环境部署+后台常驻运行,彻底脱离Python环境,完成项目最终交付
python·opencv·webpack
碎光拾影5 小时前
ARM交叉工具链各工具作用及IMX6ULL平台LED+蜂鸣器裸机程序实现
java·开发语言·数据库
CCPC不拿奖不改名5 小时前
大模型推理架构与开源生态知识整理
数据库·windows·python·架构·langchain·开源·github
Marst Code6 小时前
(python)2026Plotly 库评估:交互式可视化到底值不值得引入?
开发语言·python
databook6 小时前
当散点图不够用时:用 t-SNE 可视化多维数据
python·数据分析·数据可视化
Miao121316 小时前
微服务 API 测试实践:海外某民宿平台如何构建模式驱动测试基础设施
java·开发语言