update storage samples (#2380)
* update storage samples * use CreateIfNotExists
This commit is contained in:
parent
fe7b88194a
commit
c8a525ec74
@ -23,7 +23,8 @@ void BlobsGettingStarted()
|
|||||||
|
|
||||||
BlockBlobClient blobClient = containerClient.GetBlockBlobClient(blobName);
|
BlockBlobClient blobClient = containerClient.GetBlockBlobClient(blobName);
|
||||||
|
|
||||||
blobClient.UploadFrom(reinterpret_cast<const uint8_t*>(blobContent.data()), blobContent.size());
|
std::vector<uint8_t> buffer(blobContent.begin(), blobContent.end());
|
||||||
|
blobClient.UploadFrom(buffer.data(), buffer.size());
|
||||||
|
|
||||||
Azure::Storage::Metadata blobMetadata = {{"key1", "value1"}, {"key2", "value2"}};
|
Azure::Storage::Metadata blobMetadata = {{"key1", "value1"}, {"key2", "value2"}};
|
||||||
blobClient.SetMetadata(blobMetadata);
|
blobClient.SetMetadata(blobMetadata);
|
||||||
@ -33,9 +34,10 @@ void BlobsGettingStarted()
|
|||||||
{
|
{
|
||||||
std::cout << metadata.first << ":" << metadata.second << std::endl;
|
std::cout << metadata.first << ":" << metadata.second << std::endl;
|
||||||
}
|
}
|
||||||
blobContent.resize(static_cast<size_t>(properties.BlobSize));
|
// We know blob size is small, so it's safe to cast here.
|
||||||
|
buffer.resize(static_cast<size_t>(properties.BlobSize));
|
||||||
|
|
||||||
blobClient.DownloadTo(reinterpret_cast<uint8_t*>(&blobContent[0]), blobContent.size());
|
blobClient.DownloadTo(buffer.data(), buffer.size());
|
||||||
|
|
||||||
std::cout << blobContent << std::endl;
|
std::cout << std::string(buffer.begin(), buffer.end()) << std::endl;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,93 +10,51 @@
|
|||||||
SAMPLE(DataLakeGettingStarted, DataLakeGettingStarted)
|
SAMPLE(DataLakeGettingStarted, DataLakeGettingStarted)
|
||||||
void DataLakeGettingStarted()
|
void DataLakeGettingStarted()
|
||||||
{
|
{
|
||||||
// Please note that you can always reference test cases for advanced usages.
|
|
||||||
|
|
||||||
using namespace Azure::Storage::Files::DataLake;
|
using namespace Azure::Storage::Files::DataLake;
|
||||||
|
|
||||||
std::string fileSystemName = "sample-file-system";
|
std::string fileSystemName = "sample-file-system";
|
||||||
std::string directoryName = "sample-directory";
|
std::string directoryName = "sample-directory";
|
||||||
std::string fileName = "sample-file";
|
std::string fileName = "sample-file";
|
||||||
|
|
||||||
// Initializing a ServiceClient that can then initialize the FileSystemClient or list file
|
|
||||||
// systems.
|
|
||||||
auto serviceClient = DataLakeServiceClient::CreateFromConnectionString(GetConnectionString());
|
|
||||||
// Initializing a FileSystemClient that can then initialize the PathClient, FileClient,
|
|
||||||
// DirectoryClient.
|
|
||||||
auto fileSystemClient
|
auto fileSystemClient
|
||||||
= DataLakeFileSystemClient::CreateFromConnectionString(GetConnectionString(), fileSystemName);
|
= DataLakeFileSystemClient::CreateFromConnectionString(GetConnectionString(), fileSystemName);
|
||||||
|
fileSystemClient.CreateIfNotExists();
|
||||||
|
|
||||||
try
|
// Create a directory.
|
||||||
{
|
auto directoryClient = fileSystemClient.GetDirectoryClient(directoryName);
|
||||||
// Create file systems and ignore the already exist error.
|
directoryClient.Create();
|
||||||
try
|
|
||||||
{
|
|
||||||
fileSystemClient.Create();
|
|
||||||
}
|
|
||||||
catch (const Azure::Storage::StorageException& e)
|
|
||||||
{
|
|
||||||
if (e.ErrorCode != "ContainerAlreadyExists")
|
|
||||||
{
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
std::cout << "ErrorCode: " + e.ErrorCode << std::endl;
|
|
||||||
std::cout << "ReasonPhrase: " + e.ReasonPhrase << std::endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a directory.
|
// Creates a file under the directory.
|
||||||
auto directoryClient = fileSystemClient.GetDirectoryClient(directoryName);
|
auto fileClient = directoryClient.GetFileClient(fileName);
|
||||||
directoryClient.Create();
|
fileClient.Create();
|
||||||
|
|
||||||
// Creates a file under the directory.
|
// Append/flush/read data from the client.
|
||||||
auto fileClient = directoryClient.GetFileClient(fileName);
|
// Append data
|
||||||
fileClient.Create();
|
// Initialize the string that contains the first piece of data to be appended to the file.
|
||||||
|
std::string str1 = "Hello ";
|
||||||
|
// Initialize the buffer that represents what contains your data to be appended, please ignore
|
||||||
|
// how it is constructed here, since the memory copy is not efficient.
|
||||||
|
std::string str2 = "Azure!";
|
||||||
|
std::vector<uint8_t> buffer(str1.begin(), str1.end());
|
||||||
|
|
||||||
// Append/flush/read data from the client.
|
// One way of passing in the buffer, note that the buffer is not copied.
|
||||||
// Append data
|
auto bufferStream = Azure::Core::IO::MemoryBodyStream(buffer);
|
||||||
// Initialize the string that contains the first piece of data to be appended to the file.
|
|
||||||
std::string str1 = "Hello ";
|
|
||||||
// Initialize the buffer that represents what contains your data to be appended, please ignore
|
|
||||||
// how it is constructed here, since the memory copy is not efficient.
|
|
||||||
std::string str2 = "World!";
|
|
||||||
std::vector<uint8_t> buffer(str1.begin(), str1.end());
|
|
||||||
|
|
||||||
// One way of passing in the buffer, note that the buffer is not copied.
|
fileClient.Append(bufferStream, 0 /* Offset of the position to be appended.*/);
|
||||||
auto bufferStream = Azure::Core::IO::MemoryBodyStream(buffer);
|
|
||||||
|
|
||||||
fileClient.Append(bufferStream, 0 /* Offset of the position to be appended.*/);
|
// Another way of passing in the buffer, note that buffer is also not copied.
|
||||||
|
bufferStream = Azure::Core::IO::MemoryBodyStream(
|
||||||
|
reinterpret_cast<const uint8_t*>(str2.data()), str2.size());
|
||||||
|
|
||||||
// Another way of passing in the buffer, note that buffer is also not copied.
|
fileClient.Append(bufferStream, str1.size());
|
||||||
bufferStream = Azure::Core::IO::MemoryBodyStream(
|
|
||||||
reinterpret_cast<const uint8_t*>(str2.data()), str2.size());
|
|
||||||
|
|
||||||
fileClient.Append(bufferStream, str1.size());
|
// Flush
|
||||||
|
fileClient.Flush(str1.size() + str2.size());
|
||||||
|
|
||||||
// Flush
|
// Read
|
||||||
fileClient.Flush(str1.size() + str2.size());
|
auto result = fileClient.Download();
|
||||||
|
Azure::Core::Context context;
|
||||||
// Read
|
std::vector<uint8_t> downloaded = result.Value.Body->ReadToEnd(context);
|
||||||
auto result = fileClient.Download();
|
// downloaded contains your downloaded data.
|
||||||
Azure::Core::Context context;
|
std::cout << std::string(downloaded.begin(), downloaded.end()) << std::endl;
|
||||||
std::vector<uint8_t> downloaded = result.Value.Body->ReadToEnd(context);
|
|
||||||
// downloaded contains your downloaded data.
|
|
||||||
std::cout << "Downloaded data was:\n" + std::string(downloaded.begin(), downloaded.end())
|
|
||||||
<< std::endl;
|
|
||||||
|
|
||||||
// Delete file system.
|
|
||||||
fileSystemClient.Delete();
|
|
||||||
|
|
||||||
std::cout << "Successfully finished sample." << std::endl;
|
|
||||||
}
|
|
||||||
catch (const Azure::Storage::StorageException& e)
|
|
||||||
{
|
|
||||||
// Deal with the information when storage error is met.
|
|
||||||
std::cout << "Error encountered when sending the request." << std::endl;
|
|
||||||
std::cout << "ErrorCode: " + e.ErrorCode << std::endl;
|
|
||||||
std::cout << "Message: " + e.Message << std::endl;
|
|
||||||
std::cout << "ReasonPhrase: " + e.ReasonPhrase << std::endl;
|
|
||||||
std::cout << "RequestId: " + e.RequestId << std::endl;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,19 +17,12 @@ void FileShareGettingStarted()
|
|||||||
std::string fileContent = "Hello Azure!";
|
std::string fileContent = "Hello Azure!";
|
||||||
|
|
||||||
auto shareClient = ShareClient::CreateFromConnectionString(GetConnectionString(), shareName);
|
auto shareClient = ShareClient::CreateFromConnectionString(GetConnectionString(), shareName);
|
||||||
try
|
shareClient.CreateIfNotExists();
|
||||||
{
|
|
||||||
shareClient.Create();
|
|
||||||
}
|
|
||||||
catch (const std::runtime_error& e)
|
|
||||||
{
|
|
||||||
// The share may already exist
|
|
||||||
std::cout << e.what() << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
ShareFileClient fileClient = shareClient.GetRootDirectoryClient().GetFileClient(fileName);
|
ShareFileClient fileClient = shareClient.GetRootDirectoryClient().GetFileClient(fileName);
|
||||||
|
|
||||||
fileClient.UploadFrom(reinterpret_cast<const uint8_t*>(fileContent.data()), fileContent.size());
|
std::vector<uint8_t> buffer(fileContent.begin(), fileContent.end());
|
||||||
|
fileClient.UploadFrom(buffer.data(), buffer.size());
|
||||||
|
|
||||||
Azure::Storage::Metadata fileMetadata = {{"key1", "value1"}, {"key2", "value2"}};
|
Azure::Storage::Metadata fileMetadata = {{"key1", "value1"}, {"key2", "value2"}};
|
||||||
fileClient.SetMetadata(fileMetadata);
|
fileClient.SetMetadata(fileMetadata);
|
||||||
@ -39,9 +32,10 @@ void FileShareGettingStarted()
|
|||||||
{
|
{
|
||||||
std::cout << metadata.first << ":" << metadata.second << std::endl;
|
std::cout << metadata.first << ":" << metadata.second << std::endl;
|
||||||
}
|
}
|
||||||
fileContent.resize(static_cast<size_t>(properties.FileSize));
|
// We know file size is small, so it's safe to cast here.
|
||||||
|
buffer.resize(static_cast<size_t>(properties.FileSize));
|
||||||
|
|
||||||
fileClient.DownloadTo(reinterpret_cast<uint8_t*>(&fileContent[0]), fileContent.size());
|
fileClient.DownloadTo(buffer.data(), buffer.size());
|
||||||
|
|
||||||
std::cout << fileContent << std::endl;
|
std::cout << std::string(buffer.begin(), buffer.end()) << std::endl;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user