0

I'm using Linux, I mounted a Azure file share named fileshare01.

Then I wrote a program to create a file in the fileshare01 using C++

Here is my code `

#include <iostream>

#include <fstream>

#include <filesystem>

using namespace std;

int main() {

// Set the file path

string filePath = "randompath/fileshare01/aef.txt";

// Check if file already exists

if (filesystem::exists(filePath)) {

    // Print a message indicating the file already exists

    cout << "Error: File already exists." << endl;

    return 0;

}

// Create the file

ofstream fileStream(filePath);

if (fileStream.is_open()) {

    // Write to the file

    fileStream << "hello" << endl;

    // Close the file

    fileStream.close();

    // Print a message indicating success

    cout << "File created successfully!" << endl;

} else {

    // Print a message indicating failure

    cout << "Error: Could not create file." << endl;

}

return 0;

}

`

It worked, but the official code on the documentation is Documentation

Have I stumbled upon the wrong documentation? Or my code is completely wrong? I'm a beginner and some help will really help me alot.

1 Answers1

0

You can use the Azure Storage C++ SDK to read and write files in Azure file share.

Thanks @ Anton Kolesnyk for the code GitHub Samples on Azure Storage.

    const std::string shareName = "sample-share";
    const std::string fileName = "sample-file";
    const std::string fileContent = "Hello Azure!";

    auto shareClient = ShareClient::CreateFromConnectionString(GetConnectionString(), shareName);
    shareClient.CreateIfNotExists();

    ShareFileClient fileClient = shareClient.GetRootDirectoryClient().GetFileClient(fileName);

    std::vector<uint8_t> buffer(fileContent.begin(), fileContent.end());
    fileClient.UploadFrom(buffer.data(), buffer.size());

    Azure::Storage::Metadata fileMetadata = { {"key1", "value1"}, {"key2", "value2"} };
    fileClient.SetMetadata(fileMetadata);

    auto properties = fileClient.GetProperties().Value;
    for (auto metadata : properties.Metadata)
    {
        std::cout << metadata.first << ":" << metadata.second << std::endl;
    }
    buffer.resize(static_cast<size_t>(properties.FileSize));

    fileClient.DownloadTo(buffer.data(), buffer.size());

In azure

enter image description here

enter image description here

For more information refer to the MSDoc.

Rajesh Mopati
  • 1,329
  • 1
  • 2
  • 7