diff --git a/contrib/test/azure/main.cpp b/contrib/test/azure/main.cpp index 943546a5fb..78ecc8b9f5 100644 --- a/contrib/test/azure/main.cpp +++ b/contrib/test/azure/main.cpp @@ -13,42 +13,67 @@ * along with this program. If not, see . */ -/** - * @file - * @brief Application that consumes the Azure SDK for C++. - * - * @remark Set environment variable `STORAGE_CONNECTION_STRING` before running the application. - * - */ - -#include - -#include #include +// Include the necessary SDK headers +#include +#include + +// Add appropriate using namespace directives +using namespace Azure::Storage; using namespace Azure::Storage::Blobs; -int main(int argc, char* argv[]) { - (void)argc; - (void)argv; +// Secrets should be stored & retrieved from secure locations such as Azure::KeyVault. For +// convenience and brevity of samples, the secrets are retrieved from environment variables. + +std::string GetEndpointUrl() { + // return std::getenv("AZURE_STORAGE_ACCOUNT_URL"); + std::string accountId = getenv("ablob_account_id"); + if (accountId.empty()) { + return accountId; + } + + return accountId + ".blob.core.windows.net"; +} + +std::string GetAccountName() { + // return std::getenv("AZURE_STORAGE_ACCOUNT_NAME"); + return getenv("ablob_account_id"); +} + +std::string GetAccountKey() { + // return std::getenv("AZURE_STORAGE_ACCOUNT_KEY"); + + return getenv("ablob_account_secret"); +} + +int main() { + std::string endpointUrl = GetEndpointUrl(); + std::string accountName = GetAccountName(); + std::string accountKey = GetAccountKey(); - /**************** Container SDK client ************************/ - /**************** Create container ************************/ try { - auto containerClient = - BlobContainerClient::CreateFromConnectionString(std::getenv("STORAGE_CONNECTION_STRING"), "td-test"); + auto sharedKeyCredential = std::make_shared(accountName, accountKey); + + std::string accountURL = "https://fd2d01cd892f844eeaa2273.blob.core.windows.net"; + BlobServiceClient blobServiceClient(accountURL, sharedKeyCredential); + + std::string containerName = "myblobcontainer"; + // auto containerClient = blobServiceClient.GetBlobContainerClient("myblobcontainer"); + auto containerClient = blobServiceClient.GetBlobContainerClient("td-test"); // Create the container if it does not exist - // std::cout << "Creating container: " << containerName << std::endl; + std::cout << "Creating container: " << containerName << std::endl; // containerClient.CreateIfNotExists(); - /**************** Container SDK client ************************/ - /**************** list blobs (one page) ******************/ - // auto response = containerClient.ListBlobsSinglePage(); - // auto response = containerClient.ListBlobs(); - // auto blobListPage = response.Value; - // auto blobListPage = response.Blobs; - //(void)_azUploadFrom(blobClient, file, offset, size); + std::string blobName = "blob.txt"; + uint8_t blobContent[] = "Hello Azure!"; + // Create the block blob client + BlockBlobClient blobClient = containerClient.GetBlockBlobClient(blobName); + + // Upload the blob + std::cout << "Uploading blob: " << blobName << std::endl; + blobClient.UploadFrom(blobContent, sizeof(blobContent)); /* auto blockBlobClient = BlockBlobClient(endpointUrl, sharedKeyCredential); @@ -62,15 +87,11 @@ int main(int argc, char* argv[]) { std::cout << "Last modified date of uploaded blob: " << model.LastModified.ToString() << std::endl; */ + } catch (const Azure::Core::RequestFailedException& e) { + std::cout << "Status Code: " << static_cast(e.StatusCode) << ", Reason Phrase: " << e.ReasonPhrase + << std::endl; + std::cout << e.what() << std::endl; - for (auto page = containerClient.ListBlobs(/*options*/); page.HasPage(); page.MoveToNextPage()) { - for (auto& blob : page.Blobs) { - std::cout << blob.Name << std::endl; - } - } - - } catch (const std::exception& ex) { - std::cout << ex.what(); return 1; } diff --git a/source/libs/tcs/test/tcsTest.cpp b/source/libs/tcs/test/tcsTest.cpp index d07513c644..5e17d09fc7 100644 --- a/source/libs/tcs/test/tcsTest.cpp +++ b/source/libs/tcs/test/tcsTest.cpp @@ -1,4 +1,5 @@ #include + #include #include #include @@ -15,7 +16,7 @@ int32_t tcsInitEnv(int8_t isBlob) { extern char tsS3BucketName[TSDB_FQDN_LEN]; /* TCS parameter format - tsS3Hostname[0] = "endpoint/.blob.core.windows.net"; + tsS3Hostname[0] = "/.blob.core.windows.net"; tsS3AccessKeyId[0] = ""; tsS3AccessKeySecret[0] = ""; tsS3BucketName = ""; @@ -23,16 +24,38 @@ int32_t tcsInitEnv(int8_t isBlob) { tsS3Ablob = isBlob; if (isBlob) { - const char *hostname = "endpoint/.blob.core.windows.net"; + const char *hostname = "/.blob.core.windows.net"; const char *accessKeyId = ""; const char *accessKeySecret = ""; const char *bucketName = ""; - tstrncpy(&tsS3Hostname[0][0], hostname, TSDB_FQDN_LEN); - tstrncpy(&tsS3AccessKeyId[0][0], accessKeyId, TSDB_FQDN_LEN); - tstrncpy(&tsS3AccessKeySecret[0][0], accessKeySecret, TSDB_FQDN_LEN); - tstrncpy(tsS3BucketName, bucketName, TSDB_FQDN_LEN); + if (hostname[0] != '<') { + tstrncpy(&tsS3Hostname[0][0], hostname, TSDB_FQDN_LEN); + tstrncpy(&tsS3AccessKeyId[0][0], accessKeyId, TSDB_FQDN_LEN); + tstrncpy(&tsS3AccessKeySecret[0][0], accessKeySecret, TSDB_FQDN_LEN); + tstrncpy(tsS3BucketName, bucketName, TSDB_FQDN_LEN); + } else { + const char *accountId = getenv("ablob_account_id"); + if (!accountId) { + return -1; + } + const char *accountSecret = getenv("ablob_account_secret"); + if (!accountSecret) { + return -1; + } + + const char *containerName = getenv("ablob_container"); + if (!containerName) { + return -1; + } + + TAOS_STRCPY(&tsS3Hostname[0][0], accountId); + TAOS_STRCAT(&tsS3Hostname[0][0], ".blob.core.windows.net"); + TAOS_STRCPY(&tsS3AccessKeyId[0][0], accountId); + TAOS_STRCPY(&tsS3AccessKeySecret[0][0], accountSecret); + TAOS_STRCPY(tsS3BucketName, containerName); + } } else { /* const char *hostname = "endpoint/.blob.core.windows.net"; @@ -67,19 +90,22 @@ int32_t tcsInitEnv(int8_t isBlob) { tstrncpy(tsTempDir, "/tmp/", PATH_MAX); tsS3Enabled = true; - if (!tsS3Ablob) { - } return code; } -TEST(TcsTest, DISABLED_InterfaceTest) { - // TEST(TcsTest, InterfaceTest) { +// TEST(TcsTest, DISABLED_InterfaceTest) { +TEST(TcsTest, InterfaceTest) { int code = 0; bool check = false; bool withcp = false; code = tcsInitEnv(true); + if (code) { + std::cout << "ablob env init failed with: " << code << std::endl; + return; + } + GTEST_ASSERT_EQ(code, 0); GTEST_ASSERT_EQ(tsS3Enabled, 1); GTEST_ASSERT_EQ(tsS3Ablob, 1);