您可以使用AWS Glue的Python API来编写代码将文件写入输出存储桶并进行压缩。以下是一个示例代码:
import boto3
import gzip
# 创建S3客户端
s3 = boto3.client('s3')
# 将文件写入输出存储桶并进行压缩
def write_to_output_bucket(bucket_name, file_name, content):
# 将文件内容写入本地临时文件
temp_file = '/tmp/output_file'
with open(temp_file, 'wb') as f:
f.write(content)
# 压缩文件
compressed_file = '/tmp/compressed_file.gz'
with open(temp_file, 'rb') as f_in:
with gzip.open(compressed_file, 'wb') as f_out:
f_out.writelines(f_in)
# 将压缩文件上传到S3存储桶
s3.upload_file(compressed_file, bucket_name, file_name)
# 调用函数来写入文件到输出存储桶并进行压缩
write_to_output_bucket('your-output-bucket', 'output_file.csv', b'file content')
请注意,您需要将your-output-bucket替换为您自己的输出存储桶名称,并将output_file.csv替换为您想要的输出文件名。此代码示例假设您已经配置了AWS凭证,并且具有对指定存储桶的写入权限。