[英]How to list all the contents in a given key on Amazon S3 using Apache Libcloud?
The code to list contents in S3 using boto3 is known: 使用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)
However, I need to list contents on S3 using libcloud. 但是,我需要使用libcloud列出S3上的内容。 I could not find it in the documentation.
我在文档中找不到它。
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. 生成的
objects
将包含该存储桶中所有键的列表,并包含文件名,字节大小和元数据。 To download call the download_object
method on s3 with the full libcloud Object and your file path. 要下载,请使用完整的libcloud Object和您的文件路径在s3上调用
download_object
方法。
If you'd rather get all objects of all buckets, change get_container
to list_containers
with no parameters. 如果你宁愿让所有的桶中的所有对象,改变
get_container
到list_containers
不带参数。
Information for all driver methods: https://libcloud.readthedocs.io/en/latest/storage/api.html 所有驱动程序方法的信息: https : //libcloud.readthedocs.io/en/latest/storage/api.html
Short examples specific to s3: https://libcloud.readthedocs.io/en/latest/storage/drivers/s3.html 特定于s3的简短示例: https : //libcloud.readthedocs.io/en/latest/storage/drivers/s3.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.