Skip to main content

Python Library for Compressing Files in Google Cloud Storage

This library provides a function compress_csv_files_gcs_bucket that simplifies the process of compressing multiple CSV files from a Google Cloud Storage (GCS) bucket into a single ZIP archive. The archive is then uploaded back to the same bucket.

1. compress_csv_files_gcs_bucket(bucket_name, prefix, output_zip_name, output_zip_prefix):

The compress_csv_files_gcs_bucket function takes the following arguments:

Parameters:

  1. bucket_name: (str) The name of the GCS bucket containing the CSV files.
  2. prefix: (str) The prefix to filter files within the bucket (e.g., "path/to/your/csv/files/"). Include a trailing slash.
  3. output_zip_name: (str) The desired name for the output ZIP archive.
  4. output_zip_prefix (str, optional): An optional prefix to add to the filename within the output bucket. Defaults to saving the ZIP file in the same bucket as the source files.

Example Usage:

from Compress_csv_files_gcs_bucket import compress_csv_files_gcs_bucket

# Replace with your information
bucket_name = "your-bucket-name"
prefix = "path/to/your/csv/files/"  # Include trailing slash
output_zip_name = "compressed_data.zip"
output_zip_prefix = "archived/data/"  # Optional, defaults to none
                                       # (saves in the same bucket)

compress_csv_files_gcs_bucket(bucket_name, prefix, output_zip_name, output_zip_prefix)

Function Behavior

  1. Connects to GCS using storage.Client.
  2. Retrieves the bucket specified by bucket_name.
  3. Lists all files with the provided prefix and extension .csv.
  4. Creates an in-memory buffer (BytesIO) to hold the compressed data.
  5. Creates a ZipFile object for writing to the buffer using DEFLATED compression.
  6. Iterates through each file in the bucket: a). Filters for files with extension .csv. b). Downloads the file content as a string. c). Constructs the filename for the ZIP archive: 1. If prefix is provided and the filename starts with the prefix, it removes the prefix from the filename before adding it to the archive. 2. Otherwise, the original filename is used. d). Adds the file content to the ZIP archive with the constructed filename.
  7. Rewinds the in-memory buffer to the beginning for upload.
  8. Constructs the final output filename within the GCS bucket, including the output_zip_prefix if provided.
  9. Creates a new blob object in the GCS bucket with the final filename.
  10. Uploads the in-memory buffer content (containing the ZIP archive) to the GCS bucket as a ZIP file (content_type='application/zip').

Additional Notes:

  1. This library uses the google-cloud-storage and zipfile libraries. Ensure they are installed (pip install google-cloud-storage).
  2. The library only compresses files with the extension .csv.
  3. The library uses an in-memory buffer for efficiency, avoiding creating temporary files on disk.

Metadata

Release files for Compress-csv-files-gcs-bucket 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for Compress-csv-files-gcs-bucket 0.1
File Size Uploaded
Compress_csv_files_gcs_bucket-0.1.tar.gz 2.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for Compress-csv-files-gcs-bucket 0.1
File Interpreter ABI Platform
Compress_csv_files_gcs_bucket-0.1-py3-none-any.whl Python 3 none any Details

Total release size: 6.6 kB

Release files / Compress_csv_files_gcs_bucket-0.1.tar.gz

Download URL Compress_csv_files_gcs_bucket-0.1.tar.gz
Size 2.8 kB
Tags Source
SHA-256 checksum
How to use checksums
18a8a087eafa14b2336fe316cdddb4e06d2a675471258de86b4ec74b27d9b6be
BLAKE2b-256 checksum
How to use checksums
1588395da02ebec3719626ac2410677aefdc2eac405fec42117b37ed0b76d39b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Release files / Compress_csv_files_gcs_bucket-0.1-py3-none-any.whl

Download URL Compress_csv_files_gcs_bucket-0.1-py3-none-any.whl
Size 3.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c23091e2fba4cfc3b56c11197485980ddb247b306f41ad5eb59cf623119e9c03
BLAKE2b-256 checksum
How to use checksums
6cf2ff8dea0326442b46197cf57d55966af31344d663669c0dcfe97f824e6050
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Release history Release notifications | RSS feed

This release

0.1 This release

2 release files

0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page