擅长:python、mysql、java
<p>使用boto3下载文件非常简单,在使用此代码之前,请在系统级别配置您的AWS凭据。</p>
<pre><code>client = boto3.client('s3')
// if your bucket name is mybucket and the file path is test/abc.txt
// then the Bucket='mybucket' Prefix='test'
resp = client.list_objects_v2(Bucket="<your bucket name>", Prefix="<prefix of the s3 folder>")
for obj in resp['Contents']:
key = obj['Key']
//to read s3 file contents as String
response = client.get_object(Bucket="<your bucket name>",
Key=key)
print(response['Body'].read().decode('utf-8'))
//to download the file to local
client.download_file('<your bucket name>', key, key.replace('test',''))
</code></pre>
<p>replace是用s3文件名在本地定位文件,如果不替换,它将尝试另存为“test/abc.txt”。</p>