是否可以直接从存储在 S3 上的 zip 文件中读取特定文件?
Is it possible to read a specific file directly from a zip file that is stored on S3?
我在名为 big.zip
的 zip 文件中有一个名为 story.txt
的文件,该文件存储在名为 zips-bucket
.
的 S3 存储桶中
我希望我的 Python 代码只读取 story.txt 的内容,而无需下载甚至扫描整个大 zip 文件。可能吗?怎么样?
不,在您的特定情况下这是不可能的。但是,S3 提供了一个名为 S3 Select 的功能,如果满足某些要求,该功能可以选择性地读取文件的一部分。您可以查看文档。
是的,这是可能的。您将需要导入 smart-open
和 zipfile
模块。假设您的压缩文件在 s3://zips-bucket/big.zip
中。执行以下操作:
import smart_open as so
import zipfile
with so.open('s3://zips-bucket/big.zip', 'rb') as file_data
with zipfile.ZipFile(file_data) as z:
with z.open('story.txt') as zip_file_data:
story_lines = zip_file_data.readlines()
应该就可以了!
我在名为 big.zip
的 zip 文件中有一个名为 story.txt
的文件,该文件存储在名为 zips-bucket
.
我希望我的 Python 代码只读取 story.txt 的内容,而无需下载甚至扫描整个大 zip 文件。可能吗?怎么样?
不,在您的特定情况下这是不可能的。但是,S3 提供了一个名为 S3 Select 的功能,如果满足某些要求,该功能可以选择性地读取文件的一部分。您可以查看文档。
是的,这是可能的。您将需要导入 smart-open
和 zipfile
模块。假设您的压缩文件在 s3://zips-bucket/big.zip
中。执行以下操作:
import smart_open as so
import zipfile
with so.open('s3://zips-bucket/big.zip', 'rb') as file_data
with zipfile.ZipFile(file_data) as z:
with z.open('story.txt') as zip_file_data:
story_lines = zip_file_data.readlines()
应该就可以了!