-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
ShareSample.java
170 lines (145 loc) · 6.81 KB
/
ShareSample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.storage.file.share;
import com.azure.core.util.Configuration;
import com.azure.core.util.CoreUtils;
import com.azure.storage.file.share.models.ShareProperties;
import com.azure.storage.file.share.models.ShareStorageException;
/**
* Sample demonstrates how to create, list and delete shares, and get and set properties.
*/
public class ShareSample {
private static final String ENDPOINT = Configuration.getGlobalConfiguration().get("PRIMARY_STORAGE_FILE_ENDPOINT");
private static final String SASTOKEN = Configuration.getGlobalConfiguration().get("SAS_TOKEN");
// This is the helper method to generate random name.
private static String generateRandomName() {
return CoreUtils.randomUuid().toString().substring(0, 8);
}
/**
* The main method shows how to do the base operation using share sync client.
* @param args No args needed for the main method
*/
public static void main(String[] args) {
// Build a share client
String shareName = generateRandomName();
ShareClient shareClient = new ShareClientBuilder().endpoint(ENDPOINT)
.sasToken(SASTOKEN)
.shareName(shareName).buildClient();
// Create a share using the share client
try {
shareClient.create();
} catch (ShareStorageException e) {
System.out.printf("Failed to create the share %s with share client. Reasons: %s%n", shareName, e.getMessage());
}
// Create first snapshot on share.
String shareSnapshot1 = null;
try {
shareSnapshot1 = shareClient.createSnapshot().getSnapshot();
} catch (ShareStorageException e) {
System.out.println("Failed to create snapshot on share. Reasons: " + e.getMessage());
}
// Create 3 directories using share client
for (int i = 0; i < 3; i++) {
try {
shareClient.createDirectory(generateRandomName());
} catch (ShareStorageException e) {
System.out.println("Failed to create directory using the share client. Reasons: " + e.getMessage());
}
}
// Create another snapshot on share.
String shareSnapshot2 = null;
try {
shareSnapshot2 = shareClient.createSnapshot().getSnapshot();
} catch (ShareStorageException e) {
System.out.println("Failed to create snapshot on share. Reasons: " + e.getMessage());
}
// Create another directory after having the snapshot
try {
shareClient.createDirectory(generateRandomName());
} catch (ShareStorageException e) {
System.out.println("Failed to create directory using the share client. Reasons: " + e.getMessage());
}
// Get the properties of the share with first snapshot.
ShareClient shareClientWithSnapshot1 = new ShareClientBuilder()
.endpoint(ENDPOINT)
.sasToken(SASTOKEN)
.shareName(shareName)
.snapshot(shareSnapshot1)
.buildClient();
try {
ShareProperties shareProperties1 = shareClientWithSnapshot1.getProperties();
System.out.println("This is the first snapshot eTag: " + shareProperties1.getETag());
} catch (ShareStorageException e) {
System.out.println("Failed to get properties for the first share snapshot. Reasons: " + e.getMessage());
}
// Get the properties of the share with second snapshot.
ShareClient shareClientWithSnapshot2 = new ShareClientBuilder()
.endpoint(ENDPOINT)
.sasToken(SASTOKEN)
.shareName(shareName)
.snapshot(shareSnapshot2)
.buildClient();
try {
ShareProperties shareProperties2 = shareClientWithSnapshot2.getProperties();
System.out.println("This is the second snapshot eTag: " + shareProperties2.getETag());
} catch (ShareStorageException e) {
System.out.println("Failed to get properties for the second share snapshot. Reasons: " + e.getMessage());
}
// Get the root directory and list all directories.
try {
shareClient.getRootDirectoryClient().listFilesAndDirectories().forEach(resource ->
System.out.printf("Name: %s, Directory? %b%n", resource.getName(), resource.isDirectory()));
} catch (ShareStorageException e) {
System.out.println("Failed to delete the share. Reasons: " + e.getMessage());
}
// Create a file with size of 1024 bytes under the share.
try {
ShareFileClient fileClient = shareClient.getFileClient("myFile");
fileClient.create(1024);
} catch (ShareStorageException e) {
System.out.println("Failed to create a file under the share. Reasons: " + e.getMessage());
}
// Create a file with size of 1024 bytes under the share using createFile API.
try {
shareClient.createFile(generateRandomName(), 1024);
} catch (ShareStorageException e) {
System.out.println("Failed to create file using the share client. Reasons: " + e.getMessage());
}
// Delete the file "myFile" using shareClient.
try {
shareClient.deleteFile("myFile");
} catch (ShareStorageException e) {
System.out.println("Failed to delete the file named myFile. Reasons: " + e.getMessage());
}
// Delete the share snapshot 1
try {
shareClientWithSnapshot1.delete();
} catch (ShareStorageException e) {
System.out.println("Failed to delete the share snapshot 1. Reasons: " + e.getMessage());
}
// Delete the share snapshot 2
try {
shareClientWithSnapshot2.delete();
} catch (ShareStorageException e) {
System.out.println("Failed to delete the share snapshot 2. Reasons: " + e.getMessage());
}
// Check the delete share snapshot properties.
try {
shareClientWithSnapshot1.getProperties();
} catch (ShareStorageException e) {
System.out.println("This is expected. The snapshot has been delete. Reasons: " + e.getMessage());
}
// Check the one not deleted.
try {
shareClientWithSnapshot2.getProperties();
} catch (ShareStorageException e) {
System.out.println("Failed to get the properties of share snapshot 2. Reasons: " + e.getMessage());
}
// Delete the share.
try {
shareClient.delete();
} catch (ShareStorageException e) {
System.out.println("Failed to delete the share. Reasons: " + e.getMessage());
}
}
}