是否可以在不使用 boto3 下载的情况下获取 S3 文件的内容?
Is it possible to get the contents of an S3 file without downloading it using boto3?
我正在处理从 Redshift
数据库中转储文件的过程,并且希望不必在本地下载文件来处理数据。我看到 Java
有一个 StreamingObject
class 可以满足我的要求,但我在 boto3
.
中没有看到类似的东西
如果您有一个 mybucket
S3 存储桶,其中包含一个 beer
密钥,下面是如何下载和获取值而不将其存储在本地文件中:
import boto3
s3 = boto3.resource('s3')
print s3.Object('mybucket', 'beer').get()['Body'].read()
这可能与您想要做的事情相关,也可能不相关,但对于我的情况来说,使用临时文件是一件很有效的事情:
import tempfile
import boto3
bucket_name = '[BUCKET_NAME]'
key_name = '[OBJECT_KEY_NAME]'
s3 = boto3.resource('s3')
temp = tempfile.NamedTemporaryFile()
s3.Bucket(bucket_name).download_file(key_name, temp.name)
# do what you will with your file...
temp.close()
我使用那个解决方案,实际上:
import boto3
s3_client = boto3.client('s3')
def get_content_from_s3(bucket: str, key: str) -> str:
"""Save s3 content locally
param: bucket, s3 bucket
param: key, path to the file, f.i. folder/subfolder/file.txt
"""
s3_file = s3_client.get_ojct(Bucket=bucket, Key=key)['Body'].read()
return s3_file.decode('utf-8').strip()
我正在处理从 Redshift
数据库中转储文件的过程,并且希望不必在本地下载文件来处理数据。我看到 Java
有一个 StreamingObject
class 可以满足我的要求,但我在 boto3
.
如果您有一个 mybucket
S3 存储桶,其中包含一个 beer
密钥,下面是如何下载和获取值而不将其存储在本地文件中:
import boto3
s3 = boto3.resource('s3')
print s3.Object('mybucket', 'beer').get()['Body'].read()
这可能与您想要做的事情相关,也可能不相关,但对于我的情况来说,使用临时文件是一件很有效的事情:
import tempfile
import boto3
bucket_name = '[BUCKET_NAME]'
key_name = '[OBJECT_KEY_NAME]'
s3 = boto3.resource('s3')
temp = tempfile.NamedTemporaryFile()
s3.Bucket(bucket_name).download_file(key_name, temp.name)
# do what you will with your file...
temp.close()
我使用那个解决方案,实际上:
import boto3
s3_client = boto3.client('s3')
def get_content_from_s3(bucket: str, key: str) -> str:
"""Save s3 content locally
param: bucket, s3 bucket
param: key, path to the file, f.i. folder/subfolder/file.txt
"""
s3_file = s3_client.get_ojct(Bucket=bucket, Key=key)['Body'].read()
return s3_file.decode('utf-8').strip()