How to upload folders on cloud9 to S3 bucket

0

Hello all, please i have a blocker uploading folders from cloud9 to s3 because it is emphasizing specifying file. Is there a way around it?

asked 8 months ago535 views
2 Answers
0

S3 differs from a normal filesystem as it has a flat structure instead of hierarchical as we are used to, so there's no real folders in S3, it's simulated with prefixes.

That said there is no supported operation on s3 to upload a folder directly, instead you must especify all files to be uploaded. You might think you can, through console experience, but that's a convenience, behind the curtains the UI uploads each file individually.

On Cloud9 you could use a code script to upload all files from the desired folder into an s3 "folder", one by one or in parallel with threading. I will leave here a code snippet using boto3 on python to upload one by one:

import os

import boto3 

S3_BUCKET= 'my_bucket' 
S3_PREFIX = 'path/to/my/files/' 

LOCAL_FOLDER_PATH = '/path/to/files/'

session = boto3.Session()  # Authentication happens through EC2 role
s3 = session.client('s3') 

for file in os.listdir(LOCAL_FOLDER_PATH):
    s3_file = S3_PREFIX + file
    s3.upload_file(file, S3_BUCKET, s3_file)

Note that there are a another ways to upload using boto3, another is to create a higher-level API object using **session.resource('s3) **and use it's methods but resources are not updated anymore so may lack a few features.

Also important to say that there are indeed some solutions that will let you upload a folder to s3 in single call, but they all have to upload one by one under the hood.

Hope this answers your question, if you still have any question please let me know.

answered 8 months ago
0

For multiple folders and files, you can use AWS CLI's s3 sync command. To sync from local folder in Cloud 9 to S3 bucket

aws s3 sync . s3://<MYBUCKET>/<FOLDER>

If you have not, you will need to configure your credentials, i.e.

aws configure

If this is a few files, consider using AWS Toolkit. See Upload a file to an S3 bucket using the interface

AWS
EXPERT
Mike_L
answered 8 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions