0

I want to upload folder in S3, but I have lot of folders which have space between their names. It's an automate process, so could not change the name of the folders manually (from abc d to 'abc d').

folder_name = abc d
import os
cmd = 'aws s3 sync'+''+folder_name+'/'+''+s3path+folder_name+'/'
os.system(cmd)
3
  • Are you receiving a specific error? Try print(cmd) to see what it is try to run. You can also use an f-string, such as cmd = f"aws s3 sync '{folder_name}/' '{s3path}{folder_name}/'". Commented Apr 12, 2022 at 7:54
  • It's working fine if the folder name is not having any space. It will be fine if I could change the folder name. Name=abc d.... To Name='abc d' Commented Apr 12, 2022 at 8:27
  • That code can't possibly work. Please post the code you're using when asking questions. As a guess, you need to quote strings in arguments: cmd = 'aws s3 sync "'+folder_name+'/" "'+s3path+folder_name+'/"' Commented Apr 12, 2022 at 14:49

1 Answer 1

1

Will be pasting ,my code here. Will be glad if it helps:)

!pip install boto3  ##install boto3
!pip install s3fs  ##required package

import boto3
import pandas as pd

s3 = boto3.client('s3')

s3 = boto3.resource(
service_name='s3',
region_name='us-east-2',
aws_access_key_id='',
aws_secret_access_key='')

# Upload files to S3 bucket
s3.Bucket('bucket_name').upload_file(Filename='foo.csv', Key='foo.csv')
s3.Bucket('bucket_name').upload_file(Filename='bar.csv', Key='bar.csv')

for obj in s3.Bucket('bucket_name').objects.all():
    print(obj)

Note :

  1. the last for loop will print the keys of files that you've uploaded (for confirmation).
  2. aws_access_key_id and aws_secret_access_key - both of these will be available in your aws profile.
Sign up to request clarification or add additional context in comments.

1 Comment

Ohhh great. But suppose I want to change the folder names like if their any space is present then folder name will be inside of single/double quotes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.