1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
| import os import boto3 from loguru import logger BUCKET_NAME = "your-bucket-name"
CN_S3_AKI = 'your_aws_access_key_id' CN_S3_SAK = 'your_aws_secret_access_key' CN_REGION_NAME = 'your-cn-region-name'
s3 = boto3.client('s3', region_name=CN_REGION_NAME, aws_access_key_id=CN_S3_AKI, aws_secret_access_key=CN_S3_SAK ) def upload_files(path_local, path_s3): """ 上传(重复上传会覆盖同名文件) :param path_local: 本地路径 :param path_s3: s3路径 """ logger.info(f'Start upload files.') if not upload_single_file(path_local, path_s3): logger.error(f'Upload files failed.') logger.info(f'Upload files successful.') def upload_single_file(src_local_path, dest_s3_path): """ 上传单个文件 :param src_local_path: :param dest_s3_path: :return: """ try: with open(src_local_path, 'rb') as f: s3.upload_fileobj(f, BUCKET_NAME, dest_s3_path) except Exception as e: logger.error(f'Upload data failed. | src: {src_local_path} | dest: {dest_s3_path} | Exception: {e}') return False logger.info(f'Uploading file successful. | src: {src_local_path} | dest: {dest_s3_path}') return True def download_zip(path_s3, path_local): """ 下载 :param path_s3: :param path_local: :return: """ retry = 0 while retry < 3: logger.info(f'Start downloading files. | path_s3: {path_s3} | path_local: {path_local}') try: s3.download_file(BUCKET_NAME, path_s3, path_local) file_size = os.path.getsize(path_local) logger.info(f'Downloading completed. | size: {round(file_size / 1048576, 2)} MB') break except Exception as e: logger.error(f'Download zip failed. | Exception: {e}') retry += 1 if retry >= 3: logger.error(f'Download zip failed after max retry.') def delete_s3_zip(path_s3, file_name=''): """ 删除 :param path_s3: :param file_name: :return: """ try: s3.delete_object(Bucket=BUCKET_NAME, Key=path_s3) except Exception as e: logger.error(f'Delete s3 file failed. | Exception: {e}') logger.info(f'Delete s3 file Successful. | path_s3 = {path_s3}') def batch_delete_s3(delete_key_list): """ 批量删除 :param delete_key_list: [ {'Key': "test-01/虎式03的副本.jpeg"}, {'Key': "test-01/tank001.png"}, ] :return: """ try: res = s3.delete_objects( Bucket=BUCKET_NAME, Delete={'Objects': delete_key_list} ) except Exception as e: logger.error(f"Batch delete file failed. | Excepthon: {e}") logger.info(f"Batch delete file success. ") def get_files_list(Prefix=None): """ 查询 :param start_after: :return: """ logger.info(f'Start getting files from s3.') try: if Prefix is not None: all_obj = s3.list_objects_v2(Bucket=BUCKET_NAME, Prefix=Prefix) else: all_obj = s3.list_objects_v2(Bucket=BUCKET_NAME) except Exception as e: logger.error(f'Get files list failed. | Exception: {e}') return contents = all_obj.get('Contents') logger.info(f"--- contents = {contents}") if not contents: return file_name_list = [] for zip_obj in contents: file_size = round(zip_obj['Size'] / 1024 / 1024, 3) zip_name = zip_obj['Key'] file_name_list.append(zip_name) logger.info(f'Get file list successful.') return file_name_list
if __name__ == "__main__": pass file_name_list = get_files_list() logger.info(f"file_name_list = {file_name_list}")
|