简体   繁体   English

如何使用 Python 访问 boto 中存储桶中的文件夹中的文件?

[英]How to access a file in a folder in a bucket in boto using Python?

All I could find was this , which lists all the files.我只能找到这个,它列出了所有文件。 The listing succeeds.上市成功。

I want to just access a specific file by its link.我只想通过链接访问特定文件。 The file is in a folder.该文件位于文件夹中。

For example, I want to download from the following URL:比如我想从下面的URL下载:

s3://my_bucket/my_folder/my_next_folder/my_file.csv

Without traversing the entire file tree.无需遍历整个文件树。

Have you try 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)

Thing I was missing was the key already contains the folder path.我缺少的是密钥已经包含文件夹路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用python boto3将文件上传到aws S3存储桶中的文件夹 - How to upload file to folder in aws S3 bucket using python boto3 如何使用条件为 python boto3 代码在 s3 存储桶中创建文件夹 - how to create a folder in s3 bucket by using python boto3 code for with conditions 如何使用 Python Boto3 列出和读取 S3 存储桶的特定文件夹中的每个文件 - How to list and read each of the files in specific folder of an S3 bucket using Python Boto3 如何使用boto3删除AWS桶中的文件夹及其内容 - How to delete folder and its content in a AWS bucket using boto3 如何使用Python libcloud访问Google存储桶中的文件夹“ /”? - How to access a folder “/” in the Google bucket using Python libcloud? 如何使用 python (boto3) 连接到带有 pem 文件的 Amazon S3 存储桶 - How do I connect to an Amazon S3 bucket with a pem file using python (boto3) 如何使用Python中的Boto检查文件是否已完成上传到S3 Bucket? - How to check if a file has completed uploading into S3 Bucket using Boto in Python? 如何使用 Python 和 Boto3 从 S3 Bucket 读取 Txt 文件 - How to read Txt file from S3 Bucket using Python And Boto3 如何使用 boto 和 python 从存储桶中删除 s3 版本 - How to delete a s3 version from a bucket using boto and python 如何使用Boto使用python监视AWS S3存储桶? - How to monitor a AWS S3 bucket with python using boto?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM