首页 > 解决方案 > s3 - 传输前自动压缩

问题描述

如果这个问题是重复或错误的,那么对不起,请删除这个。

我想知道 :

  1. 如果任何可用的工具默认压缩并传输文件到 S3

  2. 或工具可以选择将文件压缩并传输到 S3

  3. 或者我必须调用python库并压缩然后转移到S3

标签: pythonamazon-s3compression

解决方案


如果你愿意,你可以使用简单的 python 代码。

import gzip
import json
import boto3


# To compress
# Data to compress
data = [{'name': 'test'}, {'name': 'test2'}]
# Converting data to string
json_str = json.dumps(data) + "\n"
# Converting to bytes
json_bytes = json_str.encode('utf-8')
jsonfilename = "s3_compressed_file.json.gz"
# Compressing to gzip
with gzip.GzipFile(jsonfilename, 'w') as f:
    f.write(json_bytes)


# Upload to S3
s3BucketName = 'mybucket'
s3_resource = boto3.resource('s3')
# if you want to rename file while uploading
file_name = 's3_compressed_file-1.json.gz'
# '/current_dir_path/' + filename, '<bucket-name>', 's3_folder/{}'.format(filename)
s3_response = s3_resource.meta.client.upload_file('source_dir/' + 's3_compressed_file.gz', s3BucketName,
                                                      'destination_dir/{}'.format(file_name))

推荐阅读