如何使用 Python 访问 boto 中存储桶中文件夹中的文件?
How to access a file in a folder in a bucket in boto using Python?
我能找到的只有 this,它列出了所有文件。上架成功
我只想通过 link 访问特定文件。文件在文件夹中。
比如我要从下面下载URL:
s3://my_bucket/my_folder/my_next_folder/my_file.csv
无需遍历整个文件树。
你试过了吗this:
import boto3
import botocore
BUCKET_NAME = 'my-bucket' # replace with your bucket name
KEY = 'my_image_in_s3.jpg' # replace with your object key
s3 = boto3.resource('s3')
try:
s3.Bucket(BUCKET_NAME).download_file(KEY, 'my_local_image.jpg')
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == "404":
print("The object does not exist.")
else:
raise
from boto.s3.key import Key
from boto.s3.connection import S3Connection
conn = S3Connection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
s3_url_split = s3_url.split('/')
bucket_name = s3_url_split[2]
dir_name = '/'.join(s3_url_split[3:-1])
bucket = conn.get_bucket(bucket_name)
file_name = s3_url_split[-1]
k = Key(bucket)
k.key = dir_name + "/" + file_name
k.get_contents_to_filename(dst_path)
我缺少的是密钥已经包含文件夹路径。
我能找到的只有 this,它列出了所有文件。上架成功
我只想通过 link 访问特定文件。文件在文件夹中。
比如我要从下面下载URL:
s3://my_bucket/my_folder/my_next_folder/my_file.csv
无需遍历整个文件树。
你试过了吗this:
import boto3
import botocore
BUCKET_NAME = 'my-bucket' # replace with your bucket name
KEY = 'my_image_in_s3.jpg' # replace with your object key
s3 = boto3.resource('s3')
try:
s3.Bucket(BUCKET_NAME).download_file(KEY, 'my_local_image.jpg')
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == "404":
print("The object does not exist.")
else:
raise
from boto.s3.key import Key
from boto.s3.connection import S3Connection
conn = S3Connection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
s3_url_split = s3_url.split('/')
bucket_name = s3_url_split[2]
dir_name = '/'.join(s3_url_split[3:-1])
bucket = conn.get_bucket(bucket_name)
file_name = s3_url_split[-1]
k = Key(bucket)
k.key = dir_name + "/" + file_name
k.get_contents_to_filename(dst_path)
我缺少的是密钥已经包含文件夹路径。