S3Loader
A class for loading data from Amazon S3 buckets.
This class extends the _S3Connection parent class and provides methods for
establishing connections to S3 buckets and loading data from S3 objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Package to use for S3 connections ('s3fs' or 'boto3'). Defaults to "boto3". |
'boto3'
|
Attributes:
| Name | Type | Description |
|---|---|---|
s3 |
The S3 connection object (initialized when needed) |
|
s3_package |
str
|
The package being used for S3 connectivity |
Examples:
Load CSV from S3 using boto3:
>>> loader = S3Loader()
>>> loader.connect(
... aws_access_key_id='YOUR_KEY',
... aws_secret_access_key='YOUR_SECRET'
... )
>>> data = loader.load(
... bucket='my-bucket',
... key='path/to/file.csv'
... )
Load Excel file using s3fs:
>>> loader = S3Loader(s3_package='s3fs')
>>> loader.connect() # Uses environment variables
>>> data = loader.load(
... bucket='my-bucket',
... key='path/to/file.xlsx',
... sheet_name='Data'
... )
Methods:
| Name | Description |
|---|---|
connect |
Establish a connection to the S3 bucket. |
load |
Load data from a specified S3 object based on its file extension. |
Source code in dashboard_template_database/storage/s3/loader.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |
connect
connect(**kwargs) -> None
Establish a connection to the S3 bucket.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Additional keyword arguments for establishing the connection. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
object |
None
|
The established S3 connection. |
Example :
s3_loader = S3Loader(package='boto3') s3_connection = s3_loader.connect(aws_access_key_id='your_access_key', aws_secret_access_key='your_secret_key')
Source code in dashboard_template_database/storage/s3/loader.py
load
Load data from a specified S3 object based on its file extension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The name of the S3 bucket. |
required |
|
str
|
The key of the S3 object to load. |
required |
|
Additional keyword arguments for reading the data. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
object |
None
|
The loaded data (Pandas DataFrame, JSON object, Pickle object, or GeoDataFrame). |
Example :
s3_loader = S3Loader(package='boto3') s3_connection = s3_loader.connect(aws_access_key_id='your_access_key', aws_secret_access_key='your_secret_key') data = s3_loader.load(bucket='your_bucket', key='your_file.csv')