| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include "s3.h" |
| 6 |
|
| 7 |
#include "auth.h" |
| 8 |
#include "io.h" |
| 9 |
|
| 10 |
#include <aws/auth/credentials.h> |
| 11 |
#include <aws/common/cross_process_lock.h> |
| 12 |
#include <aws/common/hash_table.h> |
| 13 |
#include <aws/common/string.h> |
| 14 |
#include <aws/s3/s3_client.h> |
| 15 |
|
| 16 |
static const char *s_capsule_name_s3_client = "aws_s3_client"; |
| 17 |
static const char *s_capsule_name_s3_instance_lock = "aws_cross_process_lock"; |
| 18 |
|
| 19 |
PyObject *aws_py_s3_get_ec2_instance_type(PyObject *self, PyObject *args) { |
| 20 |
(void)self; |
| 21 |
(void)args; |
| 22 |
|
| 23 |
const struct aws_s3_platform_info *platform_info = aws_s3_get_current_platform_info(); |
| 24 |
|
| 25 |
if (platform_info->instance_type.len) { |
| 26 |
PyObject *ret_value = PyUnicode_FromAwsByteCursor(&platform_info->instance_type); |
| 27 |
return ret_value; |
| 28 |
} |
| 29 |
|
| 30 |
Py_RETURN_NONE; |
| 31 |
} |
| 32 |
|
| 33 |
PyObject *aws_py_s3_is_crt_s3_optimized_for_system(PyObject *self, PyObject *args) { |
| 34 |
(void)self; |
| 35 |
(void)args; |
| 36 |
|
| 37 |
const struct aws_s3_platform_info *platform_info = aws_s3_get_current_platform_info(); |
| 38 |
|
| 39 |
if (platform_info->has_recommended_configuration) { |
| 40 |
Py_RETURN_TRUE; |
| 41 |
} |
| 42 |
|
| 43 |
Py_RETURN_FALSE; |
| 44 |
} |
| 45 |
|
| 46 |
PyObject *aws_py_s3_get_recommended_throughput_target_gbps(PyObject *self, PyObject *args) { |
| 47 |
(void)self; |
| 48 |
(void)args; |
| 49 |
|
| 50 |
const struct aws_s3_platform_info *platform_info = aws_s3_get_current_platform_info(); |
| 51 |
|
| 52 |
return PyFloat_FromDouble(platform_info->max_throughput_gbps); |
| 53 |
} |
| 54 |
|
| 55 |
PyObject *aws_py_s3_get_optimized_platforms(PyObject *self, PyObject *args) { |
| 56 |
(void)self; |
| 57 |
(void)args; |
| 58 |
|
| 59 |
bool success = false; |
| 60 |
struct aws_array_list platform_list = aws_s3_get_platforms_with_recommended_config(); |
| 61 |
|
| 62 |
size_t list_length = aws_array_list_length(&platform_list); |
| 63 |
|
| 64 |
PyObject *py_list = PyList_New(list_length); |
| 65 |
if (!py_list) { |
| 66 |
goto clean_up; |
| 67 |
} |
| 68 |
|
| 69 |
for (size_t i = 0; i < list_length; ++i) { |
| 70 |
struct aws_byte_cursor cursor; |
| 71 |
if (aws_array_list_get_at(&platform_list, &cursor, i) == AWS_OP_SUCCESS) { |
| 72 |
PyObject *platform_str = PyUnicode_FromAwsByteCursor(&cursor); |
| 73 |
if (!platform_str) { |
| 74 |
Py_DECREF(py_list); |
| 75 |
goto clean_up; |
| 76 |
} |
| 77 |
PyList_SetItem(py_list, i, platform_str); |
| 78 |
} |
| 79 |
} |
| 80 |
success = true; |
| 81 |
|
| 82 |
clean_up: |
| 83 |
aws_array_list_clean_up(&platform_list); |
| 84 |
return success ? py_list : NULL; |
| 85 |
} |
| 86 |
|
| 87 |
struct cross_process_lock_binding { |
| 88 |
struct aws_cross_process_lock *lock; |
| 89 |
struct aws_string *name; |
| 90 |
}; |
| 91 |
|
| 92 |
|
| 93 |
static void s_s3_cross_process_lock_destructor(PyObject *capsule) { |
| 94 |
struct cross_process_lock_binding *lock_binding = PyCapsule_GetPointer(capsule, s_capsule_name_s3_instance_lock); |
| 95 |
|
| 96 |
if (lock_binding->lock) { |
| 97 |
aws_cross_process_lock_release(lock_binding->lock); |
| 98 |
lock_binding->lock = NULL; |
| 99 |
} |
| 100 |
|
| 101 |
if (lock_binding->name) { |
| 102 |
aws_string_destroy(lock_binding->name); |
| 103 |
} |
| 104 |
|
| 105 |
aws_mem_release(aws_py_get_allocator(), lock_binding); |
| 106 |
} |
| 107 |
|
| 108 |
PyObject *aws_py_s3_cross_process_lock_new(PyObject *self, PyObject *args) { |
| 109 |
(void)self; |
| 110 |
|
| 111 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 112 |
|
| 113 |
struct aws_byte_cursor lock_name; |
| 114 |
|
| 115 |
if (!PyArg_ParseTuple(args, "s#", &lock_name.ptr, &lock_name.len)) { |
| 116 |
return NULL; |
| 117 |
} |
| 118 |
|
| 119 |
struct cross_process_lock_binding *binding = |
| 120 |
aws_mem_calloc(allocator, 1, sizeof(struct cross_process_lock_binding)); |
| 121 |
binding->name = aws_string_new_from_cursor(allocator, &lock_name); |
| 122 |
|
| 123 |
PyObject *capsule = PyCapsule_New(binding, s_capsule_name_s3_instance_lock, s_s3_cross_process_lock_destructor); |
| 124 |
if (!capsule) { |
| 125 |
aws_string_destroy(binding->name); |
| 126 |
aws_mem_release(allocator, binding); |
| 127 |
return PyErr_AwsLastError(); |
| 128 |
} |
| 129 |
|
| 130 |
return capsule; |
| 131 |
} |
| 132 |
|
| 133 |
PyObject *aws_py_s3_cross_process_lock_acquire(PyObject *self, PyObject *args) { |
| 134 |
(void)self; |
| 135 |
|
| 136 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 137 |
|
| 138 |
PyObject *lock_capsule; |
| 139 |
|
| 140 |
if (!PyArg_ParseTuple(args, "O", &lock_capsule)) { |
| 141 |
return NULL; |
| 142 |
} |
| 143 |
|
| 144 |
struct cross_process_lock_binding *lock_binding = |
| 145 |
PyCapsule_GetPointer(lock_capsule, s_capsule_name_s3_instance_lock); |
| 146 |
if (!lock_binding) { |
| 147 |
return NULL; |
| 148 |
} |
| 149 |
|
| 150 |
if (!lock_binding->lock) { |
| 151 |
struct aws_cross_process_lock *lock = |
| 152 |
aws_cross_process_lock_try_acquire(allocator, aws_byte_cursor_from_string(lock_binding->name)); |
| 153 |
|
| 154 |
if (!lock) { |
| 155 |
return PyErr_AwsLastError(); |
| 156 |
} |
| 157 |
lock_binding->lock = lock; |
| 158 |
} |
| 159 |
|
| 160 |
Py_RETURN_NONE; |
| 161 |
} |
| 162 |
|
| 163 |
PyObject *aws_py_s3_cross_process_lock_release(PyObject *self, PyObject *args) { |
| 164 |
(void)self; |
| 165 |
(void)args; |
| 166 |
|
| 167 |
PyObject *lock_capsule; |
| 168 |
|
| 169 |
if (!PyArg_ParseTuple(args, "O", &lock_capsule)) { |
| 170 |
return NULL; |
| 171 |
} |
| 172 |
|
| 173 |
struct cross_process_lock_binding *lock_binding = |
| 174 |
PyCapsule_GetPointer(lock_capsule, s_capsule_name_s3_instance_lock); |
| 175 |
if (!lock_binding) { |
| 176 |
return NULL; |
| 177 |
} |
| 178 |
|
| 179 |
if (lock_binding->lock) { |
| 180 |
aws_cross_process_lock_release(lock_binding->lock); |
| 181 |
lock_binding->lock = NULL; |
| 182 |
} |
| 183 |
|
| 184 |
Py_RETURN_NONE; |
| 185 |
} |
| 186 |
|
| 187 |
struct s3_client_binding { |
| 188 |
struct aws_s3_client *native; |
| 189 |
|
| 190 |
|
| 191 |
PyObject *on_shutdown; |
| 192 |
|
| 193 |
PyObject *py_core; |
| 194 |
}; |
| 195 |
|
| 196 |
static void s_destroy(struct s3_client_binding *client) { |
| 197 |
Py_XDECREF(client->on_shutdown); |
| 198 |
Py_XDECREF(client->py_core); |
| 199 |
aws_mem_release(aws_py_get_allocator(), client); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
static void s_s3_client_capsule_destructor(PyObject *capsule) { |
| 204 |
struct s3_client_binding *client = PyCapsule_GetPointer(capsule, s_capsule_name_s3_client); |
| 205 |
|
| 206 |
if (client->native) { |
| 207 |
aws_s3_client_release(client->native); |
| 208 |
} else { |
| 209 |
|
| 210 |
|
| 211 |
s_destroy(client); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
static void s_s3_client_shutdown(void *user_data) { |
| 217 |
struct s3_client_binding *client = user_data; |
| 218 |
|
| 219 |
|
| 220 |
PyGILState_STATE state; |
| 221 |
if (aws_py_gilstate_ensure(&state)) { |
| 222 |
return; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
PyObject *result = PyObject_CallFunction(client->on_shutdown, NULL); |
| 227 |
if (result) { |
| 228 |
Py_DECREF(result); |
| 229 |
} else { |
| 230 |
|
| 231 |
PyErr_WriteUnraisable(PyErr_Occurred()); |
| 232 |
} |
| 233 |
|
| 234 |
s_destroy(client); |
| 235 |
|
| 236 |
PyGILState_Release(state); |
| 237 |
} |
| 238 |
|
| 239 |
struct aws_s3_client *aws_py_get_s3_client(PyObject *client) { |
| 240 |
AWS_PY_RETURN_NATIVE_FROM_BINDING(client, s_capsule_name_s3_client, "S3Client", s3_client_binding); |
| 241 |
} |
| 242 |
|
| 243 |
PyObject *aws_py_s3_client_new(PyObject *self, PyObject *args) { |
| 244 |
(void)self; |
| 245 |
|
| 246 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 247 |
|
| 248 |
PyObject *bootstrap_py; |
| 249 |
PyObject *signing_config_py; |
| 250 |
PyObject *credential_provider_py; |
| 251 |
PyObject *tls_options_py; |
| 252 |
PyObject *on_shutdown_py; |
| 253 |
struct aws_byte_cursor region; |
| 254 |
int tls_mode; |
| 255 |
uint64_t part_size; |
| 256 |
uint64_t multipart_upload_threshold; |
| 257 |
double throughput_target_gbps; |
| 258 |
int enable_s3express; |
| 259 |
uint64_t mem_limit; |
| 260 |
PyObject *network_interface_names_py; |
| 261 |
int fio_options_set; |
| 262 |
int should_stream; |
| 263 |
double disk_throughput_gbps; |
| 264 |
int direct_io; |
| 265 |
uint64_t max_active_connections_override; |
| 266 |
PyObject *py_core; |
| 267 |
|
| 268 |
if (!PyArg_ParseTuple( |
| 269 |
args, |
| 270 |
"OOOOOs#iKKdpKOppdpKO", |
| 271 |
&bootstrap_py, |
| 272 |
&signing_config_py, |
| 273 |
&credential_provider_py, |
| 274 |
&tls_options_py, |
| 275 |
&on_shutdown_py, |
| 276 |
®ion.ptr, |
| 277 |
®ion.len, |
| 278 |
&tls_mode, |
| 279 |
&part_size, |
| 280 |
&multipart_upload_threshold, |
| 281 |
&throughput_target_gbps, |
| 282 |
&enable_s3express, |
| 283 |
&mem_limit, |
| 284 |
&network_interface_names_py, |
| 285 |
&fio_options_set, |
| 286 |
&should_stream, |
| 287 |
&disk_throughput_gbps, |
| 288 |
&direct_io, |
| 289 |
&max_active_connections_override, |
| 290 |
&py_core)) { |
| 291 |
return NULL; |
| 292 |
} |
| 293 |
|
| 294 |
struct aws_client_bootstrap *bootstrap = aws_py_get_client_bootstrap(bootstrap_py); |
| 295 |
if (!bootstrap) { |
| 296 |
return NULL; |
| 297 |
} |
| 298 |
|
| 299 |
struct aws_credentials_provider *credential_provider = NULL; |
| 300 |
if (credential_provider_py != Py_None) { |
| 301 |
credential_provider = aws_py_get_credentials_provider(credential_provider_py); |
| 302 |
if (!credential_provider) { |
| 303 |
return NULL; |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
struct aws_tls_connection_options *tls_options = NULL; |
| 308 |
if (tls_options_py != Py_None) { |
| 309 |
tls_options = aws_py_get_tls_connection_options(tls_options_py); |
| 310 |
if (!tls_options) { |
| 311 |
return NULL; |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
struct aws_signing_config_aws default_signing_config; |
| 316 |
AWS_ZERO_STRUCT(default_signing_config); |
| 317 |
|
| 318 |
struct aws_signing_config_aws *signing_config = NULL; |
| 319 |
struct aws_credentials *anonymous_credentials = NULL; |
| 320 |
struct aws_byte_cursor *network_interface_names = NULL; |
| 321 |
size_t num_network_interface_names = 0; |
| 322 |
PyObject *capsule = NULL; |
| 323 |
|
| 324 |
bool success = false; |
| 325 |
|
| 326 |
if (signing_config_py != Py_None) { |
| 327 |
signing_config = aws_py_get_signing_config(signing_config_py); |
| 328 |
if (!signing_config) { |
| 329 |
goto cleanup; |
| 330 |
} |
| 331 |
} else if (credential_provider) { |
| 332 |
aws_s3_init_default_signing_config(&default_signing_config, region, credential_provider); |
| 333 |
signing_config = &default_signing_config; |
| 334 |
} else { |
| 335 |
|
| 336 |
anonymous_credentials = aws_credentials_new_anonymous(allocator); |
| 337 |
default_signing_config.credentials = anonymous_credentials; |
| 338 |
signing_config = &default_signing_config; |
| 339 |
} |
| 340 |
|
| 341 |
struct s3_client_binding *s3_client = aws_mem_calloc(allocator, 1, sizeof(struct s3_client_binding)); |
| 342 |
|
| 343 |
capsule = PyCapsule_New(s3_client, s_capsule_name_s3_client, s_s3_client_capsule_destructor); |
| 344 |
if (!capsule) { |
| 345 |
aws_mem_release(allocator, s3_client); |
| 346 |
goto cleanup; |
| 347 |
} |
| 348 |
|
| 349 |
s3_client->on_shutdown = on_shutdown_py; |
| 350 |
Py_INCREF(s3_client->on_shutdown); |
| 351 |
|
| 352 |
s3_client->py_core = py_core; |
| 353 |
Py_INCREF(s3_client->py_core); |
| 354 |
|
| 355 |
if (network_interface_names_py != Py_None) { |
| 356 |
if (!PyList_Check(network_interface_names_py)) { |
| 357 |
PyErr_SetString(PyExc_TypeError, "Expected network_interface_names to be a sequence."); |
| 358 |
goto cleanup; |
| 359 |
} |
| 360 |
Py_ssize_t list_size = PyList_Size(network_interface_names_py); |
| 361 |
if (list_size < 0) { |
| 362 |
goto cleanup; |
| 363 |
} |
| 364 |
num_network_interface_names = (size_t)list_size; |
| 365 |
network_interface_names = |
| 366 |
aws_mem_calloc(allocator, num_network_interface_names, sizeof(struct aws_byte_cursor)); |
| 367 |
for (size_t i = 0; i < num_network_interface_names; ++i) { |
| 368 |
bool strong_ref = false; |
| 369 |
#ifdef Py_GIL_DISABLED |
| 370 |
PyObject *str_obj = PyList_GetItemRef(network_interface_names_py, i); |
| 371 |
strong_ref = true; |
| 372 |
#else |
| 373 |
PyObject *str_obj = PyList_GetItem(network_interface_names_py, i); |
| 374 |
#endif |
| 375 |
if (!str_obj) { |
| 376 |
goto cleanup; |
| 377 |
} |
| 378 |
network_interface_names[i] = aws_byte_cursor_from_pyunicode(str_obj); |
| 379 |
if (network_interface_names[i].ptr == NULL) { |
| 380 |
if (strong_ref) { |
| 381 |
Py_DECREF(str_obj); |
| 382 |
} |
| 383 |
PyErr_SetString(PyExc_TypeError, "Expected all network_interface_names elements to be strings."); |
| 384 |
goto cleanup; |
| 385 |
} |
| 386 |
if (strong_ref) { |
| 387 |
Py_DECREF(str_obj); |
| 388 |
} |
| 389 |
} |
| 390 |
} |
| 391 |
struct aws_s3_file_io_options fio_opts = { |
| 392 |
.should_stream = should_stream, |
| 393 |
.disk_throughput_gbps = disk_throughput_gbps, |
| 394 |
.direct_io = direct_io, |
| 395 |
}; |
| 396 |
|
| 397 |
struct aws_s3_client_config s3_config = { |
| 398 |
.region = region, |
| 399 |
.client_bootstrap = bootstrap, |
| 400 |
.tls_mode = tls_mode, |
| 401 |
.signing_config = signing_config, |
| 402 |
.part_size = part_size, |
| 403 |
.memory_limit_in_bytes = mem_limit, |
| 404 |
.multipart_upload_threshold = multipart_upload_threshold, |
| 405 |
.tls_connection_options = tls_options, |
| 406 |
.throughput_target_gbps = throughput_target_gbps, |
| 407 |
.shutdown_callback = s_s3_client_shutdown, |
| 408 |
.shutdown_callback_user_data = s3_client, |
| 409 |
.enable_s3express = enable_s3express, |
| 410 |
.network_interface_names_array = network_interface_names, |
| 411 |
.num_network_interface_names = num_network_interface_names, |
| 412 |
|
| 413 |
.fio_opts = fio_options_set ? &fio_opts : NULL, |
| 414 |
.max_active_connections_override = max_active_connections_override, |
| 415 |
}; |
| 416 |
|
| 417 |
s3_client->native = aws_s3_client_new(allocator, &s3_config); |
| 418 |
if (s3_client->native == NULL) { |
| 419 |
PyErr_SetAwsLastError(); |
| 420 |
goto cleanup; |
| 421 |
} |
| 422 |
success = true; |
| 423 |
|
| 424 |
cleanup: |
| 425 |
aws_credentials_release(anonymous_credentials); |
| 426 |
aws_mem_release(allocator, network_interface_names); |
| 427 |
if (!success) { |
| 428 |
Py_XDECREF(capsule); |
| 429 |
return NULL; |
| 430 |
} |
| 431 |
return capsule; |
| 432 |
} |