Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving external dependency abs-fuu to local dep (#525)
* Update CXX version to 17 * Added requirement for C++17 * Removed external dependency abs-fuu * updating install deps script * Removed deprecated function use and moved to Windows API. Also learned about HN * Addressed PR comments * Fixed compiler error * fixed inclusion of a string library * Addressing windows build errors
- Loading branch information
Showing
12 changed files
with
303 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/diagnostics_component/utils/file_upload_utils/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
cmake_minimum_required (VERSION 3.13) | ||
|
||
set (target_name file_upload_utility) | ||
|
||
add_library (${target_name} STATIC src/file_upload_utility.cpp src/blob_storage_helper.cpp | ||
src/blob_storage_helper.hpp) | ||
add_library (diagnostic_utils::${target_name} ALIAS ${target_name}) | ||
|
||
find_package (azure_c_shared_utility REQUIRED) | ||
find_package (azure-storage-blobs-cpp CONFIG REQUIRED) | ||
|
||
include (agentRules) | ||
|
||
include (find_curl_and_import_libcurl) | ||
|
||
find_curl_and_import_libcurl () | ||
|
||
target_include_directories (${target_name} PUBLIC inc) | ||
|
||
target_link_libraries ( | ||
${target_name} | ||
PUBLIC aziotsharedutil | ||
PRIVATE aduc::c_utils aduc::exception_utils Azure::azure-storage-blobs Azure::azure-core | ||
CURL::libcurl) |
33 changes: 33 additions & 0 deletions
33
src/diagnostics_component/utils/file_upload_utils/inc/file_upload_utility.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* @file file_upload_utility.h | ||
* @brief Defines the interface for interacting with Azure Blob Storage and uploading files | ||
* | ||
* @copyright Copyright (c) Microsoft Corp. | ||
*/ | ||
#ifndef FILE_UPLOAD_UTILITY_H | ||
#define FILE_UPLOAD_UTILITY_H | ||
|
||
#include <aduc/c_utils.h> | ||
#include <azure_c_shared_utility/strings.h> | ||
#include <azure_c_shared_utility/vector.h> | ||
#include <stdbool.h> | ||
#include <stdlib.h> | ||
|
||
EXTERN_C_BEGIN | ||
|
||
/** | ||
* @brief Struct that contains the information for uploading a set of blobs to Azure Blob Storage | ||
*/ | ||
typedef struct tagBlobStorageInfo | ||
{ | ||
STRING_HANDLE | ||
virtualDirectoryPath; //!< Virtual hierarchy for the blobs | ||
STRING_HANDLE storageSasCredential; //!< Combined SAS URI and SAS Token for connecting to storage | ||
} BlobStorageInfo; | ||
|
||
bool FileUploadUtility_UploadFilesToContainer( | ||
const BlobStorageInfo* blobInfo, VECTOR_HANDLE fileNames, const char* directoryPath); | ||
|
||
EXTERN_C_END | ||
|
||
#endif // FILE_UPLOAD_UTILITY_H |
99 changes: 99 additions & 0 deletions
99
src/diagnostics_component/utils/file_upload_utils/src/blob_storage_helper.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/** | ||
* @file blob_storage_helper.cpp | ||
* @brief Implements the interface for interacting with Azure Blob Storage | ||
* | ||
* @copyright Copyright (c) Microsoft Corp. | ||
*/ | ||
#include "blob_storage_helper.hpp" | ||
|
||
#include <aduc/exception_utils.hpp> | ||
#include <azure_c_shared_utility/string_token.h> | ||
#include <azure_c_shared_utility/urlencode.h> | ||
#include <cstring> | ||
#include <fstream> | ||
|
||
/** | ||
* @brief Creates the blob storage client using the information in @p blobInfo and then constructs the object | ||
* @param blobInfo information related to the blob storage account | ||
* @param maxConcurrency the maximum number of threads that can be working at one time for the client connection | ||
*/ | ||
AzureBlobStorageHelper::AzureBlobStorageHelper(const BlobStorageInfo& blobInfo) | ||
{ | ||
if (blobInfo.storageSasCredential == nullptr) | ||
{ | ||
throw std::invalid_argument("Container name invalid"); | ||
} | ||
|
||
client = std::make_unique<Azure::Storage::Blobs::BlobContainerClient>(STRING_c_str(blobInfo.storageSasCredential)); | ||
} | ||
|
||
/** | ||
* @brief strips the file name from the @p filePath | ||
* @param fileName a properly formed name for the file who's path is being constructed- must include extension if the file has one | ||
* @param directoryPath a properly formed path to the directory where the file exists | ||
* @returns the filePath, or "" | ||
*/ | ||
std::string | ||
AzureBlobStorageHelper::CreatePathFromFileAndDirectory(const std::string& fileName, const std::string& directoryPath) | ||
{ | ||
if (fileName.empty() || directoryPath.empty()) | ||
{ | ||
throw std::invalid_argument(__FUNCTION__); | ||
} | ||
|
||
bool needsSeperator = false; | ||
if (directoryPath.find_last_of('/') != directoryPath.length() - 1) | ||
{ | ||
needsSeperator = true; | ||
} | ||
|
||
if (needsSeperator) | ||
{ | ||
return directoryPath + "/" + fileName; | ||
} | ||
return directoryPath + fileName; | ||
} | ||
|
||
/** | ||
* @brief Uploads all the files listed in @p files using the storage account associated with this object | ||
* @param fileNames vector of file names to upload | ||
* @param directoryPath path to the directory where @p fileNames can be found | ||
* @param virtualDirectory a properly formatted virtual directory (ending in '/') to be used when uploading the files | ||
* @returns true on success; false on any failure | ||
*/ | ||
bool AzureBlobStorageHelper::UploadFilesToContainer( | ||
VECTOR_HANDLE fileNames, const std::string& directoryPath, const std::string& virtualDirectory) | ||
{ | ||
if (VECTOR_size(fileNames) == 0) | ||
{ | ||
throw std::invalid_argument("blobInfo.storageSasCredential"); | ||
} | ||
|
||
std::string virtualDirectoryPath = virtualDirectory; | ||
if (!virtualDirectoryPath.empty() && virtualDirectoryPath[virtualDirectoryPath.length() - 1] != '/') | ||
{ | ||
virtualDirectoryPath += "/"; | ||
} | ||
|
||
size_t fileNameSize = VECTOR_size(fileNames); | ||
for (unsigned int i = 0; i < fileNameSize; ++i) | ||
{ | ||
auto fileNameHandle = static_cast<const STRING_HANDLE*>(VECTOR_element(fileNames, i)); | ||
const char* fileName = STRING_c_str(*fileNameHandle); | ||
|
||
ADUC::ExceptionUtils::CallVoidMethodAndHandleExceptions( | ||
[fileName, directoryPath, virtualDirectoryPath, this]() -> void { | ||
std::string filePath = CreatePathFromFileAndDirectory(fileName, directoryPath); | ||
|
||
Azure::Core::IO::FileBodyStream fileStream(filePath); | ||
|
||
std::string blobName = virtualDirectoryPath + fileName; | ||
|
||
std::vector<std::pair<std::string, std::string>> metadata; | ||
|
||
this->client->UploadBlob(blobName, fileStream); | ||
}); | ||
} | ||
|
||
return true; | ||
} |
Oops, something went wrong.