使用 python,您可以使用 boto3 库,我发现它对于解决类似情况非常有用。
示例代码:
import boto3
import os
KEY = ''
SECRET = ''
download_folder = os.path.join(os.path.expanduser('~'), 'Downloads')
bucket = 'bucketSample'
folders = ['abc', 'def', 'xyz']
prefixes = ['hij_']
try:
# Needed for the pagination method in order to get objects with certain prefixes instead of iterating over all objects, you should get the aws_access_key_id and aws_secret_access_key for your bucket if available
s3 = boto3.resource(
's3',
aws_access_key_id=KEY,
aws_secret_access_key=SECRET)
# Needed for the download method, you should get the aws_access_key_id and aws_secret_access_key for your bucket if available
client = boto3.client(
's3',
aws_access_key_id=KEY,
aws_secret_access_key=SECRET)
# Get paginated objects
paginator = client.get_paginator('list_objects')
for folder in folders:
for file_prefix in prefixes:
prefix = folder + file_prefix
page_iterator = paginator.paginate(Bucket=bucket, Prefix=prefix)
if page_iterator:
for page in page_iterator:
if 'Contents' in page:
for content in page['Contents']:
file_path = os.path.join(download_folder, content['Key'])
s3.meta.client.download_file(bucket, str(content['Key']), file_path)
except:
print('An error occurred')