test/tcs, _azure_sdk: use env vars for blob ut

This commit is contained in:
Minglei Jin 2024-10-22 09:52:24 +08:00
parent 35c200e6ec
commit aa14186f18
2 changed files with 91 additions and 44 deletions

View File

@ -13,42 +13,67 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file
* @brief Application that consumes the Azure SDK for C++.
*
* @remark Set environment variable `STORAGE_CONNECTION_STRING` before running the application.
*
*/
#include <azure/storage/blobs.hpp>
#include <exception>
#include <iostream>
// Include the necessary SDK headers
#include <azure/core.hpp>
#include <azure/storage/blobs.hpp>
// 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<StorageSharedKeyCredential>(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<int>(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;
}

View File

@ -1,4 +1,5 @@
#include <gtest/gtest.h>
#include <cstring>
#include <iostream>
#include <queue>
@ -15,7 +16,7 @@ int32_t tcsInitEnv(int8_t isBlob) {
extern char tsS3BucketName[TSDB_FQDN_LEN];
/* TCS parameter format
tsS3Hostname[0] = "endpoint/<account-name>.blob.core.windows.net";
tsS3Hostname[0] = "<endpoint>/<account-name>.blob.core.windows.net";
tsS3AccessKeyId[0] = "<access-key-id/account-name>";
tsS3AccessKeySecret[0] = "<access-key-secret/account-key>";
tsS3BucketName = "<bucket/container-name>";
@ -23,16 +24,38 @@ int32_t tcsInitEnv(int8_t isBlob) {
tsS3Ablob = isBlob;
if (isBlob) {
const char *hostname = "endpoint/<account-name>.blob.core.windows.net";
const char *hostname = "<endpoint>/<account-name>.blob.core.windows.net";
const char *accessKeyId = "<access-key-id/account-name>";
const char *accessKeySecret = "<access-key-secret/account-key>";
const char *bucketName = "<bucket/container-name>";
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/<account-name>.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);