python之压缩与boto3上传下载

zipfile

https://docs.python.org/zh-cn/3/library/zipfile.html

https://www.cnblogs.com/thomson-fred/p/10328662.html

压缩文件

import zipfile
import os


def zip_files(dir_path, zip_file):
    """压缩为zip文件"""
    z = zipfile.ZipFile(zip_file, 'w', zipfile.ZIP_DEFLATED)
    for src_path, src_names, src_filenames in os.walk(dir_path):
        for filename in src_filenames:
            z.write(os.path.join(src_path, filename),  filename)
    z.close()


if __name__ == '__main__':
    dir_path = './pic' # 源文件
    zip_file = 'pic.zip' # 压缩文件
    zip_files(dir_path, zip_file)

boto3

我们的文件或者图片过多时,存在本地会导致加载十分缓慢,并且占用空间,将其存于s3是一个不错的选择, 使用s3的上传和下载。

https://aws.amazon.com/cn/sdk-for-python/

https://www.cnblogs.com/junneyang/p/5980286.html

https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3.html

具体代码可参考此链接, 有老版本boto的具体实现代码,也有新版本boto3 的实现方法,比我写的丰富,自己代码就不再上传了。

https://blog.csdn.net/nslogheyang/article/details/100115336

你可能感兴趣的:(python之压缩与boto3上传下载)