Skip to content

Commit 3c02188

Browse files
Jinming-Huvinjiang
authored andcommitted
Add Files Properties sample
1 parent 9758249 commit 3c02188

4 files changed

+92
-0
lines changed

Microsoft.WindowsAzure.Storage/samples/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ if(UNIX)
1010
QueuesGettingStarted.cpp
1111
samples_common.h
1212
TablesGettingStarted.cpp
13+
FilesProperties.cpp
1314
)
1415
endif()
1516

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// -----------------------------------------------------------------------------------------
2+
// <copyright file="FilesProperties.cpp" company="Microsoft">
3+
// Copyright 2013 Microsoft Corporation
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
// </copyright>
16+
// -----------------------------------------------------------------------------------------
17+
18+
#include "samples_common.h"
19+
20+
#include <was/storage_account.h>
21+
#include <was/file.h>
22+
23+
24+
namespace azure { namespace storage { namespace samples {
25+
26+
SAMPLE(FilesProperties, files_properties_sample)
27+
void files_properties_sample()
28+
{
29+
try
30+
{
31+
// Initialize storage account
32+
azure::storage::cloud_storage_account storage_account = azure::storage::cloud_storage_account::parse(storage_connection_string);
33+
34+
// Create share
35+
azure::storage::cloud_file_client file_client = storage_account.create_cloud_file_client();
36+
azure::storage::cloud_file_share share = file_client.get_share_reference(_XPLATSTR("my-sample-share"));
37+
share.create_if_not_exists();
38+
39+
// azure-storage-cpp sdk treats permission as an opaque string. The string below is pretty much a default permission.
40+
utility::string_t permission = _XPLATSTR("O:S-1-5-21-2127521184-1604012920-1887927527-21560751G:S-1-5-21-2127521184-1604012920-1887927527-513D:(A;;FA;;;SY)(A;;FA;;;BA)(A;;0x1200a9;;;S-1-5-21-397955417-626881126-188441444-3053964)");
41+
utility::string_t permission_key = share.upload_file_permission(permission);
42+
43+
azure::storage::cloud_file_directory directory = share.get_directory_reference(_XPLATSTR("my-sample-directory"));
44+
directory.delete_directory_if_exists();
45+
46+
// Create a new directory with properties.
47+
directory.properties().set_attributes(azure::storage::cloud_file_attributes::directory | azure::storage::cloud_file_attributes::system);
48+
directory.properties().set_creation_time(azure::storage::cloud_file_directory_properties::now);
49+
directory.properties().set_last_write_time(utility::datetime::from_string(_XPLATSTR("Thu, 31 Oct 2019 06:42:18 GMT")));
50+
// You can specify either permission or permission key, but not both.
51+
directory.properties().set_permission(permission);
52+
//directory.properties().set_permission_key(permission_key);
53+
directory.create();
54+
55+
// Upload a file.
56+
azure::storage::cloud_file file = directory.get_file_reference(_XPLATSTR("my-sample-file-1"));
57+
// Properties for file are pretty much the same as for directory.
58+
// You can leave properties unset to use default.
59+
file.properties().set_attributes(azure::storage::cloud_file_attributes::archive);
60+
//file.properties().set_permission(azure::storage::cloud_file_properties::inherit);
61+
file.properties().set_permission_key(permission_key);
62+
file.upload_text(_XPLATSTR("some text"));
63+
64+
// Update properties for an existing file/directory.
65+
file.properties().set_creation_time(utility::datetime::from_string(_XPLATSTR("Wed, 10 Oct 2001 20:51:31 +0000")));
66+
file.upload_properties();
67+
68+
file.delete_file();
69+
directory.delete_directory();
70+
share.delete_share_if_exists();
71+
}
72+
catch (const azure::storage::storage_exception& e)
73+
{
74+
ucout << _XPLATSTR("Error: ") << e.what() << std::endl;
75+
76+
azure::storage::request_result result = e.result();
77+
azure::storage::storage_extended_error extended_error = result.extended_error();
78+
if (!extended_error.message().empty())
79+
{
80+
ucout << extended_error.message() << std::endl;
81+
}
82+
}
83+
catch (const std::exception& e)
84+
{
85+
ucout << _XPLATSTR("Error: ") << e.what() << std::endl;
86+
}
87+
}
88+
89+
}}} // namespace azure::storage::samples

Microsoft.WindowsAzure.Storage/samples/Microsoft.WindowsAzure.Storage.Samples.v141.vcxproj

+1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
<ItemGroup>
141141
<ClCompile Include="BlobsGettingStarted.cpp" />
142142
<ClCompile Include="FilesGettingStarted.cpp" />
143+
<ClCompile Include="FilesProperties.cpp" />
143144
<ClCompile Include="JsonPayloadFormat.cpp" />
144145
<ClCompile Include="main.cpp" />
145146
<ClCompile Include="NativeClientLibraryDemo1.cpp" />

Microsoft.WindowsAzure.Storage/samples/Microsoft.WindowsAzure.Storage.Samples.v141.vcxproj.filters

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<ClCompile Include="JsonPayloadFormat.cpp" />
1111
<ClCompile Include="NativeClientLibraryDemo1.cpp" />
1212
<ClCompile Include="NativeClientLibraryDemo2.cpp" />
13+
<ClCompile Include="FilesProperties.cpp" />
1314
</ItemGroup>
1415
<ItemGroup>
1516
<ClInclude Include="samples_common.h" />

0 commit comments

Comments
 (0)