Update stream_files_to_cos.py
Browse files- stream_files_to_cos.py +9 -9
stream_files_to_cos.py
CHANGED
@@ -165,24 +165,24 @@ def stream_file_to_cos():
|
|
165 |
session = requests.Session()
|
166 |
response = session.request(http_method, source_url, stream=True)
|
167 |
response.raise_for_status()
|
168 |
-
|
169 |
# Extract actual filename from response
|
170 |
filename = extract_filename_from_headers(response)
|
171 |
-
|
172 |
# Combine prefix with filename for the full COS key
|
173 |
target_key = f"{prefix}{filename}" if prefix else filename
|
174 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
# Upload file to COS
|
176 |
conf = ibm_boto3.s3.transfer.TransferConfig(
|
177 |
-
multipart_threshold=1024**2, # 1MB
|
178 |
-
max_concurrency=100
|
179 |
)
|
180 |
-
|
181 |
cos_client.upload_fileobj(
|
182 |
-
|
183 |
-
cos_config['bucket_name'],
|
184 |
-
target_key,
|
185 |
-
Config=conf
|
186 |
)
|
187 |
|
188 |
results.append({
|
|
|
165 |
session = requests.Session()
|
166 |
response = session.request(http_method, source_url, stream=True)
|
167 |
response.raise_for_status()
|
|
|
168 |
# Extract actual filename from response
|
169 |
filename = extract_filename_from_headers(response)
|
|
|
170 |
# Combine prefix with filename for the full COS key
|
171 |
target_key = f"{prefix}{filename}" if prefix else filename
|
172 |
|
173 |
+
# Create a BytesIO buffer and write decompressed content
|
174 |
+
file_buffer = io.BytesIO()
|
175 |
+
for chunk in response.iter_content(chunk_size=8192):
|
176 |
+
if chunk:
|
177 |
+
file_buffer.write(chunk)
|
178 |
+
file_buffer.seek(0) # Reset to beginning for upload
|
179 |
+
|
180 |
# Upload file to COS
|
181 |
conf = ibm_boto3.s3.transfer.TransferConfig(
|
182 |
+
multipart_threshold=1024**2, max_concurrency=100 # 1MB
|
|
|
183 |
)
|
|
|
184 |
cos_client.upload_fileobj(
|
185 |
+
file_buffer, cos_config["bucket_name"], target_key, Config=conf
|
|
|
|
|
|
|
186 |
)
|
187 |
|
188 |
results.append({
|