S3 User Storage Access

[20]:
import os
import boto3
import s3fs
[14]:
# start an s3 session with boto3
s3 = boto3.client('s3')

bucket_name = "bioscape-smce-user-bucket"
sub_dir_name = "edlang"
object_name = "example_write.txt"
[15]:
# creates a dummy file
with open(object_name, 'w') as f:
    f.write("Hello World!")
[16]:
# upload the file with boto3
try:
    response = s3.upload_file("example_write.txt", bucket_name, os.path.join(sub_dir_name, object_name))
    print(f"File uploaded successfully to s3://{bucket_name}/{object_name}")
except Exception as e:
    print(f"Error uploading file: {e}")
File uploaded successfully to s3://bioscape-smce-user-bucket/example_write.txt
[17]:
# read the file with boto3
try:
    response = s3.get_object(Bucket=bucket_name, Key=os.path.join(sub_dir_name, object_name))
    content = response['Body'].read().decode('utf-8')
    print(content)
except Exception as e:
    print(f"Error reading file from S3: {e}")
Contents of the file:
Hello World!
[21]:
# read the file with s3fs
s3 = s3fs.S3FileSystem(anon=False)

# Read the file from S3
with s3.open(os.path.join(bucket_name, sub_dir_name, object_name), 'r') as f:
    content = f.read()
print(content)
Hello World!