Skip to content

Instantly share code, notes, and snippets.

@sebsto
Created May 4, 2019 12:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebsto/869141beecee15a6b326bf1f0aad9da3 to your computer and use it in GitHub Desktop.
Save sebsto/869141beecee15a6b326bf1f0aad9da3 to your computer and use it in GitHub Desktop.
Bien démarrer avec Amazon S3 et Python
import logging
import boto3
from botocore.exceptions import ClientError
def upload_file(file_name, bucket, object_name=None):
"""Upload a file to an S3 bucket
:param file_name: File to upload
:param bucket: Bucket to upload to
:param object_name: S3 object name. If not specified then file_name is used
:return: True if file was uploaded, else False
"""
# If S3 object_name was not specified, use file_name
if object_name is None:
object_name = file_name
# Upload the file
s3_client = boto3.client('s3')
try:
response = s3_client.upload_file(file_name, bucket, object_name)
except ClientError as e:
logging.error(e)
return False
return True
def download_file(file_name, bucket_name, object_name):
s3 = boto3.client('s3')
s3.download_file(bucket_name, object_name, file_name)
print("downloading demo.jpg")
download_file("demo.jpg", "demo-bien-demarrer", "barcelona.jpg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment