bestinslot-xyz / OPI

Open Protocol Indexer, OPI, is the best-in-slot open-source indexing client for meta-protocols on Bitcoin.
Apache License 2.0
202 stars 110 forks source link

s3下载 改为断点续传,由于服务器下载老中断,修改s3下载方式 #25

Closed yutou123 closed 7 months ago

yutou123 commented 7 months ago

def s3_download(s3_bucket, s3_object_key, local_file_name): s3_object_key = S3_KEY_PREFIX + s3_object_key meta_data = s3client.head_object(Bucket=s3_bucket, Key=s3_object_key) total_length = int(meta_data.get('ContentLength', 0))

Check if the local file exists

if os.path.exists(local_file_name):

If file exists, get its size

    local_file_size = os.path.getsize(local_file_name)

else:

If file doesn't exist, set local_file_size to 0

    local_file_size = 0

# Calculate the starting position for resuming the download

range_start = local_file_size

# Set the Range header for resuming the download

range_header = f'bytes={range_start}-'

# Adjust total_length to only download remaining content

total_length -= range_start

with tqdm(total=total_length, desc=f'source: s3://{s3_bucket}/{s3_object_key}', bar_format="{percentage:.1f}%|{bar:25} | {rate_fmt} | {desc}", unit='B', unit_scale=True, unit_divisor=1024) as pbar: with open(local_file_name, 'wb') as f: response = s3client.get_object(Bucket=s3_bucket, Key=s3_object_key, Range=range_header) for chunk in response['Body'].iter_chunks(): f.write(chunk) pbar.update(len(chunk))