Upload a file to AWS S3 using Boto3
This Python snippet demonstrates how to upload a test file to an Amazon S3 bucket. It handles file creation, writing content, and uploading using the Boto3 library. The script also includes error handling for missing credentials.import boto3 from botocore.exceptions import NoCredentialsError # Create an S3 client s3 = boto3.client('s3') # Upload a test file try: # Specify the file content and bucket details file_name = '/tmp/test_file.txt' bucket_name = 'symbolix-stg-bucket' file_content = "This is a test file." # Write the content to the file with open(file_name, 'w') as file: file.write(file_content) # Upload the file to the S3 bucket s3.upload_file(file_name, bucket_name, 'test_file.txt') print("File uploaded successfully.") except NoCredentialsError: print("Credentials not found.") except Exception as e: print(f"Error uploading file: {str(e)}")
Leave a Comment