项目作者: xtream1101

项目描述 :
Stream s3 data into a tar file in s3
高级语言: Python
项目地址: git://github.com/xtream1101/s3-tar.git
创建时间: 2020-02-08T21:23:03Z
项目社区:https://github.com/xtream1101/s3-tar

开源协议:MIT License

下载


s3-tar

PyPI
PyPI

Create a tar/tar.gz/tar.bz2 file from many s3 files and stream back into s3.

Install

pip install s3-tar

Usage

Set up s3 credentials on your system by either of these options:

Set the environment variable S3_ENDPOINT_URL to use a custom s3 host (minio/etc…) , not needed if using AWS s3.

This will use very little RAM. As it downloads files, it streams up the tar’d pieces as it goes.
You can use more or less ram by playing with the options cache_size & part_size_multiplier.

Import

  1. from s3_tar import S3Tar
  2. # Init the job
  3. job = S3Tar(
  4. 'YOUR_BUCKET_NAME',
  5. 'FILE_TO_SAVE_TO.tar', # Use `tar.gz` or `tar.bz2` to enable compression
  6. # target_bucket=None, # Default: source bucket. Can be used to save the archive into a different bucket
  7. # min_file_size='50MB', # Default: None. The min size to make each tar file [B,KB,MB,GB,TB]. If set, a number will be added to each file name
  8. # save_metadata=False, # If True, and the file has metadata, save a file with the same name using the suffix of `.metadata.json`
  9. # remove_keys=False, # If True, will delete s3 files after the tar is created
  10. # ADVANCED USAGE
  11. # allow_dups=False, # When False, will raise ValueError if a file will overwrite another in the tar file, set to True to ignore
  12. # cache_size=5, # Default 5. Number of files to hold in memory to be processed
  13. # s3_max_retries=4, # Default is 4. This value is passed into boto3.client's s3 botocore config as the `max_attempts`
  14. # part_size_multiplier=10, # is multiplied by 5 MB to find how large each part that gets upload should be
  15. # session=boto3.session.Session(), # For custom aws session
  16. )
  17. # Add files, can call multiple times to add files from other directories
  18. job.add_files(
  19. 'FOLDER_IN_S3/',
  20. # folder='', # If a folder is set, then all files from this directory will be added into that folder in the tar file
  21. # preserve_paths=False, # If True, it will use the dir paths relative to the input path inside the tar file
  22. )
  23. # Add a single file at a time
  24. job.add_file(
  25. 'some/file_key.json',
  26. # folder='', # If a folder is set, then the file will be added into that folder in the tar file
  27. )
  28. # Start the tar'ing job after files have been added
  29. job.tar()

Command Line

To see all command line options run:

  1. s3-tar -h
  2. usage: s3-tar [-h] --source-bucket SOURCE_BUCKET --folder FOLDER --filename FILENAME [--target-bucket TARGET_BUCKET] [--min-filesize MIN_FILESIZE] [--save-metadata] [--remove]
  3. [--preserve-paths] [--allow-dups] [--cache-size CACHE_SIZE] [--s3-max-retries S3_MAX_RETRIES] [--part-size-multiplier PART_SIZE_MULTIPLIER]
  4. Tar (and compress) files in s3
  5. optional arguments:
  6. -h, --help show this help message and exit
  7. --source-bucket SOURCE_BUCKET
  8. base bucket to use
  9. --folder FOLDER folder whose contents should be combined
  10. --filename FILENAME Output filename for the tar file. Extension: tar, tar.gz, or tar.bz2
  11. --target-bucket TARGET_BUCKET
  12. Bucket that the tar will be saved to. Only needed if different then source bucket
  13. --min-filesize MIN_FILESIZE
  14. Use to create multiple files if needed. Min filesize of the tar'd files in [B,KB,MB,GB,TB]. e.x. 5.2GB
  15. --save-metadata If a file has metadata, save it to a .metadata.json file
  16. --remove Delete files that were added to the tar file
  17. --preserve-paths Preserve the path layout relative to the input folder
  18. --allow-dups ADVANCED: Allow duplicate filenames to be saved into the tar file
  19. --cache-size CACHE_SIZE
  20. ADVANCED: Number of files to download into memory at a time
  21. --s3-max-retries S3_MAX_RETRIES
  22. ADVANCED: Max retries for each request the s3 client makes
  23. --part-size-multiplier PART_SIZE_MULTIPLIER
  24. ADVANCED: Multiplied by 5MB to set the max size of each upload chunk

CLI Examples

This example will take all the files in the bucket my-data in the folder 2020/07/01 and save it into a compressed tar gzip file in the same bucket into the directory Archives

  1. s3-tar --source-bucket my-data --folder 2020/07/01 --filename Archive/2020-07-01.tar.gz

Now lets say you have a large amount of data and it would create a tar file to large to work with. This example will create files that are ~2.5GB each and save into a different bucket. Inside each tar file it will also save the folder structure as it is in s3.

  1. s3-tar --preserve-paths --source-bucket my-big-data --folder 2009 --target-bucket my-archived-data --filename big_data/2009-archive.tar.gz --min-filesize 2.5GB

In the bucket my-archived-data, in the folder big_data/ there will be multiple files named:

  • 2009-archive-1.tar.gz
  • 2009-archive-2.tar.gz
  • 2009-archive-3.tar.gz

Notes

  • For better performance, if you know the files you are adding will not have any duplicate names (or you are ok with duplicates), you can set --allow-dups in the cli or pass allow_dups=True to the S3Tar class to get better performance since it wil not have to check each files name.