diff --git a/sdk/storage/azure-storage-blobs/sample/blob_getting_started.cpp b/sdk/storage/azure-storage-blobs/sample/blob_getting_started.cpp index 3addddbbc..986acf7df 100644 --- a/sdk/storage/azure-storage-blobs/sample/blob_getting_started.cpp +++ b/sdk/storage/azure-storage-blobs/sample/blob_getting_started.cpp @@ -23,7 +23,8 @@ void BlobsGettingStarted() BlockBlobClient blobClient = containerClient.GetBlockBlobClient(blobName); - blobClient.UploadFrom(reinterpret_cast(blobContent.data()), blobContent.size()); + std::vector buffer(blobContent.begin(), blobContent.end()); + blobClient.UploadFrom(buffer.data(), buffer.size()); Azure::Storage::Metadata blobMetadata = {{"key1", "value1"}, {"key2", "value2"}}; blobClient.SetMetadata(blobMetadata); @@ -33,9 +34,10 @@ void BlobsGettingStarted() { std::cout << metadata.first << ":" << metadata.second << std::endl; } - blobContent.resize(static_cast(properties.BlobSize)); + // We know blob size is small, so it's safe to cast here. + buffer.resize(static_cast(properties.BlobSize)); - blobClient.DownloadTo(reinterpret_cast(&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; } diff --git a/sdk/storage/azure-storage-files-datalake/sample/datalake_getting_started.cpp b/sdk/storage/azure-storage-files-datalake/sample/datalake_getting_started.cpp index fb45ee5e5..65d28563e 100644 --- a/sdk/storage/azure-storage-files-datalake/sample/datalake_getting_started.cpp +++ b/sdk/storage/azure-storage-files-datalake/sample/datalake_getting_started.cpp @@ -10,93 +10,51 @@ SAMPLE(DataLakeGettingStarted, DataLakeGettingStarted) void DataLakeGettingStarted() { - // Please note that you can always reference test cases for advanced usages. - using namespace Azure::Storage::Files::DataLake; std::string fileSystemName = "sample-file-system"; std::string directoryName = "sample-directory"; 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 = DataLakeFileSystemClient::CreateFromConnectionString(GetConnectionString(), fileSystemName); + fileSystemClient.CreateIfNotExists(); - try - { - // Create file systems and ignore the already exist error. - 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. + auto directoryClient = fileSystemClient.GetDirectoryClient(directoryName); + directoryClient.Create(); - // Create a directory. - auto directoryClient = fileSystemClient.GetDirectoryClient(directoryName); - directoryClient.Create(); + // Creates a file under the directory. + auto fileClient = directoryClient.GetFileClient(fileName); + fileClient.Create(); - // Creates a file under the directory. - auto fileClient = directoryClient.GetFileClient(fileName); - fileClient.Create(); + // Append/flush/read data from the client. + // Append data + // 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 buffer(str1.begin(), str1.end()); - // Append/flush/read data from the client. - // Append data - // 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 buffer(str1.begin(), str1.end()); + // One way of passing in the buffer, note that the buffer is not copied. + auto bufferStream = Azure::Core::IO::MemoryBodyStream(buffer); - // One way of passing in the buffer, note that the buffer is not copied. - auto bufferStream = Azure::Core::IO::MemoryBodyStream(buffer); + fileClient.Append(bufferStream, 0 /* Offset of the position to be appended.*/); - 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(str2.data()), str2.size()); - // Another way of passing in the buffer, note that buffer is also not copied. - bufferStream = Azure::Core::IO::MemoryBodyStream( - reinterpret_cast(str2.data()), str2.size()); + fileClient.Append(bufferStream, str1.size()); - fileClient.Append(bufferStream, str1.size()); + // Flush + fileClient.Flush(str1.size() + str2.size()); - // Flush - fileClient.Flush(str1.size() + str2.size()); - - // Read - auto result = fileClient.Download(); - Azure::Core::Context context; - std::vector 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; - } + // Read + auto result = fileClient.Download(); + Azure::Core::Context context; + std::vector downloaded = result.Value.Body->ReadToEnd(context); + // downloaded contains your downloaded data. + std::cout << std::string(downloaded.begin(), downloaded.end()) << std::endl; } diff --git a/sdk/storage/azure-storage-files-shares/sample/file_share_getting_started.cpp b/sdk/storage/azure-storage-files-shares/sample/file_share_getting_started.cpp index a94dcb709..07036d00e 100644 --- a/sdk/storage/azure-storage-files-shares/sample/file_share_getting_started.cpp +++ b/sdk/storage/azure-storage-files-shares/sample/file_share_getting_started.cpp @@ -17,19 +17,12 @@ void FileShareGettingStarted() std::string fileContent = "Hello Azure!"; auto shareClient = ShareClient::CreateFromConnectionString(GetConnectionString(), shareName); - try - { - shareClient.Create(); - } - catch (const std::runtime_error& e) - { - // The share may already exist - std::cout << e.what() << std::endl; - } + shareClient.CreateIfNotExists(); ShareFileClient fileClient = shareClient.GetRootDirectoryClient().GetFileClient(fileName); - fileClient.UploadFrom(reinterpret_cast(fileContent.data()), fileContent.size()); + std::vector buffer(fileContent.begin(), fileContent.end()); + fileClient.UploadFrom(buffer.data(), buffer.size()); Azure::Storage::Metadata fileMetadata = {{"key1", "value1"}, {"key2", "value2"}}; fileClient.SetMetadata(fileMetadata); @@ -39,9 +32,10 @@ void FileShareGettingStarted() { std::cout << metadata.first << ":" << metadata.second << std::endl; } - fileContent.resize(static_cast(properties.FileSize)); + // We know file size is small, so it's safe to cast here. + buffer.resize(static_cast(properties.FileSize)); - fileClient.DownloadTo(reinterpret_cast(&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; }