Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jupyter-naas
GitHub Repository: jupyter-naas/awesome-notebooks
Path: blob/master/AWS/AWS_Upload_file_to_S3_bucket.ipynb
2973 views
Kernel: Python 3

AWS.png

AWS - Upload file to S3 bucket

Give Feedback | Bug report

Tags: #aws #cloud #storage #S3bucket #snippet #operations# AWS - Upload file to S3 bucket

Last update: 2023-11-20 (Created: 2021-08-03)

Description: This notebook upload a file to an Amazon Web Services (AWS) S3 bucket, allowing for secure storage and easy access to the file. It is a simple and efficient way to store and manage data in the cloud.

Input

Import libraries

import naas try: import boto3 except: !pip install boto3 getpass4 import boto3

Setup variables

Mandatory

  • aws_access_key_id: This variable is used to store the AWS access key ID.

  • aws_secret_access_key: This variable is used to store the AWS secret access key.

  • bucket_name: The name of the S3 bucket from which you want to list the files.

  • file_path: The path of the file to be uploaded.

# Mandatory aws_access_key_id = naas.secret.get("AWS_ACCESS_KEY_ID") or "YOUR_AWS_ACCESS_KEY_ID" aws_secret_access_key = naas.secret.get("AWS_SECRET_ACCESS_KEY") or "YOUR_AWS_SECRET_ACCESS_KEY" bucket_name = "naas-example" file_path = "AWS_Upload_file_to_S3_bucket.ipynb"

Model

Connect to AWS

s3 = boto3.client( "s3", aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key )

Output

Upload data

with open(file_path, "rb") as f: s3.upload_fileobj(f, bucket_name, file_path)