Ad
Uploading Folder From Local System To A Perticular Folder In S3
What should I change in my code so that I can upload my entire folder from local system to a particular folder present in my s3 bucket.
import os
import boto3
s3_resource = boto3.resource("s3", region_name="ap-south-1")
def upload_objects():
try:
bucket_name = "<S3 bucket-name>"
root_path = '<local folder path>'
bucket_folder = '<S3 folder name>'
my_bucket = s3_resource.Bucket(bucket_name)
# s3 = boto3.resource('s3')
for path, subdirs, files in os.walk(root_path):
path = path.replace("\\","/")
directory_name = path.replace(root_path,"")
for file in files:
my_bucket.upload_file(os.path.join(path, file), directory_name+'/'+file)
except Exception as err:
print(err)
if __name__ == '__main__':
upload_objects()
Ad
Answer
You are not using your bucket_folder
at all. This should be the beginning of your S3 prefix as in the S3 there are no folders. Its all about key names and prefixes.
So it should be something as the following:
my_bucket.upload_file(os.path.join(path, file), bucket_folder + '/' + directory_name+'/'+file)
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad