aos_status_t *oss_put_object_from_file(const oss_request_options_t *options, const aos_string_t *bucket, const aos_string_t *object, const aos_string_t *filename, aos_table_t *headers, aos_table_t **resp_headers) { aos_status_t *s = NULL; aos_http_request_t *req = NULL; aos_http_response_t *resp = NULL; aos_table_t *query_params = NULL; int res = AOSE_OK; s = aos_status_create(options->pool); headers = aos_table_create_if_null(options, headers, 1); set_content_type(filename->data, object->data, headers); query_params = aos_table_create_if_null(options, query_params, 0); oss_init_object_request(options, bucket, object, HTTP_PUT, &req, query_params, headers, &resp); res = oss_write_request_body_from_file(options->pool, filename, req); if (res != AOSE_OK) { aos_file_error_status_set(s, res); return s; } s = oss_process_request(options, req, resp); *resp_headers = resp->headers; return s; }
aos_status_t *oss_get_object_to_file_by_url(const oss_request_options_t *options, const aos_string_t *signed_url, aos_table_t *headers, aos_table_t *params, aos_string_t *filename, aos_table_t **resp_headers) { aos_status_t *s = NULL; aos_http_request_t *req = NULL; aos_http_response_t *resp = NULL; int res = AOSE_OK; s = aos_status_create(options->pool); headers = aos_table_create_if_null(options, headers, 0); params = aos_table_create_if_null(options, params, 0); oss_init_signed_url_request(options, signed_url, HTTP_GET, &req, params, headers, &resp); res = oss_init_read_response_body_to_file(options->pool, filename, resp); if (res != AOSE_OK) { aos_file_error_status_set(s, res); return s; } s = oss_process_signed_request(options, req, resp); *resp_headers = resp->headers; return s; }
aos_status_t *oss_send_request(aos_http_controller_t *ctl, aos_http_request_t *req, aos_http_response_t *resp) { aos_status_t *s; const char *reason; int res = AOSE_OK; s = aos_status_create(ctl->pool); res = aos_http_send_request(ctl, req, resp); if (res != AOSE_OK) { reason = aos_http_controller_get_reason(ctl); aos_status_set(s, res, AOS_HTTP_IO_ERROR_CODE, reason); } else if (!aos_http_is_ok(resp->status)) { s = aos_status_parse_from_body(ctl->pool, &resp->body, resp->status, s); } else { s->code = resp->status; } s->req_id = (char*)(apr_table_get(resp->headers, "x-oss-request-id")); if (s->req_id == NULL) { s->req_id = (char*)(apr_table_get(resp->headers, "x-img-request-id")); if (s->req_id == NULL) { s->req_id = ""; } } return s; }
aos_status_t *oss_process_request(const oss_request_options_t *options, aos_http_request_t *req, aos_http_response_t *resp) { int res = AOSE_OK; aos_status_t *s; s = aos_status_create(options->pool); res = oss_sign_request(req, options->config); if (res != AOSE_OK) { aos_status_set(s, res, AOS_CLIENT_ERROR_CODE, NULL); return s; } return oss_send_request(options->ctl, req, resp); }
aos_status_t *oss_append_object_from_file(const oss_request_options_t *options, const aos_string_t *bucket, const aos_string_t *object, int64_t position, const aos_string_t *append_file, aos_table_t *headers, aos_table_t **resp_headers) { aos_status_t *s = NULL; aos_http_request_t *req = NULL; aos_http_response_t *resp = NULL; aos_table_t *query_params = NULL; int res = AOSE_OK; /* init query_params */ query_params = aos_table_create_if_null(options, query_params, 2); apr_table_add(query_params, OSS_APPEND, ""); aos_table_add_int64(query_params, OSS_POSITION, position); /* init headers */ headers = aos_table_create_if_null(options, headers, 1); set_content_type(append_file->data, object->data, headers); oss_init_object_request(options, bucket, object, HTTP_POST, &req, query_params, headers, &resp); res = oss_write_request_body_from_file(options->pool, append_file, req); s = aos_status_create(options->pool); if (res != AOSE_OK) { aos_file_error_status_set(s, res); return s; } s = oss_process_request(options, req, resp); *resp_headers = resp->headers; return s; }
aos_status_t *log_post_logs_with_sts_token(aos_pool_t *p, const char *endpoint, const char * accesskeyId, const char *accessKey, const char *stsToken, const char *project, const char *logstore, cJSON *root) { aos_string_t project_name, logstore_name; aos_table_t *headers = NULL; aos_table_t *resp_headers = NULL; log_request_options_t *options = NULL; aos_list_t buffer; unsigned char *md5 = NULL; char *buf = NULL; int64_t buf_len; char *b64_value = NULL; aos_buf_t *content = NULL; aos_status_t *s = NULL; options = log_request_options_create(p); options->config = log_config_create(options->pool); aos_str_set(&(options->config->endpoint), endpoint); aos_str_set(&(options->config->access_key_id), accesskeyId); aos_str_set(&(options->config->access_key_secret), accessKey); if(stsToken != NULL) { aos_str_set(&(options->config->sts_token), stsToken); } options->ctl = aos_http_controller_create(options->pool, 0); headers = aos_table_make(p, 5); apr_table_set(headers, LOG_API_VERSION, "0.6.0"); apr_table_set(headers, LOG_COMPRESS_TYPE, "lz4"); apr_table_set(headers, LOG_SIGNATURE_METHOD, "hmac-sha1"); apr_table_set(headers, LOG_CONTENT_TYPE, "application/json"); aos_str_set(&project_name, project); aos_str_set(&logstore_name, logstore); aos_list_init(&buffer); char *body = cJSON_PrintUnformatted(root); if(body == NULL) { s = aos_status_create(options->pool); aos_status_set(s, 400, AOS_CLIENT_ERROR_CODE, "fail to format cJSON data"); return s; } int org_body_size = strlen(body); apr_table_set(headers, LOG_BODY_RAW_SIZE, apr_itoa(options->pool, org_body_size)); int compress_bound = LZ4_compressBound(org_body_size); char *compress_data = aos_pcalloc(options->pool, compress_bound); int compressed_size = LZ4_compress(body, compress_data, org_body_size); if(compressed_size <= 0) { s = aos_status_create(options->pool); aos_status_set(s, 400, AOS_CLIENT_ERROR_CODE, "fail to compress json data"); return s; } content = aos_buf_pack(options->pool, compress_data, compressed_size); aos_list_add_tail(&content->node, &buffer); //add Content-MD5 buf_len = aos_buf_list_len(&buffer); buf = aos_buf_list_content(options->pool, &buffer); md5 = aos_md5(options->pool, buf, (apr_size_t)buf_len); b64_value = aos_pcalloc(options->pool, 50); int loop = 0; for(; loop < 16; ++loop) { unsigned char a = ((*md5)>>4) & 0xF, b = (*md5) & 0xF; b64_value[loop<<1] = a > 9 ? (a - 10 + 'A') : (a + '0'); b64_value[(loop<<1)|1] = b > 9 ? (b - 10 + 'A') : (b + '0'); ++md5; } b64_value[loop<<1] = '\0'; apr_table_set(headers, LOG_CONTENT_MD5, b64_value); s = log_post_logs_from_buffer(options, &project_name, &logstore_name, &buffer, headers, &resp_headers); free(body); return s; }