2
votes
import boto3

_BUCKET_NAME = 'Bucket_Name'
_PREFIX = 'data/'

ACCESS_KEY='*********************'
SECRET_KEY='**************************'

client = boto3.client('s3', aws_access_key_id=ACCESS_KEY,
                            aws_secret_access_key=SECRET_KEY)

def ListFiles(client):
    """List files in specific S3 URL"""
    response = client.list_objects(Bucket=_BUCKET_NAME, Prefix=_PREFIX)
    for content in response.get('Contents', []):
        yield content.get('Key')

file_list = ListFiles(client)
for file in file_list:
client.download_file('Bucket_Name',file,'C:/Users/UserName/Desktop/folder/')

Traceback (most recent call last):

File "", line 21, in client.download_file('Bucket_Name',file,'C:/Users/User/Desktop/folder')

File "C:\ProgramData\Anaconda3\lib\site-packages\boto3\s3\inject.py", line 172, in download_file extra_args=ExtraArgs, callback=Callback)

File "C:\ProgramData\Anaconda3\lib\site-packages\boto3\s3\transfer.py", line 307, in download_file future.result()

File "C:\ProgramData\Anaconda3\lib\site-packages\s3transfer\futures.py", line 106, in result return self._coordinator.result()

File "C:\ProgramData\Anaconda3\lib\site-packages\s3transfer\futures.py", line 265, in result raise self._exception

File "C:\ProgramData\Anaconda3\lib\site-packages\s3transfer\tasks.py", line 126, in call return self._execute_main(kwargs)

File "C:\ProgramData\Anaconda3\lib\site-packages\s3transfer\tasks.py", line 150, in _execute_main return_value = self._main(**kwargs)

File "C:\ProgramData\Anaconda3\lib\site-packages\s3transfer\download.py", line 603, in _main osutil.rename_file(fileobj.name, final_filename)

File "C:\ProgramData\Anaconda3\lib\site-packages\s3transfer\utils.py", line 271, in rename_file rename_file(current_filename, new_filename)

File "C:\ProgramData\Anaconda3\lib\site-packages\s3transfer\compat.py", line 25, in rename_file os.remove(new_filename)

PermissionError: [WinError 5] Access is denied: 'C:/Users/User/Desktop/folder'

2
Please format your code and add more details about what you are trying to accomplish.Vishnu Dasu
now. you may checkSonu Kumar

2 Answers

1
votes

Permission error message is misleading. The download_file expects a target file name parameter and not a folder

client.download_file('Bucket_Name',object_key,'C:\\temp\\file_name.txt')
0
votes

You are missing a forward slash at the end of your path. Python thinks it is a file instead of a folder and throws an error. Replace last line

client.download_file('Bucket_Name',file,'C:/Users/UserName/Desktop/folder')

with:

client.download_file('Bucket_Name',file,'C:/Users/UserName/Desktop/folder/')