首页 > 解决方案 > 如何使用 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 上的内容。我在文档中找不到它。

标签: pythonpython-3.xamazon-s3libcloud

解决方案


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


推荐阅读