To display the progress of file download in automated downloads, you can use libraries or tools that provide progress tracking functionality. Here's how it works and an example:
requests and tqdm):import requests
from tqdm import tqdm
url = "https://example.com/largefile.zip"
response = requests.get(url, stream=True)
total_size = int(response.headers.get('content-length', 0))
with open("largefile.zip", "wb") as file, tqdm(
desc="Downloading",
total=total_size,
unit="B",
unit_scale=True,
unit_divisor=1024,
) as bar:
for data in response.iter_content(chunk_size=1024):
size = file.write(data)
bar.update(size)
This script downloads a file while showing a progress bar.
If downloading from Tencent Cloud Object Storage (COS), use the COS SDK with progress callbacks. For example, in Python:
from qcloud_cos import CosConfig, CosS3Client
from tqdm import tqdm
config = CosConfig(Region="ap-guangzhou", SecretId="YOUR_SECRET_ID", SecretKey="YOUR_SECRET_KEY")
client = CosS3Client(config)
def download_with_progress(bucket, key, local_path):
response = client.head_object(Bucket=bucket, Key=key)
total_size = int(response['Content-Length'])
with open(local_path, "wb") as file, tqdm(
desc="Downloading from COS",
total=total_size,
unit="B",
unit_scale=True,
unit_divisor=1024,
) as bar:
def callback(data):
file.write(data)
bar.update(len(data))
client.download_file(
Bucket=bucket,
Key=key,
DestFilePath=local_path,
PartSize=10, # MB
MAXThread=5,
Callback=callback,
)
download_with_progress("your-bucket", "your-file.zip", "local-file.zip")
This leverages Tencent Cloud COS's SDK with a progress callback.