Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TypeError: can only concatenate list (not "str") to list when upload to aws s3 bucket using boto3

I am trying to create a script that will place a file inside a folder in the s3 bucket. Here’s my code:

AccountName = ["AWSAccountName"]

def upload_s3(AccountName,bucket,filename):
    dir = os.getcwd()
    s3 = boto3.client('s3')
    #prefix = 'folder_' + datetime.now().strftime("%Y-%m-%d_%H-%M-%S") + "/" + filename
    prefix =  AccountName + 'folder_' + datetime.now().strftime("%Y-%m-%d_%H-%M-%S") + "/" + filename
    #s3object = s3.put_object(Bucket=bucket, Key=prefix)
    s3.upload_file(dir + '/' +filename, bucket, prefix)

if __name__ == "__main__":
    upload_s3(AccountName, "ec2-idle-workspace-list","EC2_Idle_info_cpu_NetworkIn_NetworkOut.csv")

However, while running the script, it gives me the following error:

 prefix =  AccountName + 'folder_' + datetime.now().strftime("%Y-%m-%d_%H-%M-%S") + "/" + filename
TypeError: can only concatenate list (not "str") to list
[Pipeline] echo
script returned exit code 1

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You are trying to concatenate AccountName + 'folder_' + datetime.now().strftime("%Y-%m-%d_%H-%M-%S") + "/" + filename but AccountName is a list, not a string.

Replace AccountName = ["AWSAccountName"] with AccountName = "AWSAccountName".

Or replace
AccountName + 'folder_' + datetime.now().strftime("%Y-%m-%d_%H-%M-%S") + "/" + filename
With
AccountName[0] + 'folder_' + datetime.now().strftime("%Y-%m-%d_%H-%M-%S") + "/" + filename

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading