| 1 |
Functions |
| 2 |
========= |
| 3 |
|
| 4 |
ms3_library_init() |
| 5 |
------------------ |
| 6 |
|
| 7 |
.. c:function:: void ms3_library_init(void) |
| 8 |
|
| 9 |
Initializes the library for use. |
| 10 |
Should be called before any threads are spawned. |
| 11 |
|
| 12 |
ms3_library_deinit() |
| 13 |
-------------------- |
| 14 |
|
| 15 |
.. c:function:: void ms3_library_deinit(void) |
| 16 |
|
| 17 |
Cleans up the library, typically for the end of the application's execution. |
| 18 |
|
| 19 |
ms3_library_init_malloc() |
| 20 |
------------------------- |
| 21 |
|
| 22 |
.. c:function:: uint8_t ms3_library_init_malloc(ms3_malloc_callback m, ms3_free_callback f, ms3_realloc_callback r, ms3_strdup_callback s, ms3_calloc_callback c) |
| 23 |
|
| 24 |
Initialize the library for use with custom allocator replacement functions. These functions are also fed into libcurl. The function prototypes should be as follows: |
| 25 |
|
| 26 |
.. c:function:: void *ms3_malloc_callback(size_t size) |
| 27 |
|
| 28 |
To replace ``malloc()``. |
| 29 |
|
| 30 |
.. c:function:: void ms3_free_callback(void *ptr) |
| 31 |
|
| 32 |
To replace ``free()``. |
| 33 |
|
| 34 |
.. c:function:: void *ms3_realloc_callback(void *ptr, size_t size) |
| 35 |
|
| 36 |
To replace ``realloc()``. |
| 37 |
|
| 38 |
.. c:function:: char *ms3_strdup_callback(const char *str) |
| 39 |
|
| 40 |
To replace ``strdup()``. |
| 41 |
|
| 42 |
.. c:function:: void *ms3_calloc_callback(size_t nmemb, size_t size) |
| 43 |
|
| 44 |
To replace ``calloc()``. |
| 45 |
|
| 46 |
Should be called before any threads are spawned. All parameters are required or the function *will* fail. |
| 47 |
|
| 48 |
Remember: With great power comes great responsibility. |
| 49 |
|
| 50 |
:param m: The malloc callback |
| 51 |
:param f: The free callback |
| 52 |
:param r: The realloc callback |
| 53 |
:param s: The strdup callback |
| 54 |
:param c: The calloc callback |
| 55 |
:returns: ``0`` on success, ``MS3_ERR_PARAMETER`` if a parameter is ``NULL`` |
| 56 |
|
| 57 |
ms3_init() |
| 58 |
---------- |
| 59 |
|
| 60 |
.. c:function:: ms3_st *ms3_init(const char *s3key, const char *s3secret, const char *region, const char *base_domain) |
| 61 |
|
| 62 |
Initializes a :c:type:`ms3_st` object. This object should only be used in |
| 63 |
the thread that created it because it reuses connections. But it is safe to |
| 64 |
have other :c:type:`ms3_st` objects running at the same time in other threads. |
| 65 |
|
| 66 |
.. note:: |
| 67 |
You *MUST* call :c:func:`ms3_library_init` before |
| 68 |
spawning threads when using this access method. |
| 69 |
|
| 70 |
:param s3key: The AWS access key |
| 71 |
:param s3secret: The AWS secret key |
| 72 |
:param region: The AWS region to use (such as ``us-east-1``) |
| 73 |
:param base_domain: A domain name to use if AWS S3 is not the desired server (set to ``NULL`` for S3) |
| 74 |
:returns: A newly allocated marias3 object |
| 75 |
|
| 76 |
ms3_deinit() |
| 77 |
------------ |
| 78 |
|
| 79 |
.. c:function:: void ms3_deinit(ms3_st *ms3) |
| 80 |
|
| 81 |
Cleans up and frees a :c:type:`ms3_st` object. |
| 82 |
|
| 83 |
:param ms3: The marias3 object |
| 84 |
|
| 85 |
ms3_server_error() |
| 86 |
------------------ |
| 87 |
|
| 88 |
.. c:function:: const char *ms3_server_error(ms3_st *ms3) |
| 89 |
|
| 90 |
Returns the last error message from the S3 server or underlying Curl library. |
| 91 |
|
| 92 |
:param ms3: The marias3 object |
| 93 |
:returns: The error message string or ``NULL`` if there is no message. |
| 94 |
|
| 95 |
ms3_error() |
| 96 |
----------- |
| 97 |
|
| 98 |
.. c:function:: const char *ms3_error(uint8_t errcode) |
| 99 |
|
| 100 |
Returns an error message for a given error code |
| 101 |
|
| 102 |
:param errcode: The error code to translate |
| 103 |
:returns: The error message |
| 104 |
|
| 105 |
ms3_debug() |
| 106 |
----------- |
| 107 |
|
| 108 |
.. c:function:: void ms3_debug(int debug_state) |
| 109 |
|
| 110 |
Enables and disables debugging output on stderr. |
| 111 |
|
| 112 |
:param debug_state: Set to 1 to enable debugging, zero to disable. |
| 113 |
|
| 114 |
Note:: |
| 115 |
This enables/disables globally for the library |
| 116 |
|
| 117 |
ms3_list() |
| 118 |
---------- |
| 119 |
|
| 120 |
.. c:function:: uint8_t ms3_list(ms3_st *ms3, const char *bucket, const char *prefix, ms3_list_st **list) |
| 121 |
|
| 122 |
Retrieves a list of files from a given S3 bucket and fills it into a :c:type:`ms3_list_st`. |
| 123 |
|
| 124 |
The list generated is the eqivilent of a recursive directory listing but only has files in it, no entries for directories. |
| 125 |
|
| 126 |
The list will automatically be freed on the next list/list_dir call or :c:func:`ms3_deinit` |
| 127 |
|
| 128 |
:param ms3: The marias3 object |
| 129 |
:param bucket: The bucket name to use |
| 130 |
:param prefix: An optional path/file prefix to use (``NULL`` for all files) |
| 131 |
:param list: A pointer to a pointer that will contain the returned list |
| 132 |
:returns: ``0`` on success, a positive integer on failure |
| 133 |
|
| 134 |
Example |
| 135 |
^^^^^^^ |
| 136 |
|
| 137 |
.. code-block:: c |
| 138 |
|
| 139 |
char *s3key= getenv("S3KEY"); |
| 140 |
char *s3secret= getenv("S3SECRET"); |
| 141 |
char *s3region= getenv("S3REGION"); |
| 142 |
char *s3bucket= getenv("S3BUCKET"); |
| 143 |
ms3_list_st *list= NULL, *list_it= NULL; |
| 144 |
uint8_t res; |
| 145 |
|
| 146 |
ms3_library_init(); |
| 147 |
ms3_st *ms3= ms3_thread_init(s3key, s3secret, s3region, NULL); |
| 148 |
|
| 149 |
res= ms3_list(ms3, s3bucket, NULL, &list); |
| 150 |
if (res) |
| 151 |
{ |
| 152 |
printf("Error occurred: %d\n", res); |
| 153 |
return; |
| 154 |
} |
| 155 |
list_it= list; |
| 156 |
while(list_it) |
| 157 |
{ |
| 158 |
printf("File: %s, size: %ld, tstamp: %ld\n", list_it->key, list_it->length, list_it->created); |
| 159 |
list_it= list_it->next; |
| 160 |
} |
| 161 |
ms3_deinit(ms3); |
| 162 |
|
| 163 |
ms3_list_dir() |
| 164 |
-------------- |
| 165 |
|
| 166 |
.. c:function:: uint8_t ms3_list_dir(ms3_st *ms3, const char *bucket, const char *prefix, ms3_list_st **list) |
| 167 |
|
| 168 |
Retrieves a list of files from a given S3 bucket and fills it into a :c:type:`ms3_list_st`. |
| 169 |
|
| 170 |
The list generated will automatically add the delimiter ``/`` and therefore filter up to the first ``/`` after the prefix. Unlike :c:func:`ms3_list` it includes directory entries. This is the eqivilent of doing a regular directory listing in a current directory (as designated by ``prefix``). |
| 171 |
|
| 172 |
The list will automatically be freed on the next list/list_dir call or :c:func:`ms3_deinit` |
| 173 |
|
| 174 |
:param ms3: The marias3 object |
| 175 |
:param bucket: The bucket name to use |
| 176 |
:param prefix: An optional path/file prefix to use (``NULL`` for all files) |
| 177 |
:param list: A pointer to a pointer that will contain the returned list |
| 178 |
:returns: ``0`` on success, a positive integer on failure |
| 179 |
|
| 180 |
|
| 181 |
ms3_list_free() |
| 182 |
--------------- |
| 183 |
|
| 184 |
.. c:function:: void ms3_list_free(ms3_list_st *list) |
| 185 |
|
| 186 |
.. deprecated:: 3.1.1 |
| 187 |
Now a NULL operation which be removed in 4.0 |
| 188 |
|
| 189 |
A NULL operation, previously free'd :c:func:`ms3_list`, but this is now done internally on :c:func:`ms3_deinit` or when a new list is requested. |
| 190 |
|
| 191 |
:param list: The list to free |
| 192 |
|
| 193 |
ms3_put() |
| 194 |
--------- |
| 195 |
|
| 196 |
.. c:function:: uint8_t ms3_put(ms3_st *ms3, const char *bucket, const char *key, const uint8_t *data, size_t length) |
| 197 |
|
| 198 |
Puts a binary data from a given pointer into S3 at a given key/filename. If an existing key/file exists with the same name this will be overwritten. |
| 199 |
|
| 200 |
:param ms3: The marias3 object |
| 201 |
:param bucket: The bucket name to use |
| 202 |
:param key: The key/filename to create/overwrite |
| 203 |
:param data: A pointer to the data to write |
| 204 |
:param length: The length of the data to write |
| 205 |
:returns: ``0`` on success, a positive integer on failure |
| 206 |
|
| 207 |
Example |
| 208 |
^^^^^^^ |
| 209 |
|
| 210 |
.. code-block:: c |
| 211 |
|
| 212 |
char *s3key= getenv("S3KEY"); |
| 213 |
char *s3secret= getenv("S3SECRET"); |
| 214 |
char *s3region= getenv("S3REGION"); |
| 215 |
char *s3bucket= getenv("S3BUCKET"); |
| 216 |
uint8_t res; |
| 217 |
const char *test_string= "Another one bites the dust"; |
| 218 |
|
| 219 |
ms3_library_init(); |
| 220 |
ms3_st *ms3= ms3_thread_init(s3key, s3secret, s3region, NULL); |
| 221 |
|
| 222 |
res= ms3_put(ms3, s3bucket, "test/ms3.txt", (const uint8_t*)test_string, strlen(test_string)); |
| 223 |
if (res) |
| 224 |
{ |
| 225 |
printf("Error occurred: %d\n", res); |
| 226 |
return; |
| 227 |
} |
| 228 |
ms3_deinit(ms3); |
| 229 |
|
| 230 |
|
| 231 |
ms3_copy() |
| 232 |
---------- |
| 233 |
|
| 234 |
.. c:function:: uint8_t ms3_copy(ms3_st *ms3, const char *source_bucket, const char *source_key, const char *dest_bucket, const char *dest_key) |
| 235 |
|
| 236 |
S3 internally copies an object from a source bucket and key to a destination bucket and key. |
| 237 |
|
| 238 |
:param ms3: The marias3 object |
| 239 |
:param source_bucket: The bucket where the source object is |
| 240 |
:param source_key: The key/filename of the source object |
| 241 |
:param dest_bucket: The destination bucket (can be the same as source) |
| 242 |
:param dest_key: The destination key/filename |
| 243 |
:returns: ``0`` on success, a positive integer on failure |
| 244 |
|
| 245 |
ms3_move() |
| 246 |
---------- |
| 247 |
|
| 248 |
.. c:function:: uint8_t ms3_move(ms3_st *ms3, const char *source_bucket, const char *source_key, const char *dest_bucket, const char *dest_key) |
| 249 |
|
| 250 |
Moves an object from source to destination. Internally the library performs a copy and if successful performs a delete on the source object. |
| 251 |
|
| 252 |
:param ms3: The marias3 object |
| 253 |
:param source_bucket: The bucket where the source object is |
| 254 |
:param source_key: The key/filename of the source object |
| 255 |
:param dest_bucket: The destination bucket (can be the same as source) |
| 256 |
:param dest_key: The destination key/filename |
| 257 |
:returns: ``0`` on success, a positive integer on failure |
| 258 |
|
| 259 |
ms3_get() |
| 260 |
--------- |
| 261 |
|
| 262 |
.. c:function:: uint8_t ms3_get(ms3_st *ms3, const char *bucket, const char *key, uint8_t **data, size_t *length) |
| 263 |
|
| 264 |
Retrieves a given object from S3. |
| 265 |
|
| 266 |
.. Note:: |
| 267 |
The application is expected to free the resulting data pointer after use |
| 268 |
|
| 269 |
:param ms3: The marias3 object |
| 270 |
:param bucket: The bucket name to use |
| 271 |
:param key: The key/filename to retrieve |
| 272 |
:param data: A pointer to a pointer the data to be retrieved into |
| 273 |
:param length: A pointer to the data length |
| 274 |
:returns: ``0`` on success, a positive integer on failure |
| 275 |
|
| 276 |
Example |
| 277 |
^^^^^^^ |
| 278 |
|
| 279 |
.. code-block:: c |
| 280 |
|
| 281 |
char *s3key= getenv("S3KEY"); |
| 282 |
char *s3secret= getenv("S3SECRET"); |
| 283 |
char *s3region= getenv("S3REGION"); |
| 284 |
char *s3bucket= getenv("S3BUCKET"); |
| 285 |
uint8_t res; |
| 286 |
uint8_t *data= NULL; |
| 287 |
size_t length; |
| 288 |
|
| 289 |
ms3_library_init(); |
| 290 |
ms3_st *ms3= ms3_thread_init(s3key, s3secret, s3region, NULL); |
| 291 |
|
| 292 |
res= ms3_get(ms3, s3bucket, "test/ms3.txt", &data, &length); |
| 293 |
if (res) |
| 294 |
{ |
| 295 |
printf("Error occurred: %d\n", res); |
| 296 |
return; |
| 297 |
} |
| 298 |
printf("File contents: %s\n", data); |
| 299 |
printf("File length: %ld\n", length); |
| 300 |
ms3_free(data); |
| 301 |
ms3_deinit(ms3); |
| 302 |
|
| 303 |
ms3_free() |
| 304 |
---------- |
| 305 |
|
| 306 |
.. c:function:: void ms3_free(uint8_t *data) |
| 307 |
|
| 308 |
Used to free the data allocated by :c:func:`ms3_get`. |
| 309 |
|
| 310 |
:param data: The data to free |
| 311 |
|
| 312 |
ms3_set_option() |
| 313 |
---------------- |
| 314 |
|
| 315 |
.. c:function:: uint8_t ms3_set_option(ms3_st *ms3, ms3_set_option_t option, void *value) |
| 316 |
|
| 317 |
Sets a given connection option. See :c:type:`ms3_set_option_t` for a list of options. |
| 318 |
|
| 319 |
:param ms3: The marias3 object |
| 320 |
:param option: The option to set |
| 321 |
:param value: A pointer to the value for the option (if required, ``NULL`` if not) |
| 322 |
:returns: ``0`` on success, a positive integer on failure |
| 323 |
|
| 324 |
ms3_delete() |
| 325 |
------------ |
| 326 |
|
| 327 |
.. c:function:: uint8_t ms3_delete(ms3_st *ms3, const char *bucket, const char *key) |
| 328 |
|
| 329 |
Deletes an object from an S3 bucket |
| 330 |
|
| 331 |
:param ms3: The marias3 object |
| 332 |
:param bucket: The bucket name to use |
| 333 |
:param key: The key/filename to delete |
| 334 |
:returns: ``0`` on success, a positive integer on failure |
| 335 |
|
| 336 |
Example |
| 337 |
^^^^^^^ |
| 338 |
|
| 339 |
.. code-block:: c |
| 340 |
|
| 341 |
char *s3key= getenv("S3KEY"); |
| 342 |
char *s3secret= getenv("S3SECRET"); |
| 343 |
char *s3region= getenv("S3REGION"); |
| 344 |
char *s3bucket= getenv("S3BUCKET"); |
| 345 |
uint8_t res; |
| 346 |
|
| 347 |
ms3_library_init(); |
| 348 |
ms3_st *ms3= ms3_thread_init(s3key, s3secret, s3region, NULL); |
| 349 |
|
| 350 |
res = ms3_delete(ms3, s3bucket, "test/ms3.txt"); |
| 351 |
if (res) |
| 352 |
{ |
| 353 |
printf("Error occurred: %d\n", res); |
| 354 |
return; |
| 355 |
} |
| 356 |
ms3_deinit(ms3); |
| 357 |
|
| 358 |
ms3_status() |
| 359 |
------------ |
| 360 |
|
| 361 |
.. c:function:: uint8_t ms3_status(ms3_st *ms3, const char *bucket, const char *key, ms3_status_st *status) |
| 362 |
|
| 363 |
Retreives the status of a given filename/key into a :c:type:`ms3_status_st` object. Will return an error if not found. |
| 364 |
|
| 365 |
:param ms3: The marias3 object |
| 366 |
:param bucket: The bucket name to use |
| 367 |
:param key: The key/filename to status check |
| 368 |
:param status: A status object to fill |
| 369 |
:returns: ``0`` on success, a positive integer on failure |
| 370 |
|
| 371 |
Example |
| 372 |
^^^^^^^ |
| 373 |
|
| 374 |
.. code-block:: c |
| 375 |
|
| 376 |
char *s3key= getenv("S3KEY"); |
| 377 |
char *s3secret= getenv("S3SECRET"); |
| 378 |
char *s3region= getenv("S3REGION"); |
| 379 |
char *s3bucket= getenv("S3BUCKET"); |
| 380 |
uint8_t res; |
| 381 |
ms3_status_st status; |
| 382 |
|
| 383 |
ms3_library_init(); |
| 384 |
ms3_st *ms3= ms3_thread_init(s3key, s3secret, s3region, NULL); |
| 385 |
|
| 386 |
res= ms3_status(ms3, s3bucket, "test/ms3.txt", &status); |
| 387 |
if (res) |
| 388 |
{ |
| 389 |
printf("Error occurred: %d\n", res); |
| 390 |
return; |
| 391 |
} |
| 392 |
printf("File length: %ld\n", status.length); |
| 393 |
printf("File timestamp: %ld\n", status.created); |
| 394 |
ms3_deinit(ms3); |
| 395 |
|
| 396 |
ms3_set_content_type() |
| 397 |
---------------------- |
| 398 |
|
| 399 |
.. c:function:: void ms3_set_content_type(ms3_st *ms3, const char *content_type) |
| 400 |
|
| 401 |
Sets the ``Content-Type:`` header for subsequent PUT requests. Note that this |
| 402 |
is not copied, so it should remain in scope for each :c:func:`ms3_put()` call. |
| 403 |
Setting this to ``NULL`` will clear the ``Content-Type`` header to the default. |
| 404 |
|
| 405 |
:param ms3: The marias3 object |
| 406 |
:param content_type: The mime type to set |
| 407 |
|
| 408 |
ms3_get_content_type() |
| 409 |
---------------------- |
| 410 |
|
| 411 |
.. c:function:: const char *ms3_get_content_type(ms3_st *ms3) |
| 412 |
|
| 413 |
Gets the ``Content-Type:`` header for the previous GET request response |
| 414 |
from the S3 server. |
| 415 |
The memory for this is part of the :c:type:`ms3_st` object and should not be |
| 416 |
freed by the application. The contents will be reset on each GET request. |
| 417 |
|
| 418 |
:param ms3: The marias3 object |
| 419 |
:param content_type: The mime type for the previous request |
| 420 |
|
| 421 |
|