0
# Cloud Storage Integration
1
2
Native integration with AWS S3 and Azure Blob Storage including credential management, encryption support, and stage management for external data access.
3
4
## Capabilities
5
6
### AWS S3 Integration
7
8
Comprehensive AWS S3 bucket support with credential management and encryption options.
9
10
```python { .api }
11
from snowflake.sqlalchemy import AWSBucket
12
13
class AWSBucket:
14
"""AWS S3 bucket descriptor with credentials and encryption."""
15
16
def __init__(self, bucket_name: str, path: str = ""):
17
"""
18
Create AWS bucket descriptor.
19
20
Args:
21
bucket_name: S3 bucket name
22
path: Optional path within bucket
23
"""
24
25
@classmethod
26
def from_uri(cls, uri: str):
27
"""
28
Create bucket from S3 URI.
29
30
Args:
31
uri: S3 URI (s3://bucket/path)
32
33
Returns:
34
AWSBucket: Bucket instance
35
"""
36
37
def credentials(self, aws_role=None, aws_key_id=None,
38
aws_secret_key=None, aws_token=None):
39
"""
40
Set AWS credentials.
41
42
Args:
43
aws_role: IAM role ARN
44
aws_key_id: AWS access key ID
45
aws_secret_key: AWS secret access key
46
aws_token: AWS session token
47
48
Returns:
49
AWSBucket: Self for method chaining
50
"""
51
52
def encryption_aws_cse(self, master_key: str):
53
"""
54
Set AWS client-side encryption.
55
56
Args:
57
master_key: Encryption master key
58
59
Returns:
60
AWSBucket: Self for method chaining
61
"""
62
63
def encryption_aws_sse_s3(self):
64
"""
65
Set AWS server-side encryption with S3 keys.
66
67
Returns:
68
AWSBucket: Self for method chaining
69
"""
70
71
def encryption_aws_sse_kms(self, kms_key_id: str = None):
72
"""
73
Set AWS server-side encryption with KMS.
74
75
Args:
76
kms_key_id: Optional KMS key ID
77
78
Returns:
79
AWSBucket: Self for method chaining
80
"""
81
```
82
83
### Azure Blob Storage Integration
84
85
Azure container support with SAS token authentication and encryption.
86
87
```python { .api }
88
from snowflake.sqlalchemy import AzureContainer
89
90
class AzureContainer:
91
"""Azure container descriptor with SAS token and encryption."""
92
93
def __init__(self, account_name: str, container_name: str, path: str = ""):
94
"""
95
Create Azure container descriptor.
96
97
Args:
98
account_name: Azure storage account name
99
container_name: Container name
100
path: Optional path within container
101
"""
102
103
@classmethod
104
def from_uri(cls, uri: str):
105
"""
106
Create container from Azure URI.
107
108
Args:
109
uri: Azure URI (azure://account.blob.core.windows.net/container/path)
110
111
Returns:
112
AzureContainer: Container instance
113
"""
114
115
def credentials(self, azure_sas_token: str):
116
"""
117
Set Azure SAS token.
118
119
Args:
120
azure_sas_token: Shared Access Signature token
121
122
Returns:
123
AzureContainer: Self for method chaining
124
"""
125
126
def encryption_azure_cse(self, master_key: str):
127
"""
128
Set Azure client-side encryption.
129
130
Args:
131
master_key: Encryption master key
132
133
Returns:
134
AzureContainer: Self for method chaining
135
"""
136
```
137
138
### External Stage Management
139
140
External stage descriptors for accessing cloud storage locations.
141
142
```python { .api }
143
from snowflake.sqlalchemy import ExternalStage
144
145
class ExternalStage:
146
"""External stage descriptor for cloud storage access."""
147
148
def __init__(self, stage_name: str, path: str = ""):
149
"""
150
Create external stage descriptor.
151
152
Args:
153
stage_name: Stage name
154
path: Optional path within stage
155
"""
156
157
@classmethod
158
def from_parent_stage(cls, parent_stage, path: str, file_format=None):
159
"""
160
Create stage from parent stage.
161
162
Args:
163
parent_stage: Parent stage
164
path: Path within parent stage
165
file_format: Optional file format
166
167
Returns:
168
ExternalStage: Stage instance
169
"""
170
```
171
172
## Usage Examples
173
174
### AWS S3 Integration
175
176
```python
177
from snowflake.sqlalchemy import AWSBucket, CopyIntoStorage
178
179
# Create S3 bucket with IAM role
180
s3_bucket = (AWSBucket
181
.from_uri('s3://my-data-bucket/sales/')
182
.credentials(aws_role='arn:aws:iam::123456789012:role/SnowflakeRole')
183
)
184
185
# Copy from S3 with server-side encryption
186
s3_encrypted = (AWSBucket('secure-bucket', 'sensitive-data/')
187
.credentials(aws_key_id='AKIAIOSFODNN7EXAMPLE',
188
aws_secret_key='wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY')
189
.encryption_aws_sse_kms('arn:aws:kms:us-east-1:123456789012:key/12345678-1234-1234-1234-123456789012')
190
)
191
192
# Use in COPY statement
193
copy_from_s3 = CopyIntoStorage(
194
table=sales_table,
195
stage_location=s3_bucket,
196
file_format=csv_format
197
)
198
```
199
200
### AWS S3 with Client-Side Encryption
201
202
```python
203
# S3 with client-side encryption
204
s3_cse = (AWSBucket('encrypted-bucket')
205
.credentials(aws_key_id='AKIAIOSFODNN7EXAMPLE',
206
aws_secret_key='wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY')
207
.encryption_aws_cse('my-master-encryption-key')
208
)
209
210
copy_encrypted = CopyIntoStorage(
211
table=sensitive_table,
212
stage_location=s3_cse
213
).force(True)
214
```
215
216
### Azure Blob Storage Integration
217
218
```python
219
from snowflake.sqlalchemy import AzureContainer
220
221
# Create Azure container with SAS token
222
azure_container = (AzureContainer
223
.from_uri('azure://myaccount.blob.core.windows.net/data/exports/')
224
.credentials(azure_sas_token='sp=r&st=2023-01-01T00:00:00Z&se=2024-01-01T00:00:00Z&spr=https&sv=2022-11-02&sr=c&sig=signature')
225
)
226
227
# Copy from Azure with encryption
228
azure_encrypted = (AzureContainer('myaccount', 'secure-data')
229
.credentials(azure_sas_token='sas-token-here')
230
.encryption_azure_cse('azure-master-key')
231
)
232
233
copy_from_azure = CopyIntoStorage(
234
table=customer_table,
235
stage_location=azure_container,
236
file_format=json_format
237
)
238
```
239
240
### External Stage Usage
241
242
```python
243
from snowflake.sqlalchemy import ExternalStage
244
245
# Use existing external stage
246
external_stage = ExternalStage('MY_EXTERNAL_STAGE', 'monthly_data/')
247
248
# Create from parent stage
249
child_stage = ExternalStage.from_parent_stage(
250
parent_stage='PARENT_STAGE',
251
path='2024/01/',
252
file_format='CSV_FORMAT'
253
)
254
255
# Copy from external stage
256
copy_from_stage = CopyIntoStorage(
257
table=monthly_reports,
258
stage_location=external_stage
259
).pattern('.*report.*\.csv')
260
```
261
262
### Stage Creation with Cloud Storage
263
264
```python
265
from snowflake.sqlalchemy import CreateStage
266
267
# Create AWS stage
268
create_aws_stage = CreateStage(
269
name='AWS_DATA_STAGE',
270
url='s3://my-bucket/data/',
271
credentials={
272
'aws_key_id': 'AKIAIOSFODNN7EXAMPLE',
273
'aws_secret_key': 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY'
274
},
275
encryption={
276
'type': 'AWS_SSE_KMS',
277
'kms_key_id': 'arn:aws:kms:us-east-1:123456789012:key/12345678-1234-1234-1234-123456789012'
278
}
279
)
280
281
# Create Azure stage
282
create_azure_stage = CreateStage(
283
name='AZURE_DATA_STAGE',
284
url='azure://myaccount.blob.core.windows.net/data/',
285
credentials={
286
'azure_sas_token': 'sp=r&st=2023-01-01T00:00:00Z&se=2024-01-01T00:00:00Z&spr=https&sv=2022-11-02&sr=c&sig=signature'
287
}
288
)
289
290
engine.execute(create_aws_stage)
291
engine.execute(create_azure_stage)
292
```
293
294
### Comprehensive Cloud Storage Workflow
295
296
```python
297
from snowflake.sqlalchemy import (
298
AWSBucket, CSVFormatter, CopyIntoStorage, CreateFileFormat
299
)
300
301
# 1. Create file format
302
csv_format = CreateFileFormat(
303
name='CLOUD_CSV_FORMAT',
304
format_type='CSV',
305
field_delimiter=',',
306
skip_header=1,
307
compression='GZIP'
308
)
309
310
# 2. Set up S3 bucket with encryption
311
s3_location = (AWSBucket('data-lake-bucket', 'processed-data/')
312
.credentials(aws_role='arn:aws:iam::123456789012:role/SnowflakeDataRole')
313
.encryption_aws_sse_s3()
314
)
315
316
# 3. Copy data from S3
317
copy_operation = (CopyIntoStorage(
318
table=target_table,
319
stage_location=s3_location,
320
file_format='CLOUD_CSV_FORMAT'
321
)
322
.files(['data_2024_01.csv.gz', 'data_2024_02.csv.gz'])
323
.force(False)
324
)
325
326
# Execute operations
327
engine.execute(csv_format)
328
engine.execute(copy_operation)
329
```