python - 如何使用 Apache Libcloud 在 Amazon S3 上列出给定密钥中的所有内容?
问题描述
使用 boto3 在 S3 中列出内容的代码是已知的:
self.s3_client = boto3.client(
u's3',
aws_access_key_id=config.AWS_ACCESS_KEY_ID,
aws_secret_access_key=config.AWS_SECRET_ACCESS_KEY,
region_name=config.region_name,
config=Config(signature_version='s3v4')
)
versions = self.s3_client.list_objects(Bucket=self.bucket_name, Prefix=self.package_s3_version_key)
但是,我需要使用 libcloud 列出 S3 上的内容。我在文档中找不到它。
解决方案
If you are just looking for all the contents for a specific bucket:
from libcloud.storage.types import Provider
from libcloud.storage.providers import get_driver
client = driver(StoreProvider.S3)
s3 = client(aws_id, aws_secret)
container = s3.get_container(container_name='name')
objects = s3.list_container_objects(container)
s3.download_object(objects[0], '/path/to/download')
The resulting objects
will contain a list of all the keys in that bucket with filename, byte size, and metadata. To download call the download_object
method on s3 with the full libcloud Object and your file path.
If you'd rather get all objects of all buckets, change get_container
to list_containers
with no parameters.
Information for all driver methods: https://libcloud.readthedocs.io/en/latest/storage/api.html
Short examples specific to s3: https://libcloud.readthedocs.io/en/latest/storage/drivers/s3.html
推荐阅读
- razor - 在 Blazor 页面中使用 SVGS
- javascript - Angular 7过滤器下拉值
- c# - Gmail API:使用包含变音符号的邮件地址会导致 SMTPEception:客户端或服务器仅配置为具有 ASCII 本地部分的地址
- makefile - 为什么我的 makefile 永远不会停止构建?
- javascript - MS Edge 随机不加载脚本
- node.js - Swagger 模块中的前缀 url
- reactjs - 表单中的多个输入元素 - React
- python - 从多个 Python 文件中删除特定的代码行
- python - Python在具有可变长度的对象构造函数中创建二维数组
- xamarin.forms - 升级到 4.5.0.396 后 Xamarin.iOS 项目缺少引用错误