def update_object_version( s3: S3, allversion: bool = False, acl: bool = False, tagging: bool = False, ) -> None: """Update versions of object's attributes. Note: this operation only allow update of acl and tagging, because this won't introduce new objects. :param s3: S3 instance :type s3: S3 :param allversion: update all versions? :type allversion: bool, optional :param acl: update acl :type acl: bool, optional :param tagging: update tagging :type tagging: bool, optional """ obj_versions = s3.get_object_version(select_all=allversion) s3_args = S3Args(s3) s3_args.set_extra_args(acl, tagging, version=obj_versions) # check if only tags or acl is being updated # this way it won't create extra versions on the object check_result = s3_args.check_tag_acl() for obj_version in obj_versions: print( "(dryrun) update: s3://%s/%s with version %s" % (s3.bucket_name, obj_version.get("Key"), obj_version.get("VersionId")) ) if get_confirmation("Confirm?"): for obj_version in obj_versions: print( "update: s3://%s/%s with version %s" % ( s3.bucket_name, obj_version.get("Key"), obj_version.get("VersionId"), ) ) if check_result: if check_result.get("Tags"): s3.client.put_object_tagging( Bucket=s3.bucket_name, Key=obj_version.get("Key"), VersionId=obj_version.get("VersionId"), Tagging={"TagSet": check_result.get("Tags")}, ) if check_result.get("Grants"): grant_args = { "Bucket": s3.bucket_name, "Key": obj_version.get("Key"), "VersionId": obj_version.get("VersionId"), } grant_args.update(check_result.get("Grants", {})) s3.client.put_object_acl(**grant_args) else: print("Nothing to update")
def update_object_name(s3: S3, version: bool = False) -> None: """Update object name. :param s3: S3 class instance :type s3: S3 :param version: whether to rename version's name, this will create a new object :type version: bool, optional """ print( "Enter the new name below (format: newname or path/newname for a new path)" ) new_name = input("Name(Orignal: %s): " % s3.path_list[0]) if not version: print("(dryrun) rename: s3://%s/%s to s3://%s/%s" % (s3.bucket_name, s3.path_list[0], s3.bucket_name, new_name)) if get_confirmation("Confirm?"): print("rename: s3://%s/%s to s3://%s/%s" % (s3.bucket_name, s3.path_list[0], s3.bucket_name, new_name)) # initialise empty s3_args so that get_copy_args will use all the original value s3_args = S3Args(s3) copy_object_args = get_copy_args(s3, s3.path_list[0], s3_args, extra_args=True) copy_source = { "Bucket": s3.bucket_name, "Key": s3.path_list[0], } s3transferwrapper = S3TransferWrapper() s3.client.copy( copy_source, s3.bucket_name, new_name, Callback=S3Progress(s3.path_list[0], s3.bucket_name, s3.client), ExtraArgs=copy_object_args, Config=s3transferwrapper.transfer_config, ) s3.client.delete_object( Bucket=s3.bucket_name, Key=s3.path_list[0], ) else: # get version obj_version = s3.get_object_version(key=s3.path_list[0])[0] print("(dryrun) rename: s3://%s/%s to s3://%s/%s with version %s" % ( s3.bucket_name, obj_version.get("Key"), s3.bucket_name, new_name, obj_version.get("VersionId"), )) if get_confirmation("Confirm?"): print("rename: s3://%s/%s to s3://%s/%s with version %s" % ( s3.bucket_name, obj_version.get("Key"), s3.bucket_name, new_name, obj_version.get("VersionId"), )) # initialise empty s3_args so that get_copy_args will use all the original value s3_args = S3Args(s3) copy_object_args = get_copy_args( s3, s3.path_list[0], s3_args, extra_args=True, version=obj_version.get("VersionId"), ) copy_source = { "Bucket": s3.bucket_name, "Key": obj_version.get("Key"), "VersionId": obj_version.get("VersionId"), } s3transferwrapper = S3TransferWrapper() s3.client.copy( copy_source, s3.bucket_name, new_name, Callback=S3Progress( obj_version.get("Key", ""), s3.bucket_name, s3.client, version_id=obj_version.get("VersionId"), ), ExtraArgs=copy_object_args, Config=s3transferwrapper.transfer_config, )