If you are trying to use S3 to store files in your project. I hope that this simple example will be helpful for you.
Install Boto3 via PIP
1 |
pip install boto3 |
Please take a look to the source code at https://github.com/thanhson1085/python-s3 before reading this post.
With boto3, It is easy to push file to S3. Please make sure that you had a AWS account and created a bucket in S3 service.
1 2 3 4 |
transfer = S3Transfer(boto3.client('s3', cfg.AWS_REGION, aws_access_key_id=cfg.AWS_APP_ID, aws_secret_access_key=cfg.AWS_APP_SECRET)) transfer.upload_file(file_path, AWS_BUCKET, file_path) |
And it is not difficult to save file from S3 to local:
1 2 3 4 |
transfer = S3Transfer(boto3.client('s3', cfg.AWS_REGION, aws_access_key_id=cfg.AWS_APP_ID, aws_secret_access_key=cfg.AWS_APP_SECRET)) # download file from aws transfer.download_file(AWS_BUCKET, key, key) |