我正在尝试复制AWS CLI ls
命令,以递归方式列出AWS S3存储桶中的文件。例如,我将使用以下命令以递归方式列出“location2”存储桶中的所有文件。
aws s3 ls s3://location2 --recursive
boto3
的{{3}}(即aws s3 ls s3://location2 --recursive
)等价物是什么?
答案 0 :(得分:2)
使用AWS SDK时无需使用--recursive选项,因为它使用list_objects方法列出存储桶中的所有对象。
import boto3
client = boto3.client('s3')
client.list_objects(Bucket='MyBucket')
答案 1 :(得分:0)
使用更高级别的API并使用资源是可行的方法。
import UIKit
import Parse
var promoNum = -1
class TableViewController: UITableViewController, CLLocationManagerDelegate {
var titles = [String]()
override func viewDidLoad() {
super.viewDidLoad()
var getPromosQuery = PFQuery(className: "Promotions")
getPromosQuery.whereKey("zip", equalTo: "85281")
getPromosQuery.findObjectsInBackgroundWithBlock { (objects, error) in
if let objects = objects {
for object in objects {
self.titles.append(object["title"] as! String)
}
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return titles.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let promoCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! Cell
promoCell.promotionTitle.text = titles[indexPath.row]
return promoCell
}
override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
promoNum = indexPath.row
return indexPath
}
}
答案 2 :(得分:0)
您还可以使用minio-py客户端库,其开源和与AWS S3兼容。
下面的list_objects.py示例,您可以参考docs了解更多信息。
from minio import Minio client = Minio('s3.amazonaws.com', access_key='YOUR-ACCESSKEYID', secret_key='YOUR-SECRETACCESSKEY') # List all object paths in bucket that begin with my-prefixname. objects = client.list_objects('my-bucketname', prefix='my-prefixname', recursive=True) for obj in objects: print(obj.bucket_name, obj.object_name.encode('utf-8'), obj.last_modified, obj.etag, obj.size, obj.content_type)
希望它有所帮助。
免责声明:我为Minio
工作答案 3 :(得分:0)
您需要使用paginators:
bucket = "my-bucket"
import boto3
client = boto3.client("s3")
paginator = client.get_paginator('list_objects')
page_iterator = paginator.paginate(Bucket=bucket)
for page in page_iterator:
for obj in page['Contents']:
print("s3://%s/%s" % (bucket, obj["Key"]))