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/>. * 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 <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; using namespace Azure::Storage::Blobs;
int main(int argc, char* argv[]) { // Secrets should be stored & retrieved from secure locations such as Azure::KeyVault. For
(void)argc; // convenience and brevity of samples, the secrets are retrieved from environment variables.
(void)argv;
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 { try {
auto containerClient = auto sharedKeyCredential = std::make_shared<StorageSharedKeyCredential>(accountName, accountKey);
BlobContainerClient::CreateFromConnectionString(std::getenv("STORAGE_CONNECTION_STRING"), "td-test");
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 // Create the container if it does not exist
// std::cout << "Creating container: " << containerName << std::endl; std::cout << "Creating container: " << containerName << std::endl;
// containerClient.CreateIfNotExists(); // containerClient.CreateIfNotExists();
/**************** Container SDK client ************************/ std::string blobName = "blob.txt";
/**************** list blobs (one page) ******************/ uint8_t blobContent[] = "Hello Azure!";
// auto response = containerClient.ListBlobsSinglePage(); // Create the block blob client
// auto response = containerClient.ListBlobs(); BlockBlobClient blobClient = containerClient.GetBlockBlobClient(blobName);
// auto blobListPage = response.Value;
// auto blobListPage = response.Blobs; // Upload the blob
//(void)_azUploadFrom(blobClient, file, offset, size); std::cout << "Uploading blob: " << blobName << std::endl;
blobClient.UploadFrom(blobContent, sizeof(blobContent));
/* /*
auto blockBlobClient = BlockBlobClient(endpointUrl, sharedKeyCredential); 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::cout << "Last modified date of uploaded blob: " << model.LastModified.ToString()
<< std::endl; << 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; return 1;
} }

View File

@ -1,4 +1,5 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#include <queue> #include <queue>
@ -15,7 +16,7 @@ int32_t tcsInitEnv(int8_t isBlob) {
extern char tsS3BucketName[TSDB_FQDN_LEN]; extern char tsS3BucketName[TSDB_FQDN_LEN];
/* TCS parameter format /* 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>"; tsS3AccessKeyId[0] = "<access-key-id/account-name>";
tsS3AccessKeySecret[0] = "<access-key-secret/account-key>"; tsS3AccessKeySecret[0] = "<access-key-secret/account-key>";
tsS3BucketName = "<bucket/container-name>"; tsS3BucketName = "<bucket/container-name>";
@ -23,16 +24,38 @@ int32_t tcsInitEnv(int8_t isBlob) {
tsS3Ablob = isBlob; tsS3Ablob = isBlob;
if (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 *accessKeyId = "<access-key-id/account-name>";
const char *accessKeySecret = "<access-key-secret/account-key>"; const char *accessKeySecret = "<access-key-secret/account-key>";
const char *bucketName = "<bucket/container-name>"; const char *bucketName = "<bucket/container-name>";
tstrncpy(&tsS3Hostname[0][0], hostname, TSDB_FQDN_LEN); if (hostname[0] != '<') {
tstrncpy(&tsS3AccessKeyId[0][0], accessKeyId, TSDB_FQDN_LEN); tstrncpy(&tsS3Hostname[0][0], hostname, TSDB_FQDN_LEN);
tstrncpy(&tsS3AccessKeySecret[0][0], accessKeySecret, TSDB_FQDN_LEN); tstrncpy(&tsS3AccessKeyId[0][0], accessKeyId, TSDB_FQDN_LEN);
tstrncpy(tsS3BucketName, bucketName, 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 { } else {
/* /*
const char *hostname = "endpoint/<account-name>.blob.core.windows.net"; 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); tstrncpy(tsTempDir, "/tmp/", PATH_MAX);
tsS3Enabled = true; tsS3Enabled = true;
if (!tsS3Ablob) {
}
return code; return code;
} }
TEST(TcsTest, DISABLED_InterfaceTest) { // TEST(TcsTest, DISABLED_InterfaceTest) {
// TEST(TcsTest, InterfaceTest) { TEST(TcsTest, InterfaceTest) {
int code = 0; int code = 0;
bool check = false; bool check = false;
bool withcp = false; bool withcp = false;
code = tcsInitEnv(true); code = tcsInitEnv(true);
if (code) {
std::cout << "ablob env init failed with: " << code << std::endl;
return;
}
GTEST_ASSERT_EQ(code, 0); GTEST_ASSERT_EQ(code, 0);
GTEST_ASSERT_EQ(tsS3Enabled, 1); GTEST_ASSERT_EQ(tsS3Enabled, 1);
GTEST_ASSERT_EQ(tsS3Ablob, 1); GTEST_ASSERT_EQ(tsS3Ablob, 1);