吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 309|回复: 1
收起左侧

[Python 原创] 网易数帆下线-对象存储下载方法

[复制链接]
52jsc 发表于 2025-4-3 15:04

应该前几年不少友友用网易数帆的对象存储做免费图床薅羊毛
邮件被告知要下线了,4-7号,去对象存储那看了下,没有批量取回
然后写了个py脚本,因为我只存图,所以这篇只测过图片下载
希望有帮助  也致敬网易数帆,感谢


环境: python3.11

前置步骤: 安装依赖

pip3 install nos-python3-sdk
代码

import os
import time

import nos
from numexpr.expressions import double

# 初始化
access_key = "你的"
secret_key = "换成你的"
end_point = "nos-eastchina1.126.net"
client = nos.Client(access_key, secret_key, end_point=end_point)
bucket = "换成你的桶名"

def download_file(file_name):
    try:
        result = client.get_object(bucket, file_name)
        fp = result.get("body")
        object_str = fp.read()

        # 定义相对路径下的目标文件夹和文件名-可以换成你的文件夹名
        target_folder = './blog-cc'
        file_path = os.path.join(target_folder, file_name)

        # 确保目标文件夹存在,如果不存在则创建
        os.makedirs(target_folder, exist_ok=True)

        # 保存到本地文件,以二进制模式写入
        with open(file_path, 'wb') as local_file:
            local_file.write(object_str)

    except nos.exceptions.ServiceException as e:
        print(
            "ServiceException: %s\n"
            "status_code: %s\n"
            "error_type: %s\n"
            "error_code: %s\n"
            "request_id: %s\n"
            "message: %s\n"
        ) % (
            e,
            e.status_code,  # 错误http状态码
            e.error_type,  # NOS服务器定义错误类型
            e.error_code,  # NOS服务器定义错误码
            e.request_id,  # 请求ID,有利于nos开发人员跟踪异常请求的错误原因
            e.message  # 错误描述信息
        )
    except nos.exceptions.ClientException as e:
        print(
            "ClientException: %s\n"
            "message: %s\n"
        ) % (
            e,
            e.message  # 客户端错误信息
        )

if __name__ == '__main__':
    try:
        object_lists = client.list_objects(bucket=bucket, limit=1000)
        f_num = len(object_lists["response"].findall("Contents"))
        print(f'文件个数:{f_num}')
        print('准备全部下载')
        # 当前时间
        start_time = time.time()

        for count, item in enumerate(object_lists["response"].findall("Contents"), start=1):
            f_name = item.find("Key").text
            f_size = double(item.find("Size").text) / 1000
            update_time = item.find("LastModified").text
            print(f'正在下载第{count},文件名:{f_name}')
            now = time.time()
            download_file(f_name)
            print(f'第{count},文件名:{f_name}下载成功,耗时{time.time() - now} s')
            # print(f'第{count},文件名:{f_name}, \n'
            #       f'大小:{f_size}  KB, \n'
            #       f'最后修改时间:{update_time}\n')
        end_time = time.time()
        take_time_s = end_time - start_time
        print(f'下载完成,共{f_num}个文件,共耗时{take_time_s}秒 == {take_time_s / 60}分钟')

    except nos.exceptions.ServiceException as e:
        print(
            "ServiceException: %s\n"
            "status_code: %s\n"
            "error_type: %s\n"
            "error_code: %s\n"
            "request_id: %s\n"
            "message: %s\n"
        ) % (
            e,
            e.status_code,  # 错误http状态码
            e.error_type,  # NOS服务器定义错误类型
            e.error_code,  # NOS服务器定义错误码
            e.request_id,  # 请求ID,有利于nos开发人员跟踪异常请求的错误原因
            e.message  # 错误描述信息
        )
    except nos.exceptions.ClientException as e:
        print(
            "ClientException: %s\n"
            "message: %s\n"
        ) % (
            e,
            e.message  # 客户端错误信息
        )

免费评分

参与人数 1吾爱币 +7 热心值 +1 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

yhu123 发表于 2025-4-3 18:54
感谢分享
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-4-6 22:55

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表