阿里云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)
相关推荐
神仙别闹7 小时前
基于 C++ 实现(控制台)学生成绩管理系统
开发语言·c++
Y幽谷客7 小时前
Python加载本地大模型(Qwen3.5 8B)
python·大模型
伞伞悦读8 小时前
【第36期】Python 目录与路径详解:pathlib、文件遍历、创建、复制、移动和删除风险
开发语言·python
线上放牧人8 小时前
Windows删除图标缓存
windows·python·pyqt
qq_5470261798 小时前
Python 变量和简单数据类型
python
智搜广告9 小时前
智搜广告:科技行业AI回答优化公司如何破局
大数据·python·elasticsearch·geo
IpdataCloud10 小时前
AI智能体调用工具怎么核验来源IP?归属地、网络类型与代理风险识别(含Python代码)
数据库·python·tcp/ip
2601_9669496510 小时前
只拿到股票代码还不够:量化程序到底还需要哪些标的信息?——从数据建模开始理解股票元数据
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
蒸鱼Yuzheng10 小时前
Python 游戏测试开发怎么准备:日志解析、接口校验、并发与可维护性
自动化测试·python·测试开发·面试题·游戏测试
m0_7345717611 小时前
深入理解5g <十八>传输块tbsize的计算
开发语言·5g