python - s3 - 传输前自动压缩
问题描述
如果这个问题是重复或错误的,那么对不起,请删除这个。
我想知道 :
如果任何可用的工具默认压缩并传输文件到 S3
或工具可以选择将文件压缩并传输到 S3
- 或者我必须调用python库并压缩然后转移到S3
解决方案
如果你愿意,你可以使用简单的 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))
推荐阅读
- python - 更改 pandas csv 列的值样式 python
- r - 如何使用向量遍历数据框中的一组特定列名称?
- swift - 表达式在调试器中工作但不在代码中
- python - Windows 10 中的 Opencv 导入问题
- php - 图像上的水印随机出现灰色伪影
- visual-studio - 如何解决此问题:Unity 中的错误 CS0234?
- docker - 如何在 Kubernetes 部署中动态更新版本
- python - 基数为 10 的 int() 的无效文字:'create_property'
- javafx - 如何创建新项目并在场景 JavafX 中显示?
- javascript - HTML DOM 树中是否存在具有 {visibility:hidden} 的元素?