Skip to content

Instantly share code, notes, and snippets.

@feelinc
Last active November 2, 2023 14:53
Show Gist options
  • Star 61 You must be signed in to star a gist
  • Fork 22 You must be signed in to fork a gist
  • Save feelinc/d1f541af4f31d09a2ec3 to your computer and use it in GitHub Desktop.
Save feelinc/d1f541af4f31d09a2ec3 to your computer and use it in GitHub Desktop.
Upload folder contents to AWS S3
#!/usr/bin/python
import os
import sys
import boto3
# get an access token, local (from) directory, and S3 (to) directory
# from the command-line
local_directory, bucket, destination = sys.argv[1:4]
client = boto3.client('s3')
# enumerate local files recursively
for root, dirs, files in os.walk(local_directory):
for filename in files:
# construct the full local path
local_path = os.path.join(root, filename)
# construct the full Dropbox path
relative_path = os.path.relpath(local_path, local_directory)
s3_path = os.path.join(destination, relative_path)
# relative_path = os.path.relpath(os.path.join(root, filename))
print 'Searching "%s" in "%s"' % (s3_path, bucket)
try:
client.head_object(Bucket=bucket, Key=s3_path)
print "Path found on S3! Skipping %s..." % s3_path
# try:
# client.delete_object(Bucket=bucket, Key=s3_path)
# except:
# print "Unable to delete %s..." % s3_path
except:
print "Uploading %s..." % s3_path
client.upload_file(local_path, bucket, s3_path)
UploadDirS3.py /path/to/local/folder thebucketname /path/to/s3/folder
Make sure to read the boto3 credentials placement doc
@tobiadegoke
Copy link

Can you use placeholder to specify where I will need to change the parameters?

@devansh1305
Copy link

Updated for Python3

#!/usr/bin/python

import os
import sys
import boto3

# get an access token, local (from) directory, and S3 (to) directory
# from the command-line
local_directory, bucket, destination = sys.argv[1:4]

client = boto3.client('s3')

# enumerate local files recursively
for root, dirs, files in os.walk(local_directory):

  for filename in files:

    # construct the full local path
    local_path = os.path.join(root, filename)

    # construct the full Dropbox path
    relative_path = os.path.relpath(local_path, local_directory)
    s3_path = os.path.join(destination, relative_path)

    # relative_path = os.path.relpath(os.path.join(root, filename))

    print('Searching "%s" in "%s"' % (s3_path, bucket))
    try:
        client.head_object(Bucket=bucket, Key=s3_path)
        print "Path found on S3! Skipping %s..." % s3_path

        # try:
            # client.delete_object(Bucket=bucket, Key=s3_path)
        # except:
            # print("Unable to delete %s..." % s3_path)
    except:
        print("Uploading %s..." % s3_path)
        client.upload_file(local_path, bucket, s3_path)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment