|
@@ -149,3 +149,67 @@ class AmazonS3Util:
|
|
|
'Key': file_key
|
|
|
}
|
|
|
self.session_conn.meta.client.copy(source_dict, to_bucket, file_key)
|
|
|
+
|
|
|
+ def copy_single_obj(self, source_bucket, source_object, target_bucket, target_object, StorageClass=None):
|
|
|
+ """
|
|
|
+ 单个对象复制
|
|
|
+ @param source_bucket:源存储桶
|
|
|
+ @param source_object:源对象
|
|
|
+ @param target_bucket:目标存储桶
|
|
|
+ @param target_object:目标对象
|
|
|
+ @param StorageClass:存储类
|
|
|
+ @return: None
|
|
|
+ """
|
|
|
+ s3 = self.session_conn
|
|
|
+ copy_source = {
|
|
|
+ 'Bucket': source_bucket,
|
|
|
+ 'Key': source_object
|
|
|
+ }
|
|
|
+ target_object = s3.Object(target_bucket, target_object)
|
|
|
+ if StorageClass:
|
|
|
+ target_object.copy_from(CopySource=copy_source, StorageClass=StorageClass)
|
|
|
+ else:
|
|
|
+ target_object.copy_from(CopySource=copy_source)
|
|
|
+
|
|
|
+ def generate_put_obj_url(self, bucket_name, obj_key, storage_class=None):
|
|
|
+ """
|
|
|
+ 生成预签名对象URL
|
|
|
+ @param bucket_name: 存储桶名称
|
|
|
+ @param obj_key: 对象key
|
|
|
+ @param storage_class: 存储类 例
|
|
|
+ @return: 对象URL
|
|
|
+ """
|
|
|
+ params = {
|
|
|
+ 'Bucket': bucket_name,
|
|
|
+ 'Key': obj_key,
|
|
|
+ }
|
|
|
+ if storage_class:
|
|
|
+ params['StorageClass'] = storage_class
|
|
|
+ return self.session_conn.meta.client.generate_presigned_url('put_object',
|
|
|
+ Params=params,
|
|
|
+ ExpiresIn=7200)
|
|
|
+
|
|
|
+ def batch_copy_obj(self, source_bucket, target_bucket, prefix, target_prefix, storage_class=None):
|
|
|
+ """
|
|
|
+ 批量拷贝对象
|
|
|
+ @param source_bucket: 源存储桶
|
|
|
+ @param target_bucket: 目标存储桶
|
|
|
+ @param prefix: 需要搜索的对象前缀 例:AUS000247LTCLY/vod1/1686043996
|
|
|
+ @param target_prefix: 目标对象前缀 例:app/algorithm-shop/1686043996
|
|
|
+ @param storage_class: 存储类
|
|
|
+ @return: None
|
|
|
+ """
|
|
|
+ s3 = self.session_conn
|
|
|
+ # 遍历源存储桶中指定前缀下的所有对象,依次进行复制操作
|
|
|
+ for obj in s3.Bucket(source_bucket).objects.filter(Prefix=prefix):
|
|
|
+ key = obj.key # 对象键名
|
|
|
+ target_key = f'{target_prefix}/' + key.split('/')[-1] # 新的对象键名,此处为 "new_path/" + 原有文件名
|
|
|
+ copy_source = {
|
|
|
+ 'Bucket': source_bucket,
|
|
|
+ 'Key': key
|
|
|
+ }
|
|
|
+ # 将对象复制到目标存储桶,并设置存储类型和新的对象键名
|
|
|
+ if storage_class:
|
|
|
+ s3.Object(target_bucket, target_key).copy_from(CopySource=copy_source, StorageClass=storage_class)
|
|
|
+ else:
|
|
|
+ s3.Object(target_bucket, target_key).copy_from(CopySource=copy_source)
|